blob: 4a295d312572389fd3fe12a980b6ad5663b6991d [file] [log] [blame]
Damien Miller9f2abc42006-07-10 20:53:08 +10001/* $OpenBSD: monitor.c,v 1.78 2006/07/06 16:03:53 stevesk Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110032#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000033
Damien Miller6645e7a2006-03-15 14:42:54 +110034#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110035#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110036#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100037#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110038#include <signal.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000039
40#ifdef SKEY
41#include <skey.h>
42#endif
43
Damien Miller03e20032006-03-15 11:16:59 +110044#include <openssl/dh.h>
45
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000046#include "ssh.h"
47#include "auth.h"
48#include "kex.h"
49#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000050#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
51#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000052#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000053#define TARGET_OS_MAC 1
54#else
55#include "zlib.h"
56#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000057#include "packet.h"
58#include "auth-options.h"
59#include "sshpty.h"
60#include "channels.h"
61#include "session.h"
62#include "sshlogin.h"
63#include "canohost.h"
64#include "log.h"
65#include "servconf.h"
66#include "monitor.h"
67#include "monitor_mm.h"
68#include "monitor_wrap.h"
69#include "monitor_fdpass.h"
70#include "xmalloc.h"
71#include "misc.h"
72#include "buffer.h"
73#include "bufaux.h"
74#include "compat.h"
75#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076
Darren Tucker0efd1552003-08-26 11:49:55 +100077#ifdef GSSAPI
78#include "ssh-gss.h"
79static Gssctxt *gsscontext = NULL;
80#endif
81
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082/* Imports */
83extern ServerOptions options;
84extern u_int utmp_len;
85extern Newkeys *current_keys[];
86extern z_stream incoming_stream;
87extern z_stream outgoing_stream;
88extern u_char session_id[];
89extern Buffer input, output;
90extern Buffer auth_debug;
91extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +100092extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093
94/* State exported from the child */
95
96struct {
97 z_stream incoming;
98 z_stream outgoing;
99 u_char *keyin;
100 u_int keyinlen;
101 u_char *keyout;
102 u_int keyoutlen;
103 u_char *ivin;
104 u_int ivinlen;
105 u_char *ivout;
106 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000107 u_char *ssh1key;
108 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109 int ssh1cipher;
110 int ssh1protoflags;
111 u_char *input;
112 u_int ilen;
113 u_char *output;
114 u_int olen;
115} child_state;
116
Damien Miller469954d2003-06-18 20:25:33 +1000117/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118
119int mm_answer_moduli(int, Buffer *);
120int mm_answer_sign(int, Buffer *);
121int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000122int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123int mm_answer_authserv(int, Buffer *);
124int mm_answer_authpassword(int, Buffer *);
125int mm_answer_bsdauthquery(int, Buffer *);
126int mm_answer_bsdauthrespond(int, Buffer *);
127int mm_answer_skeyquery(int, Buffer *);
128int mm_answer_skeyrespond(int, Buffer *);
129int mm_answer_keyallowed(int, Buffer *);
130int mm_answer_keyverify(int, Buffer *);
131int mm_answer_pty(int, Buffer *);
132int mm_answer_pty_cleanup(int, Buffer *);
133int mm_answer_term(int, Buffer *);
134int mm_answer_rsa_keyallowed(int, Buffer *);
135int mm_answer_rsa_challenge(int, Buffer *);
136int mm_answer_rsa_response(int, Buffer *);
137int mm_answer_sesskey(int, Buffer *);
138int mm_answer_sessid(int, Buffer *);
139
Damien Miller79418552002-04-23 20:28:48 +1000140#ifdef USE_PAM
141int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000142int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000143int mm_answer_pam_init_ctx(int, Buffer *);
144int mm_answer_pam_query(int, Buffer *);
145int mm_answer_pam_respond(int, Buffer *);
146int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000147#endif
148
Darren Tucker0efd1552003-08-26 11:49:55 +1000149#ifdef GSSAPI
150int mm_answer_gss_setup_ctx(int, Buffer *);
151int mm_answer_gss_accept_ctx(int, Buffer *);
152int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100153int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000154#endif
Damien Miller25162f22002-09-12 09:47:29 +1000155
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100156#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100157int mm_answer_audit_event(int, Buffer *);
158int mm_answer_audit_command(int, Buffer *);
159#endif
160
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000161static Authctxt *authctxt;
162static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
163
164/* local state for key verify */
165static u_char *key_blob = NULL;
166static u_int key_bloblen = 0;
167static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000168static char *hostbased_cuser = NULL;
169static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000170static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000171static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000172static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000173static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174
175struct mon_table {
176 enum monitor_reqtype type;
177 int flags;
178 int (*f)(int, Buffer *);
179};
180
181#define MON_ISAUTH 0x0004 /* Required for Authentication */
182#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
183#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100184#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000185
186#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
187
188#define MON_PERMIT 0x1000 /* Request is permitted */
189
190struct mon_table mon_dispatch_proto20[] = {
191 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
192 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
193 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
194 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000195 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000196 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000197#ifdef USE_PAM
198 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000199 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000200 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
201 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
202 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
203 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000204#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100205#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100206 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100207#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208#ifdef BSD_AUTH
209 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100210 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211#endif
212#ifdef SKEY
213 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
214 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
215#endif
216 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
217 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000218#ifdef GSSAPI
219 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
220 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
221 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100222 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000223#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224 {0, 0, NULL}
225};
226
227struct mon_table mon_dispatch_postauth20[] = {
228 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
229 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
230 {MONITOR_REQ_PTY, 0, mm_answer_pty},
231 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
232 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100233#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100234 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
235 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
236#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237 {0, 0, NULL}
238};
239
240struct mon_table mon_dispatch_proto15[] = {
241 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
242 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
243 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
244 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100245 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
246 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000247 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
248 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
249#ifdef BSD_AUTH
250 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100251 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252#endif
253#ifdef SKEY
254 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
255 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
256#endif
Damien Millera33501b2002-05-08 12:24:42 +1000257#ifdef USE_PAM
258 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000259 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000260 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
261 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
262 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
263 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000264#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100265#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100266 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100267#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268 {0, 0, NULL}
269};
270
271struct mon_table mon_dispatch_postauth15[] = {
272 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
273 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
274 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100275#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100276 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100277 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100278#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000279 {0, 0, NULL}
280};
281
282struct mon_table *mon_dispatch;
283
284/* Specifies if a certain message is allowed at the moment */
285
286static void
287monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
288{
289 while (ent->f != NULL) {
290 if (ent->type == type) {
291 ent->flags &= ~MON_PERMIT;
292 ent->flags |= permit ? MON_PERMIT : 0;
293 return;
294 }
295 ent++;
296 }
297}
298
299static void
300monitor_permit_authentications(int permit)
301{
302 struct mon_table *ent = mon_dispatch;
303
304 while (ent->f != NULL) {
305 if (ent->flags & MON_AUTH) {
306 ent->flags &= ~MON_PERMIT;
307 ent->flags |= permit ? MON_PERMIT : 0;
308 }
309 ent++;
310 }
311}
312
Darren Tucker3e33cec2003-10-02 16:12:36 +1000313void
314monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000315{
316 struct mon_table *ent;
317 int authenticated = 0;
318
319 debug3("preauth child monitor started");
320
Darren Tucker3e33cec2003-10-02 16:12:36 +1000321 authctxt = _authctxt;
322 memset(authctxt, 0, sizeof(*authctxt));
323
Darren Tuckerde0de392005-03-31 23:52:04 +1000324 authctxt->loginmsg = &loginmsg;
325
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000326 if (compat20) {
327 mon_dispatch = mon_dispatch_proto20;
328
329 /* Permit requests for moduli and signatures */
330 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
331 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
332 } else {
333 mon_dispatch = mon_dispatch_proto15;
334
335 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
336 }
337
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000338 /* The first few requests do not require asynchronous access */
339 while (!authenticated) {
Damien Miller7a8f5b32006-03-31 23:14:23 +1100340 auth_method = "unknown";
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000341 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000342 if (authenticated) {
343 if (!(ent->flags & MON_AUTHDECIDE))
344 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000345 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000346 if (authctxt->pw->pw_uid == 0 &&
347 !auth_root_allowed(auth_method))
348 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000349#ifdef USE_PAM
350 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100351 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000352 Buffer m;
353
354 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100355 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000356 MONITOR_REQ_PAM_ACCOUNT, &m);
357 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
358 buffer_free(&m);
359 }
360#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361 }
362
Damien Miller7a8f5b32006-03-31 23:14:23 +1100363 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364 auth_log(authctxt, authenticated, auth_method,
365 compat20 ? " ssh2" : "");
366 if (!authenticated)
367 authctxt->failures++;
368 }
369 }
370
371 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000372 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100373 if (strcmp(auth_method, "unknown") == 0)
374 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000375
376 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000377 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000378
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000379 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000380}
381
Damien Millerbe64d432003-05-14 19:31:12 +1000382static void
383monitor_set_child_handler(pid_t pid)
384{
385 monitor_child_pid = pid;
386}
387
388static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000389monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000390{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000391 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000392}
393
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000394void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000395monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000396{
Damien Millerbe64d432003-05-14 19:31:12 +1000397 monitor_set_child_handler(pmonitor->m_pid);
398 signal(SIGHUP, &monitor_child_handler);
399 signal(SIGTERM, &monitor_child_handler);
400
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000401 if (compat20) {
402 mon_dispatch = mon_dispatch_postauth20;
403
404 /* Permit requests for moduli and signatures */
405 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000408 } else {
409 mon_dispatch = mon_dispatch_postauth15;
410 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
411 }
412 if (!no_pty_flag) {
413 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
414 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
415 }
416
417 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000418 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000419}
420
421void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000422monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000423{
Damien Miller2d6b8332002-06-21 15:59:49 +1000424 if (options.compression) {
425 /* The member allocation is not visible, so sync it */
426 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
427 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000428}
429
430int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000431monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000432 struct mon_table **pent)
433{
434 Buffer m;
435 int ret;
436 u_char type;
437
438 buffer_init(&m);
439
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000440 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000441 type = buffer_get_char(&m);
442
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000443 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444
445 while (ent->f != NULL) {
446 if (ent->type == type)
447 break;
448 ent++;
449 }
450
451 if (ent->f != NULL) {
452 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000453 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000455 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000456 buffer_free(&m);
457
458 /* The child may use this request only once, disable it */
459 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000460 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000461 type);
462 ent->flags &= ~MON_PERMIT;
463 }
464
465 if (pent != NULL)
466 *pent = ent;
467
468 return ret;
469 }
470
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000471 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472
473 /* NOTREACHED */
474 return (-1);
475}
476
477/* allowed key state */
478static int
479monitor_allowed_key(u_char *blob, u_int bloblen)
480{
481 /* make sure key is allowed */
482 if (key_blob == NULL || key_bloblen != bloblen ||
483 memcmp(key_blob, blob, key_bloblen))
484 return (0);
485 return (1);
486}
487
488static void
489monitor_reset_key_state(void)
490{
491 /* reset state */
492 if (key_blob != NULL)
493 xfree(key_blob);
494 if (hostbased_cuser != NULL)
495 xfree(hostbased_cuser);
496 if (hostbased_chost != NULL)
497 xfree(hostbased_chost);
498 key_blob = NULL;
499 key_bloblen = 0;
500 key_blobtype = MM_NOKEY;
501 hostbased_cuser = NULL;
502 hostbased_chost = NULL;
503}
504
505int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000506mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000507{
508 DH *dh;
509 int min, want, max;
510
511 min = buffer_get_int(m);
512 want = buffer_get_int(m);
513 max = buffer_get_int(m);
514
515 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000516 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517 /* We need to check here, too, in case the child got corrupted */
518 if (max < min || want < min || max < want)
519 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000520 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000521
522 buffer_clear(m);
523
524 dh = choose_dh(min, want, max);
525 if (dh == NULL) {
526 buffer_put_char(m, 0);
527 return (0);
528 } else {
529 /* Send first bignum */
530 buffer_put_char(m, 1);
531 buffer_put_bignum2(m, dh->p);
532 buffer_put_bignum2(m, dh->g);
533
534 DH_free(dh);
535 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000536 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000537 return (0);
538}
539
540int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000541mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542{
543 Key *key;
544 u_char *p;
545 u_char *signature;
546 u_int siglen, datlen;
547 int keyid;
548
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000549 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550
551 keyid = buffer_get_int(m);
552 p = buffer_get_string(m, &datlen);
553
Damien Millera63128d2006-03-15 12:08:28 +1100554 /*
Damien Millerc91e5562006-03-26 13:58:55 +1100555 * Supported KEX types will only return SHA1 (20 byte) or
Damien Millera63128d2006-03-15 12:08:28 +1100556 * SHA256 (32 byte) hashes
557 */
558 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000559 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000560
Ben Lindstromf67e0772002-06-06 20:58:19 +0000561 /* save session id, it will be passed on the first call */
562 if (session_id2_len == 0) {
563 session_id2_len = datlen;
564 session_id2 = xmalloc(session_id2_len);
565 memcpy(session_id2, p, session_id2_len);
566 }
567
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000568 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000569 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000571 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572
Ben Lindstromd5502182002-06-27 00:12:57 +0000573 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000574
575 buffer_clear(m);
576 buffer_put_string(m, signature, siglen);
577
578 xfree(p);
579 xfree(signature);
580
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000581 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582
583 /* Turn on permissions for getpwnam */
584 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
585
586 return (0);
587}
588
589/* Retrieves the password entry and also checks if the user is permitted */
590
591int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000592mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000594 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000595 struct passwd *pwent;
596 int allowed = 0;
597
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000598 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000599
600 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000601 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602
Darren Tuckerb09b6772004-06-22 15:06:46 +1000603 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604
Darren Tuckerb09b6772004-06-22 15:06:46 +1000605 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606
Darren Tuckerb09b6772004-06-22 15:06:46 +1000607 authctxt->user = xstrdup(username);
608 setproctitle("%s [priv]", pwent ? username : "unknown");
609 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610
611 buffer_clear(m);
612
613 if (pwent == NULL) {
614 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100615 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616 goto out;
617 }
618
619 allowed = 1;
620 authctxt->pw = pwent;
621 authctxt->valid = 1;
622
623 buffer_put_char(m, 1);
624 buffer_put_string(m, pwent, sizeof(struct passwd));
625 buffer_put_cstring(m, pwent->pw_name);
626 buffer_put_cstring(m, "*");
627 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000628#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000629 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000630#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000631 buffer_put_cstring(m, pwent->pw_dir);
632 buffer_put_cstring(m, pwent->pw_shell);
633
634 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000635 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000636 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637
638 /* For SSHv1 allow authentication now */
639 if (!compat20)
640 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000641 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000642 /* Allow service/style information on the auth context */
643 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000644 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
645 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646
Damien Millera33501b2002-05-08 12:24:42 +1000647#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000648 if (options.use_pam)
649 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000650#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100651#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100652 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100653#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654
655 return (0);
656}
657
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000658int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000659{
660 char *banner;
661
662 buffer_clear(m);
663 banner = auth2_read_banner();
664 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000665 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000666
667 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000668 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000669
670 return (0);
671}
672
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000673int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000674mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675{
676 monitor_permit_authentications(1);
677
678 authctxt->service = buffer_get_string(m, NULL);
679 authctxt->style = buffer_get_string(m, NULL);
680 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000681 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682
683 if (strlen(authctxt->style) == 0) {
684 xfree(authctxt->style);
685 authctxt->style = NULL;
686 }
687
688 return (0);
689}
690
691int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000692mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693{
694 static int call_count;
695 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000696 int authenticated;
697 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000698
699 passwd = buffer_get_string(m, &plen);
700 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000701 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000702 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703 memset(passwd, 0, strlen(passwd));
704 xfree(passwd);
705
706 buffer_clear(m);
707 buffer_put_int(m, authenticated);
708
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000709 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000710 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000711
712 call_count++;
713 if (plen == 0 && call_count == 1)
714 auth_method = "none";
715 else
716 auth_method = "password";
717
718 /* Causes monitor loop to terminate if authenticated */
719 return (authenticated);
720}
721
722#ifdef BSD_AUTH
723int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000724mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725{
726 char *name, *infotxt;
727 u_int numprompts;
728 u_int *echo_on;
729 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100730 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731
Damien Millerb7df3af2003-02-24 11:55:46 +1100732 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
733 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734
735 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100736 buffer_put_int(m, success);
737 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738 buffer_put_cstring(m, prompts[0]);
739
Damien Millerb7df3af2003-02-24 11:55:46 +1100740 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000741 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000742
Damien Millerb7df3af2003-02-24 11:55:46 +1100743 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744 xfree(name);
745 xfree(infotxt);
746 xfree(prompts);
747 xfree(echo_on);
748 }
749
750 return (0);
751}
752
753int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000754mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755{
756 char *response;
757 int authok;
758
759 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000760 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761
762 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000763 authok = options.challenge_response_authentication &&
764 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000765 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000766 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000767 xfree(response);
768
769 buffer_clear(m);
770 buffer_put_int(m, authok);
771
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000772 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000773 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000774
775 auth_method = "bsdauth";
776
777 return (authok != 0);
778}
779#endif
780
781#ifdef SKEY
782int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000783mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784{
785 struct skey skey;
786 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100787 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000788
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000789 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
790 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791
792 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100793 buffer_put_int(m, success);
794 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000795 buffer_put_cstring(m, challenge);
796
Damien Millerb7df3af2003-02-24 11:55:46 +1100797 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000798 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000799
800 return (0);
801}
802
803int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000804mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000805{
806 char *response;
807 int authok;
808
809 response = buffer_get_string(m, NULL);
810
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000811 authok = (options.challenge_response_authentication &&
812 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000813 skey_haskey(authctxt->pw->pw_name) == 0 &&
814 skey_passcheck(authctxt->pw->pw_name, response) != -1);
815
816 xfree(response);
817
818 buffer_clear(m);
819 buffer_put_int(m, authok);
820
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000821 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000822 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823
824 auth_method = "skey";
825
826 return (authok != 0);
827}
828#endif
829
Damien Miller79418552002-04-23 20:28:48 +1000830#ifdef USE_PAM
831int
Darren Tucker7eda5922004-06-22 13:22:50 +1000832mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000833{
Damien Miller4e448a32003-05-14 15:11:48 +1000834 if (!options.use_pam)
835 fatal("UsePAM not set, but ended up in %s anyway", __func__);
836
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100837 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000838
Damien Miller1f499fd2003-08-25 13:08:49 +1000839 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
840
Damien Miller79418552002-04-23 20:28:48 +1000841 return (0);
842}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000843
Damien Miller1f499fd2003-08-25 13:08:49 +1000844int
Darren Tucker7eda5922004-06-22 13:22:50 +1000845mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000846{
847 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100848
Damien Miller1f499fd2003-08-25 13:08:49 +1000849 if (!options.use_pam)
850 fatal("UsePAM not set, but ended up in %s anyway", __func__);
851
852 ret = do_pam_account();
853
854 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000855 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000856
Darren Tucker7eda5922004-06-22 13:22:50 +1000857 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000858
859 return (ret);
860}
861
Damien Miller4f9f42a2003-05-10 19:28:02 +1000862static void *sshpam_ctxt, *sshpam_authok;
863extern KbdintDevice sshpam_device;
864
865int
Darren Tucker7eda5922004-06-22 13:22:50 +1000866mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000867{
868
869 debug3("%s", __func__);
870 authctxt->user = buffer_get_string(m, NULL);
871 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
872 sshpam_authok = NULL;
873 buffer_clear(m);
874 if (sshpam_ctxt != NULL) {
875 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
876 buffer_put_int(m, 1);
877 } else {
878 buffer_put_int(m, 0);
879 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000880 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000881 return (0);
882}
883
884int
Darren Tucker7eda5922004-06-22 13:22:50 +1000885mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000886{
887 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000888 u_int i, num, *echo_on;
889 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000890
891 debug3("%s", __func__);
892 sshpam_authok = NULL;
893 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
894 if (ret == 0 && num == 0)
895 sshpam_authok = sshpam_ctxt;
896 if (num > 1 || name == NULL || info == NULL)
897 ret = -1;
898 buffer_clear(m);
899 buffer_put_int(m, ret);
900 buffer_put_cstring(m, name);
901 xfree(name);
902 buffer_put_cstring(m, info);
903 xfree(info);
904 buffer_put_int(m, num);
905 for (i = 0; i < num; ++i) {
906 buffer_put_cstring(m, prompts[i]);
907 xfree(prompts[i]);
908 buffer_put_int(m, echo_on[i]);
909 }
910 if (prompts != NULL)
911 xfree(prompts);
912 if (echo_on != NULL)
913 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000914 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +1000915 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000916 return (0);
917}
918
919int
Darren Tucker7eda5922004-06-22 13:22:50 +1000920mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000921{
922 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000923 u_int i, num;
924 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000925
926 debug3("%s", __func__);
927 sshpam_authok = NULL;
928 num = buffer_get_int(m);
929 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +1000930 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000931 for (i = 0; i < num; ++i)
932 resp[i] = buffer_get_string(m, NULL);
933 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
934 for (i = 0; i < num; ++i)
935 xfree(resp[i]);
936 xfree(resp);
937 } else {
938 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
939 }
940 buffer_clear(m);
941 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000942 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000943 auth_method = "keyboard-interactive/pam";
944 if (ret == 0)
945 sshpam_authok = sshpam_ctxt;
946 return (0);
947}
948
949int
Darren Tucker7eda5922004-06-22 13:22:50 +1000950mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000951{
952
953 debug3("%s", __func__);
954 (sshpam_device.free_ctx)(sshpam_ctxt);
955 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000956 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000957 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000958 return (sshpam_authok == sshpam_ctxt);
959}
Damien Miller79418552002-04-23 20:28:48 +1000960#endif
961
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000962static void
963mm_append_debug(Buffer *m)
964{
965 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000966 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967 buffer_append(m, buffer_ptr(&auth_debug),
968 buffer_len(&auth_debug));
969 buffer_clear(&auth_debug);
970 }
971}
972
973int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000974mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975{
976 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000977 char *cuser, *chost;
978 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979 u_int bloblen;
980 enum mm_keytype type = 0;
981 int allowed = 0;
982
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000983 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984
985 type = buffer_get_int(m);
986 cuser = buffer_get_string(m, NULL);
987 chost = buffer_get_string(m, NULL);
988 blob = buffer_get_string(m, &bloblen);
989
990 key = key_from_blob(blob, bloblen);
991
992 if ((compat20 && type == MM_RSAHOSTKEY) ||
993 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000994 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000995
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000996 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997
Damien Miller3e3b5142003-11-17 21:13:40 +1100998 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +1100999 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001000 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001001 allowed = options.pubkey_authentication &&
1002 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001003 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004 break;
1005 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001006 allowed = options.hostbased_authentication &&
1007 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001008 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001009 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001010 break;
1011 case MM_RSAHOSTKEY:
1012 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001013 allowed = options.rhosts_rsa_authentication &&
1014 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001015 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001016 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001017 break;
1018 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001019 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001020 break;
1021 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022 }
Damien Miller00111382003-03-10 11:21:17 +11001023 if (key != NULL)
1024 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025
1026 /* clear temporarily storage (used by verify) */
1027 monitor_reset_key_state();
1028
1029 if (allowed) {
1030 /* Save temporarily for comparison in verify */
1031 key_blob = blob;
1032 key_bloblen = bloblen;
1033 key_blobtype = type;
1034 hostbased_cuser = cuser;
1035 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001036 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001037 /* Log failed attempt */
1038 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001039 xfree(blob);
1040 xfree(cuser);
1041 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001042 }
1043
1044 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001045 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001046
1047 buffer_clear(m);
1048 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001049 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001050
1051 mm_append_debug(m);
1052
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001053 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001054
1055 if (type == MM_RSAHOSTKEY)
1056 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1057
1058 return (0);
1059}
1060
1061static int
1062monitor_valid_userblob(u_char *data, u_int datalen)
1063{
1064 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001065 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001066 u_int len;
1067 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001068
1069 buffer_init(&b);
1070 buffer_append(&b, data, datalen);
1071
1072 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001073 p = buffer_ptr(&b);
1074 len = buffer_len(&b);
1075 if ((session_id2 == NULL) ||
1076 (len < session_id2_len) ||
1077 (memcmp(p, session_id2, session_id2_len) != 0))
1078 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001079 buffer_consume(&b, session_id2_len);
1080 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001081 p = buffer_get_string(&b, &len);
1082 if ((session_id2 == NULL) ||
1083 (len != session_id2_len) ||
1084 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001085 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001086 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001087 }
1088 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1089 fail++;
1090 p = buffer_get_string(&b, NULL);
1091 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001092 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001093 authctxt->user, p);
1094 fail++;
1095 }
1096 xfree(p);
1097 buffer_skip_string(&b);
1098 if (datafellows & SSH_BUG_PKAUTH) {
1099 if (!buffer_get_char(&b))
1100 fail++;
1101 } else {
1102 p = buffer_get_string(&b, NULL);
1103 if (strcmp("publickey", p) != 0)
1104 fail++;
1105 xfree(p);
1106 if (!buffer_get_char(&b))
1107 fail++;
1108 buffer_skip_string(&b);
1109 }
1110 buffer_skip_string(&b);
1111 if (buffer_len(&b) != 0)
1112 fail++;
1113 buffer_free(&b);
1114 return (fail == 0);
1115}
1116
1117static int
Damien Millera10f5612002-09-12 09:49:15 +10001118monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1119 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001120{
1121 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001122 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001123 u_int len;
1124 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001125
1126 buffer_init(&b);
1127 buffer_append(&b, data, datalen);
1128
Ben Lindstromf67e0772002-06-06 20:58:19 +00001129 p = buffer_get_string(&b, &len);
1130 if ((session_id2 == NULL) ||
1131 (len != session_id2_len) ||
1132 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001133 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001134 xfree(p);
1135
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001136 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1137 fail++;
1138 p = buffer_get_string(&b, NULL);
1139 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001140 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001141 authctxt->user, p);
1142 fail++;
1143 }
1144 xfree(p);
1145 buffer_skip_string(&b); /* service */
1146 p = buffer_get_string(&b, NULL);
1147 if (strcmp(p, "hostbased") != 0)
1148 fail++;
1149 xfree(p);
1150 buffer_skip_string(&b); /* pkalg */
1151 buffer_skip_string(&b); /* pkblob */
1152
1153 /* verify client host, strip trailing dot if necessary */
1154 p = buffer_get_string(&b, NULL);
1155 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1156 p[len - 1] = '\0';
1157 if (strcmp(p, chost) != 0)
1158 fail++;
1159 xfree(p);
1160
1161 /* verify client user */
1162 p = buffer_get_string(&b, NULL);
1163 if (strcmp(p, cuser) != 0)
1164 fail++;
1165 xfree(p);
1166
1167 if (buffer_len(&b) != 0)
1168 fail++;
1169 buffer_free(&b);
1170 return (fail == 0);
1171}
1172
1173int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001174mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001175{
1176 Key *key;
1177 u_char *signature, *data, *blob;
1178 u_int signaturelen, datalen, bloblen;
1179 int verified = 0;
1180 int valid_data = 0;
1181
1182 blob = buffer_get_string(m, &bloblen);
1183 signature = buffer_get_string(m, &signaturelen);
1184 data = buffer_get_string(m, &datalen);
1185
1186 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001187 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001188 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189
1190 key = key_from_blob(blob, bloblen);
1191 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001192 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001193
1194 switch (key_blobtype) {
1195 case MM_USERKEY:
1196 valid_data = monitor_valid_userblob(data, datalen);
1197 break;
1198 case MM_HOSTKEY:
1199 valid_data = monitor_valid_hostbasedblob(data, datalen,
1200 hostbased_cuser, hostbased_chost);
1201 break;
1202 default:
1203 valid_data = 0;
1204 break;
1205 }
1206 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001207 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001208
1209 verified = key_verify(key, signature, signaturelen, data, datalen);
1210 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001211 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212
1213 key_free(key);
1214 xfree(blob);
1215 xfree(signature);
1216 xfree(data);
1217
Ben Lindstrome1c09122002-06-23 00:38:24 +00001218 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1219
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001220 monitor_reset_key_state();
1221
1222 buffer_clear(m);
1223 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001224 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226 return (verified);
1227}
1228
1229static void
1230mm_record_login(Session *s, struct passwd *pw)
1231{
1232 socklen_t fromlen;
1233 struct sockaddr_storage from;
1234
1235 /*
1236 * Get IP address of client. If the connection is not a socket, let
1237 * the address be 0.0.0.0.
1238 */
1239 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001240 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001242 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001243 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001245 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246 }
1247 }
1248 /* Record that there was a login on that tty from the remote host. */
1249 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001250 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001251 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001252}
1253
1254static void
1255mm_session_close(Session *s)
1256{
Damien Miller04bd8b02003-05-25 14:38:33 +10001257 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001258 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001259 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001260 session_pty_cleanup2(s);
1261 }
1262 s->used = 0;
1263}
1264
1265int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001266mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001267{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001268 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269 Session *s;
1270 int res, fd0;
1271
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001272 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001273
1274 buffer_clear(m);
1275 s = session_new();
1276 if (s == NULL)
1277 goto error;
1278 s->authctxt = authctxt;
1279 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001280 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001281 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1282 if (res == 0)
1283 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284 pty_setowner(authctxt->pw, s->tty);
1285
1286 buffer_put_int(m, 1);
1287 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001288
1289 /* We need to trick ttyslot */
1290 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001291 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001292
1293 mm_record_login(s, authctxt->pw);
1294
1295 /* Now we can close the file descriptor again */
1296 close(0);
1297
Darren Tucker09991742004-07-17 17:05:14 +10001298 /* send messages generated by record_login */
1299 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1300 buffer_clear(&loginmsg);
1301
1302 mm_request_send(sock, MONITOR_ANS_PTY, m);
1303
1304 mm_send_fd(sock, s->ptyfd);
1305 mm_send_fd(sock, s->ttyfd);
1306
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307 /* make sure nothing uses fd 0 */
1308 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001309 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001310 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001311 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001312
1313 /* slave is not needed */
1314 close(s->ttyfd);
1315 s->ttyfd = s->ptyfd;
1316 /* no need to dup() because nobody closes ptyfd */
1317 s->ptymaster = s->ptyfd;
1318
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001319 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320
1321 return (0);
1322
1323 error:
1324 if (s != NULL)
1325 mm_session_close(s);
1326 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001327 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001328 return (0);
1329}
1330
1331int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001332mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001333{
1334 Session *s;
1335 char *tty;
1336
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001337 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001338
1339 tty = buffer_get_string(m, NULL);
1340 if ((s = session_by_tty(tty)) != NULL)
1341 mm_session_close(s);
1342 buffer_clear(m);
1343 xfree(tty);
1344 return (0);
1345}
1346
1347int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001348mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001349{
1350 BIGNUM *p;
1351 int rsafail;
1352
1353 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001354 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001355
1356 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001357 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001358
1359 buffer_get_bignum2(m, p);
1360
1361 rsafail = ssh1_session_key(p);
1362
1363 buffer_clear(m);
1364 buffer_put_int(m, rsafail);
1365 buffer_put_bignum2(m, p);
1366
1367 BN_clear_free(p);
1368
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001369 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001370
1371 /* Turn on permissions for sessid passing */
1372 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1373
1374 return (0);
1375}
1376
1377int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001378mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001379{
1380 int i;
1381
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001382 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001383
1384 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001385 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386 for (i = 0; i < 16; i++)
1387 session_id[i] = buffer_get_char(m);
1388
1389 /* Turn on permissions for getpwnam */
1390 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1391
1392 return (0);
1393}
1394
1395int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001396mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001397{
1398 BIGNUM *client_n;
1399 Key *key = NULL;
1400 u_char *blob = NULL;
1401 u_int blen = 0;
1402 int allowed = 0;
1403
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001404 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001405
Damien Miller7a8f5b32006-03-31 23:14:23 +11001406 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001407 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001409 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410 buffer_get_bignum2(m, client_n);
1411 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1412 BN_clear_free(client_n);
1413 }
1414 buffer_clear(m);
1415 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001416 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417
1418 /* clear temporarily storage (used by generate challenge) */
1419 monitor_reset_key_state();
1420
1421 if (allowed && key != NULL) {
1422 key->type = KEY_RSA; /* cheat for key_to_blob */
1423 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001424 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425 buffer_put_string(m, blob, blen);
1426
1427 /* Save temporarily for comparison in verify */
1428 key_blob = blob;
1429 key_bloblen = blen;
1430 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431 }
Damien Miller00111382003-03-10 11:21:17 +11001432 if (key != NULL)
1433 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434
1435 mm_append_debug(m);
1436
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001437 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001438
1439 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1440 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1441 return (0);
1442}
1443
1444int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001445mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446{
1447 Key *key = NULL;
1448 u_char *blob;
1449 u_int blen;
1450
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001451 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452
1453 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001454 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455 blob = buffer_get_string(m, &blen);
1456 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001457 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001458 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001459 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001461 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001462
1463 if (ssh1_challenge)
1464 BN_clear_free(ssh1_challenge);
1465 ssh1_challenge = auth_rsa_generate_challenge(key);
1466
1467 buffer_clear(m);
1468 buffer_put_bignum2(m, ssh1_challenge);
1469
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001470 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001471 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001472
1473 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001474
1475 xfree(blob);
1476 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001477 return (0);
1478}
1479
1480int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001481mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001482{
1483 Key *key = NULL;
1484 u_char *blob, *response;
1485 u_int blen, len;
1486 int success;
1487
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001488 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001489
1490 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001491 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001493 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494
1495 blob = buffer_get_string(m, &blen);
1496 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001497 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001499 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001501 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 response = buffer_get_string(m, &len);
1503 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001504 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1506
Damien Miller00111382003-03-10 11:21:17 +11001507 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508 key_free(key);
1509 xfree(response);
1510
1511 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1512
1513 /* reset state */
1514 BN_clear_free(ssh1_challenge);
1515 ssh1_challenge = NULL;
1516 monitor_reset_key_state();
1517
1518 buffer_clear(m);
1519 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001520 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001521
1522 return (success);
1523}
1524
1525int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001526mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001528 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529 int res, status;
1530
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001531 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532
1533 /* The child is terminating */
1534 session_destroy_all(&mm_session_close);
1535
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001536 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001537 if (errno != EINTR)
1538 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001539
1540 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1541
1542 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001543 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544}
1545
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001546#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001547/* Report that an audit event occurred */
1548int
1549mm_answer_audit_event(int socket, Buffer *m)
1550{
1551 ssh_audit_event_t event;
1552
1553 debug3("%s entering", __func__);
1554
1555 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001556 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001557 case SSH_AUTH_FAIL_PUBKEY:
1558 case SSH_AUTH_FAIL_HOSTBASED:
1559 case SSH_AUTH_FAIL_GSSAPI:
1560 case SSH_LOGIN_EXCEED_MAXTRIES:
1561 case SSH_LOGIN_ROOT_DENIED:
1562 case SSH_CONNECTION_CLOSE:
1563 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001564 audit_event(event);
1565 break;
1566 default:
1567 fatal("Audit event type %d not permitted", event);
1568 }
1569
1570 return (0);
1571}
1572
1573int
1574mm_answer_audit_command(int socket, Buffer *m)
1575{
1576 u_int len;
1577 char *cmd;
1578
1579 debug3("%s entering", __func__);
1580 cmd = buffer_get_string(m, &len);
1581 /* sanity check command, if so how? */
1582 audit_run_command(cmd);
1583 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001584 return (0);
1585}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001586#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001587
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001588void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001589monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590{
1591 if (compat20) {
1592 set_newkeys(MODE_IN);
1593 set_newkeys(MODE_OUT);
1594 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001595 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001596 packet_set_encryption_key(child_state.ssh1key,
1597 child_state.ssh1keylen, child_state.ssh1cipher);
1598 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001599 }
1600
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001601 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001602 packet_set_keycontext(MODE_OUT, child_state.keyout);
1603 xfree(child_state.keyout);
1604 packet_set_keycontext(MODE_IN, child_state.keyin);
1605 xfree(child_state.keyin);
1606
1607 if (!compat20) {
1608 packet_set_iv(MODE_OUT, child_state.ivout);
1609 xfree(child_state.ivout);
1610 packet_set_iv(MODE_IN, child_state.ivin);
1611 xfree(child_state.ivin);
1612 }
1613
1614 memcpy(&incoming_stream, &child_state.incoming,
1615 sizeof(incoming_stream));
1616 memcpy(&outgoing_stream, &child_state.outgoing,
1617 sizeof(outgoing_stream));
1618
1619 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001620 if (options.compression)
1621 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001622
1623 /* Network I/O buffers */
1624 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1625 buffer_clear(&input);
1626 buffer_append(&input, child_state.input, child_state.ilen);
1627 memset(child_state.input, 0, child_state.ilen);
1628 xfree(child_state.input);
1629
1630 buffer_clear(&output);
1631 buffer_append(&output, child_state.output, child_state.olen);
1632 memset(child_state.output, 0, child_state.olen);
1633 xfree(child_state.output);
1634}
1635
1636static Kex *
1637mm_get_kex(Buffer *m)
1638{
1639 Kex *kex;
1640 void *blob;
1641 u_int bloblen;
1642
Damien Miller07d86be2006-03-26 14:19:21 +11001643 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001644 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001645 if ((session_id2 == NULL) ||
1646 (kex->session_id_len != session_id2_len) ||
1647 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1648 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001649 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001650 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001651 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001652 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001653 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001654 kex->server = 1;
1655 kex->hostkey_type = buffer_get_int(m);
1656 kex->kex_type = buffer_get_int(m);
1657 blob = buffer_get_string(m, &bloblen);
1658 buffer_init(&kex->my);
1659 buffer_append(&kex->my, blob, bloblen);
1660 xfree(blob);
1661 blob = buffer_get_string(m, &bloblen);
1662 buffer_init(&kex->peer);
1663 buffer_append(&kex->peer, blob, bloblen);
1664 xfree(blob);
1665 kex->done = 1;
1666 kex->flags = buffer_get_int(m);
1667 kex->client_version_string = buffer_get_string(m, NULL);
1668 kex->server_version_string = buffer_get_string(m, NULL);
1669 kex->load_host_key=&get_hostkey_by_type;
1670 kex->host_key_index=&get_hostkey_index;
1671
1672 return (kex);
1673}
1674
1675/* This function requries careful sanity checking */
1676
1677void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001678mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001679{
1680 Buffer m;
1681 u_char *blob, *p;
1682 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001683 u_int32_t seqnr, packets;
1684 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001685
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001686 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687
1688 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001689 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001690 if (!compat20) {
1691 child_state.ssh1protoflags = buffer_get_int(&m);
1692 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001693 child_state.ssh1key = buffer_get_string(&m,
1694 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001695 child_state.ivout = buffer_get_string(&m,
1696 &child_state.ivoutlen);
1697 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1698 goto skip;
1699 } else {
1700 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001701 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001702 }
1703
1704 blob = buffer_get_string(&m, &bloblen);
1705 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1706 xfree(blob);
1707
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001708 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001709 blob = buffer_get_string(&m, &bloblen);
1710 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1711 xfree(blob);
1712
1713 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001714 seqnr = buffer_get_int(&m);
1715 blocks = buffer_get_int64(&m);
1716 packets = buffer_get_int(&m);
1717 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1718 seqnr = buffer_get_int(&m);
1719 blocks = buffer_get_int64(&m);
1720 packets = buffer_get_int(&m);
1721 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001722
1723 skip:
1724 /* Get the key context */
1725 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1726 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1727
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001728 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001729 /* Get compression state */
1730 p = buffer_get_string(&m, &plen);
1731 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001732 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001733 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1734 xfree(p);
1735
1736 p = buffer_get_string(&m, &plen);
1737 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001738 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001739 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1740 xfree(p);
1741
1742 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001743 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001744 child_state.input = buffer_get_string(&m, &child_state.ilen);
1745 child_state.output = buffer_get_string(&m, &child_state.olen);
1746
1747 buffer_free(&m);
1748}
1749
1750
1751/* Allocation functions for zlib */
1752void *
1753mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1754{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001755 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001756 void *address;
1757
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001758 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001759 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1760
1761 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001762
1763 return (address);
1764}
1765
1766void
1767mm_zfree(struct mm_master *mm, void *address)
1768{
1769 mm_free(mm, address);
1770}
1771
1772void
1773mm_init_compression(struct mm_master *mm)
1774{
1775 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1776 outgoing_stream.zfree = (free_func)mm_zfree;
1777 outgoing_stream.opaque = mm;
1778
1779 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1780 incoming_stream.zfree = (free_func)mm_zfree;
1781 incoming_stream.opaque = mm;
1782}
1783
1784/* XXX */
1785
1786#define FD_CLOSEONEXEC(x) do { \
1787 if (fcntl(x, F_SETFD, 1) == -1) \
1788 fatal("fcntl(%d, F_SETFD)", x); \
1789} while (0)
1790
1791static void
1792monitor_socketpair(int *pair)
1793{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001794#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001795 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001796 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001797#else
1798 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001799 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001800#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001801 FD_CLOSEONEXEC(pair[0]);
1802 FD_CLOSEONEXEC(pair[1]);
1803}
1804
1805#define MM_MEMSIZE 65536
1806
1807struct monitor *
1808monitor_init(void)
1809{
1810 struct monitor *mon;
1811 int pair[2];
1812
Damien Miller07d86be2006-03-26 14:19:21 +11001813 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001814
1815 monitor_socketpair(pair);
1816
1817 mon->m_recvfd = pair[0];
1818 mon->m_sendfd = pair[1];
1819
1820 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001821 if (options.compression) {
1822 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1823 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001824
Damien Miller2d6b8332002-06-21 15:59:49 +10001825 /* Compression needs to share state across borders */
1826 mm_init_compression(mon->m_zlib);
1827 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001828
1829 return mon;
1830}
1831
1832void
1833monitor_reinit(struct monitor *mon)
1834{
1835 int pair[2];
1836
1837 monitor_socketpair(pair);
1838
1839 mon->m_recvfd = pair[0];
1840 mon->m_sendfd = pair[1];
1841}
Darren Tucker0efd1552003-08-26 11:49:55 +10001842
1843#ifdef GSSAPI
1844int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001845mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001846{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001847 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001848 OM_uint32 major;
1849 u_int len;
1850
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001851 goid.elements = buffer_get_string(m, &len);
1852 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001853
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001854 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001855
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001856 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001857
1858 buffer_clear(m);
1859 buffer_put_int(m, major);
1860
Damien Miller6fd6def2005-11-05 15:07:05 +11001861 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001862
1863 /* Now we have a context, enable the step */
1864 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1865
1866 return (0);
1867}
1868
1869int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001870mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001871{
1872 gss_buffer_desc in;
1873 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001874 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001875 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001876 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001877
Darren Tucker600ad8d2003-08-26 12:10:48 +10001878 in.value = buffer_get_string(m, &len);
1879 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001880 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1881 xfree(in.value);
1882
1883 buffer_clear(m);
1884 buffer_put_int(m, major);
1885 buffer_put_string(m, out.value, out.length);
1886 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001887 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001888
1889 gss_release_buffer(&minor, &out);
1890
Damien Miller6fd6def2005-11-05 15:07:05 +11001891 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001892 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1893 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001894 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001895 }
1896 return (0);
1897}
1898
1899int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001900mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001901{
1902 gss_buffer_desc gssbuf, mic;
1903 OM_uint32 ret;
1904 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001905
Damien Miller0425d402003-11-17 22:18:21 +11001906 gssbuf.value = buffer_get_string(m, &len);
1907 gssbuf.length = len;
1908 mic.value = buffer_get_string(m, &len);
1909 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001910
Damien Miller0425d402003-11-17 22:18:21 +11001911 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001912
Damien Miller0425d402003-11-17 22:18:21 +11001913 xfree(gssbuf.value);
1914 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001915
Damien Miller0425d402003-11-17 22:18:21 +11001916 buffer_clear(m);
1917 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001918
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001919 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001920
Damien Miller0425d402003-11-17 22:18:21 +11001921 if (!GSS_ERROR(ret))
1922 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001923
Damien Miller0425d402003-11-17 22:18:21 +11001924 return (0);
1925}
1926
1927int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001928mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001929{
1930 int authenticated;
1931
1932 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1933
1934 buffer_clear(m);
1935 buffer_put_int(m, authenticated);
1936
1937 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001938 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001939
Damien Miller6fd6def2005-11-05 15:07:05 +11001940 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001941
1942 /* Monitor loop will terminate if authenticated */
1943 return (authenticated);
1944}
1945#endif /* GSSAPI */