blob: 3799d8862ecbb59ff901dff390f3193be6d68ae7 [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: monitor.c,v 1.82 2006/07/22 20:48:23 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
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110036#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110037#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100039#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110040#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000042
43#ifdef SKEY
44#include <skey.h>
45#endif
46
Damien Miller03e20032006-03-15 11:16:59 +110047#include <openssl/dh.h>
48
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000049#include "ssh.h"
50#include "auth.h"
51#include "kex.h"
52#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000053#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
54#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000055#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000056#define TARGET_OS_MAC 1
57#else
58#include "zlib.h"
59#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060#include "packet.h"
61#include "auth-options.h"
62#include "sshpty.h"
63#include "channels.h"
64#include "session.h"
65#include "sshlogin.h"
66#include "canohost.h"
67#include "log.h"
68#include "servconf.h"
69#include "monitor.h"
70#include "monitor_mm.h"
71#include "monitor_wrap.h"
72#include "monitor_fdpass.h"
73#include "xmalloc.h"
74#include "misc.h"
75#include "buffer.h"
76#include "bufaux.h"
77#include "compat.h"
78#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079
Darren Tucker0efd1552003-08-26 11:49:55 +100080#ifdef GSSAPI
81#include "ssh-gss.h"
82static Gssctxt *gsscontext = NULL;
83#endif
84
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000085/* Imports */
86extern ServerOptions options;
87extern u_int utmp_len;
88extern Newkeys *current_keys[];
89extern z_stream incoming_stream;
90extern z_stream outgoing_stream;
91extern u_char session_id[];
92extern Buffer input, output;
93extern Buffer auth_debug;
94extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +100095extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000096
97/* State exported from the child */
98
99struct {
100 z_stream incoming;
101 z_stream outgoing;
102 u_char *keyin;
103 u_int keyinlen;
104 u_char *keyout;
105 u_int keyoutlen;
106 u_char *ivin;
107 u_int ivinlen;
108 u_char *ivout;
109 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000110 u_char *ssh1key;
111 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112 int ssh1cipher;
113 int ssh1protoflags;
114 u_char *input;
115 u_int ilen;
116 u_char *output;
117 u_int olen;
118} child_state;
119
Damien Miller469954d2003-06-18 20:25:33 +1000120/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121
122int mm_answer_moduli(int, Buffer *);
123int mm_answer_sign(int, Buffer *);
124int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000125int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000126int mm_answer_authserv(int, Buffer *);
127int mm_answer_authpassword(int, Buffer *);
128int mm_answer_bsdauthquery(int, Buffer *);
129int mm_answer_bsdauthrespond(int, Buffer *);
130int mm_answer_skeyquery(int, Buffer *);
131int mm_answer_skeyrespond(int, Buffer *);
132int mm_answer_keyallowed(int, Buffer *);
133int mm_answer_keyverify(int, Buffer *);
134int mm_answer_pty(int, Buffer *);
135int mm_answer_pty_cleanup(int, Buffer *);
136int mm_answer_term(int, Buffer *);
137int mm_answer_rsa_keyallowed(int, Buffer *);
138int mm_answer_rsa_challenge(int, Buffer *);
139int mm_answer_rsa_response(int, Buffer *);
140int mm_answer_sesskey(int, Buffer *);
141int mm_answer_sessid(int, Buffer *);
142
Damien Miller79418552002-04-23 20:28:48 +1000143#ifdef USE_PAM
144int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000145int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000146int mm_answer_pam_init_ctx(int, Buffer *);
147int mm_answer_pam_query(int, Buffer *);
148int mm_answer_pam_respond(int, Buffer *);
149int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000150#endif
151
Darren Tucker0efd1552003-08-26 11:49:55 +1000152#ifdef GSSAPI
153int mm_answer_gss_setup_ctx(int, Buffer *);
154int mm_answer_gss_accept_ctx(int, Buffer *);
155int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100156int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000157#endif
Damien Miller25162f22002-09-12 09:47:29 +1000158
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100159#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100160int mm_answer_audit_event(int, Buffer *);
161int mm_answer_audit_command(int, Buffer *);
162#endif
163
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000164static Authctxt *authctxt;
165static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
166
167/* local state for key verify */
168static u_char *key_blob = NULL;
169static u_int key_bloblen = 0;
170static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000171static char *hostbased_cuser = NULL;
172static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000174static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000175static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000176static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000177
178struct mon_table {
179 enum monitor_reqtype type;
180 int flags;
181 int (*f)(int, Buffer *);
182};
183
184#define MON_ISAUTH 0x0004 /* Required for Authentication */
185#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
186#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100187#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000188
189#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
190
191#define MON_PERMIT 0x1000 /* Request is permitted */
192
193struct mon_table mon_dispatch_proto20[] = {
194 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
195 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
196 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
197 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000198 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000200#ifdef USE_PAM
201 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000202 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000203 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
204 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
205 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
206 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000207#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100208#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100209 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100210#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211#ifdef BSD_AUTH
212 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100213 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000214#endif
215#ifdef SKEY
216 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
217 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
218#endif
219 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
220 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000221#ifdef GSSAPI
222 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
223 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
224 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100225 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000226#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227 {0, 0, NULL}
228};
229
230struct mon_table mon_dispatch_postauth20[] = {
231 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
232 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
233 {MONITOR_REQ_PTY, 0, mm_answer_pty},
234 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
235 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100236#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100237 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
238 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
239#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240 {0, 0, NULL}
241};
242
243struct mon_table mon_dispatch_proto15[] = {
244 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
245 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
246 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
247 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100248 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
249 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000250 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
251 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
252#ifdef BSD_AUTH
253 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100254 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000255#endif
256#ifdef SKEY
257 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
258 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
259#endif
Damien Millera33501b2002-05-08 12:24:42 +1000260#ifdef USE_PAM
261 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000262 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000263 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
264 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
265 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
266 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000267#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100268#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100269 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100270#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000271 {0, 0, NULL}
272};
273
274struct mon_table mon_dispatch_postauth15[] = {
275 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
276 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
277 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100278#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100279 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100280 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100281#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000282 {0, 0, NULL}
283};
284
285struct mon_table *mon_dispatch;
286
287/* Specifies if a certain message is allowed at the moment */
288
289static void
290monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
291{
292 while (ent->f != NULL) {
293 if (ent->type == type) {
294 ent->flags &= ~MON_PERMIT;
295 ent->flags |= permit ? MON_PERMIT : 0;
296 return;
297 }
298 ent++;
299 }
300}
301
302static void
303monitor_permit_authentications(int permit)
304{
305 struct mon_table *ent = mon_dispatch;
306
307 while (ent->f != NULL) {
308 if (ent->flags & MON_AUTH) {
309 ent->flags &= ~MON_PERMIT;
310 ent->flags |= permit ? MON_PERMIT : 0;
311 }
312 ent++;
313 }
314}
315
Darren Tucker3e33cec2003-10-02 16:12:36 +1000316void
317monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000318{
319 struct mon_table *ent;
320 int authenticated = 0;
321
322 debug3("preauth child monitor started");
323
Darren Tucker3e33cec2003-10-02 16:12:36 +1000324 authctxt = _authctxt;
325 memset(authctxt, 0, sizeof(*authctxt));
326
Darren Tuckerde0de392005-03-31 23:52:04 +1000327 authctxt->loginmsg = &loginmsg;
328
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329 if (compat20) {
330 mon_dispatch = mon_dispatch_proto20;
331
332 /* Permit requests for moduli and signatures */
333 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
334 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
335 } else {
336 mon_dispatch = mon_dispatch_proto15;
337
338 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
339 }
340
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000341 /* The first few requests do not require asynchronous access */
342 while (!authenticated) {
Damien Miller7a8f5b32006-03-31 23:14:23 +1100343 auth_method = "unknown";
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000344 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000345 if (authenticated) {
346 if (!(ent->flags & MON_AUTHDECIDE))
347 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000348 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349 if (authctxt->pw->pw_uid == 0 &&
350 !auth_root_allowed(auth_method))
351 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000352#ifdef USE_PAM
353 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100354 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000355 Buffer m;
356
357 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100358 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000359 MONITOR_REQ_PAM_ACCOUNT, &m);
360 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
361 buffer_free(&m);
362 }
363#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364 }
365
Damien Miller7a8f5b32006-03-31 23:14:23 +1100366 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367 auth_log(authctxt, authenticated, auth_method,
368 compat20 ? " ssh2" : "");
369 if (!authenticated)
370 authctxt->failures++;
371 }
372 }
373
374 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000375 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100376 if (strcmp(auth_method, "unknown") == 0)
377 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000378
379 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000380 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000381
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000382 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000383}
384
Damien Millerbe64d432003-05-14 19:31:12 +1000385static void
386monitor_set_child_handler(pid_t pid)
387{
388 monitor_child_pid = pid;
389}
390
391static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000392monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000393{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000394 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000395}
396
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000398monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399{
Damien Millerbe64d432003-05-14 19:31:12 +1000400 monitor_set_child_handler(pmonitor->m_pid);
401 signal(SIGHUP, &monitor_child_handler);
402 signal(SIGTERM, &monitor_child_handler);
403
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404 if (compat20) {
405 mon_dispatch = mon_dispatch_postauth20;
406
407 /* Permit requests for moduli and signatures */
408 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
409 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
410 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000411 } else {
412 mon_dispatch = mon_dispatch_postauth15;
413 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
414 }
415 if (!no_pty_flag) {
416 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
417 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
418 }
419
420 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000421 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000422}
423
424void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000425monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000426{
Damien Miller2d6b8332002-06-21 15:59:49 +1000427 if (options.compression) {
428 /* The member allocation is not visible, so sync it */
429 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
430 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000431}
432
433int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000434monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000435 struct mon_table **pent)
436{
437 Buffer m;
438 int ret;
439 u_char type;
440
441 buffer_init(&m);
442
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000443 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444 type = buffer_get_char(&m);
445
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000446 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000447
448 while (ent->f != NULL) {
449 if (ent->type == type)
450 break;
451 ent++;
452 }
453
454 if (ent->f != NULL) {
455 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000456 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000457 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000458 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000459 buffer_free(&m);
460
461 /* The child may use this request only once, disable it */
462 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000463 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464 type);
465 ent->flags &= ~MON_PERMIT;
466 }
467
468 if (pent != NULL)
469 *pent = ent;
470
471 return ret;
472 }
473
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000474 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000475
476 /* NOTREACHED */
477 return (-1);
478}
479
480/* allowed key state */
481static int
482monitor_allowed_key(u_char *blob, u_int bloblen)
483{
484 /* make sure key is allowed */
485 if (key_blob == NULL || key_bloblen != bloblen ||
486 memcmp(key_blob, blob, key_bloblen))
487 return (0);
488 return (1);
489}
490
491static void
492monitor_reset_key_state(void)
493{
494 /* reset state */
495 if (key_blob != NULL)
496 xfree(key_blob);
497 if (hostbased_cuser != NULL)
498 xfree(hostbased_cuser);
499 if (hostbased_chost != NULL)
500 xfree(hostbased_chost);
501 key_blob = NULL;
502 key_bloblen = 0;
503 key_blobtype = MM_NOKEY;
504 hostbased_cuser = NULL;
505 hostbased_chost = NULL;
506}
507
508int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000509mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510{
511 DH *dh;
512 int min, want, max;
513
514 min = buffer_get_int(m);
515 want = buffer_get_int(m);
516 max = buffer_get_int(m);
517
518 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000519 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000520 /* We need to check here, too, in case the child got corrupted */
521 if (max < min || want < min || max < want)
522 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000523 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000524
525 buffer_clear(m);
526
527 dh = choose_dh(min, want, max);
528 if (dh == NULL) {
529 buffer_put_char(m, 0);
530 return (0);
531 } else {
532 /* Send first bignum */
533 buffer_put_char(m, 1);
534 buffer_put_bignum2(m, dh->p);
535 buffer_put_bignum2(m, dh->g);
536
537 DH_free(dh);
538 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000539 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000540 return (0);
541}
542
543int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000544mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000545{
546 Key *key;
547 u_char *p;
548 u_char *signature;
549 u_int siglen, datlen;
550 int keyid;
551
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000552 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000553
554 keyid = buffer_get_int(m);
555 p = buffer_get_string(m, &datlen);
556
Damien Millera63128d2006-03-15 12:08:28 +1100557 /*
Damien Millerc91e5562006-03-26 13:58:55 +1100558 * Supported KEX types will only return SHA1 (20 byte) or
Damien Millera63128d2006-03-15 12:08:28 +1100559 * SHA256 (32 byte) hashes
560 */
561 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000562 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000563
Ben Lindstromf67e0772002-06-06 20:58:19 +0000564 /* save session id, it will be passed on the first call */
565 if (session_id2_len == 0) {
566 session_id2_len = datlen;
567 session_id2 = xmalloc(session_id2_len);
568 memcpy(session_id2, p, session_id2_len);
569 }
570
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000571 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000572 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000574 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575
Ben Lindstromd5502182002-06-27 00:12:57 +0000576 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577
578 buffer_clear(m);
579 buffer_put_string(m, signature, siglen);
580
581 xfree(p);
582 xfree(signature);
583
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000584 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585
586 /* Turn on permissions for getpwnam */
587 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
588
589 return (0);
590}
591
592/* Retrieves the password entry and also checks if the user is permitted */
593
594int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000595mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000597 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598 struct passwd *pwent;
599 int allowed = 0;
600
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000601 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602
603 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000604 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605
Darren Tuckerb09b6772004-06-22 15:06:46 +1000606 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607
Darren Tuckerb09b6772004-06-22 15:06:46 +1000608 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609
Darren Tuckerb09b6772004-06-22 15:06:46 +1000610 authctxt->user = xstrdup(username);
611 setproctitle("%s [priv]", pwent ? username : "unknown");
612 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000613
614 buffer_clear(m);
615
616 if (pwent == NULL) {
617 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100618 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000619 goto out;
620 }
621
622 allowed = 1;
623 authctxt->pw = pwent;
624 authctxt->valid = 1;
625
626 buffer_put_char(m, 1);
627 buffer_put_string(m, pwent, sizeof(struct passwd));
628 buffer_put_cstring(m, pwent->pw_name);
629 buffer_put_cstring(m, "*");
630 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000631#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000633#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634 buffer_put_cstring(m, pwent->pw_dir);
635 buffer_put_cstring(m, pwent->pw_shell);
636
637 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000638 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000639 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000640
641 /* For SSHv1 allow authentication now */
642 if (!compat20)
643 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000644 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645 /* Allow service/style information on the auth context */
646 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000647 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
648 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649
Damien Millera33501b2002-05-08 12:24:42 +1000650#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000651 if (options.use_pam)
652 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000653#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100654#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100655 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100656#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657
658 return (0);
659}
660
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000661int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000662{
663 char *banner;
664
665 buffer_clear(m);
666 banner = auth2_read_banner();
667 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000668 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000669
670 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000671 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000672
673 return (0);
674}
675
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000676int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000677mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000678{
679 monitor_permit_authentications(1);
680
681 authctxt->service = buffer_get_string(m, NULL);
682 authctxt->style = buffer_get_string(m, NULL);
683 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000684 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685
686 if (strlen(authctxt->style) == 0) {
687 xfree(authctxt->style);
688 authctxt->style = NULL;
689 }
690
691 return (0);
692}
693
694int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000695mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696{
697 static int call_count;
698 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000699 int authenticated;
700 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000701
702 passwd = buffer_get_string(m, &plen);
703 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000704 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000705 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706 memset(passwd, 0, strlen(passwd));
707 xfree(passwd);
708
709 buffer_clear(m);
710 buffer_put_int(m, authenticated);
711
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000712 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000713 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000714
715 call_count++;
716 if (plen == 0 && call_count == 1)
717 auth_method = "none";
718 else
719 auth_method = "password";
720
721 /* Causes monitor loop to terminate if authenticated */
722 return (authenticated);
723}
724
725#ifdef BSD_AUTH
726int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000727mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728{
729 char *name, *infotxt;
730 u_int numprompts;
731 u_int *echo_on;
732 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100733 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734
Damien Millerb7df3af2003-02-24 11:55:46 +1100735 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
736 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737
738 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100739 buffer_put_int(m, success);
740 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741 buffer_put_cstring(m, prompts[0]);
742
Damien Millerb7df3af2003-02-24 11:55:46 +1100743 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000744 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745
Damien Millerb7df3af2003-02-24 11:55:46 +1100746 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747 xfree(name);
748 xfree(infotxt);
749 xfree(prompts);
750 xfree(echo_on);
751 }
752
753 return (0);
754}
755
756int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000757mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758{
759 char *response;
760 int authok;
761
762 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000763 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000764
765 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000766 authok = options.challenge_response_authentication &&
767 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000769 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770 xfree(response);
771
772 buffer_clear(m);
773 buffer_put_int(m, authok);
774
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000775 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000776 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000777
778 auth_method = "bsdauth";
779
780 return (authok != 0);
781}
782#endif
783
784#ifdef SKEY
785int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000786mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787{
788 struct skey skey;
789 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100790 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000792 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
793 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000794
795 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100796 buffer_put_int(m, success);
797 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000798 buffer_put_cstring(m, challenge);
799
Damien Millerb7df3af2003-02-24 11:55:46 +1100800 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000801 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000802
803 return (0);
804}
805
806int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000807mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000808{
809 char *response;
810 int authok;
811
812 response = buffer_get_string(m, NULL);
813
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000814 authok = (options.challenge_response_authentication &&
815 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000816 skey_haskey(authctxt->pw->pw_name) == 0 &&
817 skey_passcheck(authctxt->pw->pw_name, response) != -1);
818
819 xfree(response);
820
821 buffer_clear(m);
822 buffer_put_int(m, authok);
823
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000824 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000825 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000826
827 auth_method = "skey";
828
829 return (authok != 0);
830}
831#endif
832
Damien Miller79418552002-04-23 20:28:48 +1000833#ifdef USE_PAM
834int
Darren Tucker7eda5922004-06-22 13:22:50 +1000835mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000836{
Damien Miller4e448a32003-05-14 15:11:48 +1000837 if (!options.use_pam)
838 fatal("UsePAM not set, but ended up in %s anyway", __func__);
839
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100840 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000841
Damien Miller1f499fd2003-08-25 13:08:49 +1000842 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
843
Damien Miller79418552002-04-23 20:28:48 +1000844 return (0);
845}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000846
Damien Miller1f499fd2003-08-25 13:08:49 +1000847int
Darren Tucker7eda5922004-06-22 13:22:50 +1000848mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000849{
850 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100851
Damien Miller1f499fd2003-08-25 13:08:49 +1000852 if (!options.use_pam)
853 fatal("UsePAM not set, but ended up in %s anyway", __func__);
854
855 ret = do_pam_account();
856
857 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000858 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000859
Darren Tucker7eda5922004-06-22 13:22:50 +1000860 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000861
862 return (ret);
863}
864
Damien Miller4f9f42a2003-05-10 19:28:02 +1000865static void *sshpam_ctxt, *sshpam_authok;
866extern KbdintDevice sshpam_device;
867
868int
Darren Tucker7eda5922004-06-22 13:22:50 +1000869mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000870{
871
872 debug3("%s", __func__);
873 authctxt->user = buffer_get_string(m, NULL);
874 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
875 sshpam_authok = NULL;
876 buffer_clear(m);
877 if (sshpam_ctxt != NULL) {
878 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
879 buffer_put_int(m, 1);
880 } else {
881 buffer_put_int(m, 0);
882 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000883 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000884 return (0);
885}
886
887int
Darren Tucker7eda5922004-06-22 13:22:50 +1000888mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000889{
890 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000891 u_int i, num, *echo_on;
892 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000893
894 debug3("%s", __func__);
895 sshpam_authok = NULL;
896 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
897 if (ret == 0 && num == 0)
898 sshpam_authok = sshpam_ctxt;
899 if (num > 1 || name == NULL || info == NULL)
900 ret = -1;
901 buffer_clear(m);
902 buffer_put_int(m, ret);
903 buffer_put_cstring(m, name);
904 xfree(name);
905 buffer_put_cstring(m, info);
906 xfree(info);
907 buffer_put_int(m, num);
908 for (i = 0; i < num; ++i) {
909 buffer_put_cstring(m, prompts[i]);
910 xfree(prompts[i]);
911 buffer_put_int(m, echo_on[i]);
912 }
913 if (prompts != NULL)
914 xfree(prompts);
915 if (echo_on != NULL)
916 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000917 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +1000918 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000919 return (0);
920}
921
922int
Darren Tucker7eda5922004-06-22 13:22:50 +1000923mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000924{
925 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000926 u_int i, num;
927 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000928
929 debug3("%s", __func__);
930 sshpam_authok = NULL;
931 num = buffer_get_int(m);
932 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +1000933 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000934 for (i = 0; i < num; ++i)
935 resp[i] = buffer_get_string(m, NULL);
936 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
937 for (i = 0; i < num; ++i)
938 xfree(resp[i]);
939 xfree(resp);
940 } else {
941 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
942 }
943 buffer_clear(m);
944 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000945 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000946 auth_method = "keyboard-interactive/pam";
947 if (ret == 0)
948 sshpam_authok = sshpam_ctxt;
949 return (0);
950}
951
952int
Darren Tucker7eda5922004-06-22 13:22:50 +1000953mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000954{
955
956 debug3("%s", __func__);
957 (sshpam_device.free_ctx)(sshpam_ctxt);
958 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000959 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000960 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000961 return (sshpam_authok == sshpam_ctxt);
962}
Damien Miller79418552002-04-23 20:28:48 +1000963#endif
964
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965static void
966mm_append_debug(Buffer *m)
967{
968 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000969 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000970 buffer_append(m, buffer_ptr(&auth_debug),
971 buffer_len(&auth_debug));
972 buffer_clear(&auth_debug);
973 }
974}
975
976int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000977mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978{
979 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000980 char *cuser, *chost;
981 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000982 u_int bloblen;
983 enum mm_keytype type = 0;
984 int allowed = 0;
985
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000986 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
988 type = buffer_get_int(m);
989 cuser = buffer_get_string(m, NULL);
990 chost = buffer_get_string(m, NULL);
991 blob = buffer_get_string(m, &bloblen);
992
993 key = key_from_blob(blob, bloblen);
994
995 if ((compat20 && type == MM_RSAHOSTKEY) ||
996 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000997 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000999 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001000
Damien Miller3e3b5142003-11-17 21:13:40 +11001001 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001002 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001004 allowed = options.pubkey_authentication &&
1005 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001006 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007 break;
1008 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001009 allowed = options.hostbased_authentication &&
1010 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001011 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001012 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013 break;
1014 case MM_RSAHOSTKEY:
1015 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001016 allowed = options.rhosts_rsa_authentication &&
1017 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001019 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001020 break;
1021 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001022 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023 break;
1024 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025 }
Damien Miller00111382003-03-10 11:21:17 +11001026 if (key != NULL)
1027 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001028
1029 /* clear temporarily storage (used by verify) */
1030 monitor_reset_key_state();
1031
1032 if (allowed) {
1033 /* Save temporarily for comparison in verify */
1034 key_blob = blob;
1035 key_bloblen = bloblen;
1036 key_blobtype = type;
1037 hostbased_cuser = cuser;
1038 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001039 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001040 /* Log failed attempt */
1041 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001042 xfree(blob);
1043 xfree(cuser);
1044 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001045 }
1046
1047 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001048 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001049
1050 buffer_clear(m);
1051 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001052 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001053
1054 mm_append_debug(m);
1055
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001056 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001057
1058 if (type == MM_RSAHOSTKEY)
1059 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1060
1061 return (0);
1062}
1063
1064static int
1065monitor_valid_userblob(u_char *data, u_int datalen)
1066{
1067 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001068 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001069 u_int len;
1070 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001071
1072 buffer_init(&b);
1073 buffer_append(&b, data, datalen);
1074
1075 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001076 p = buffer_ptr(&b);
1077 len = buffer_len(&b);
1078 if ((session_id2 == NULL) ||
1079 (len < session_id2_len) ||
1080 (memcmp(p, session_id2, session_id2_len) != 0))
1081 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001082 buffer_consume(&b, session_id2_len);
1083 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001084 p = buffer_get_string(&b, &len);
1085 if ((session_id2 == NULL) ||
1086 (len != session_id2_len) ||
1087 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001088 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001089 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001090 }
1091 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1092 fail++;
1093 p = buffer_get_string(&b, NULL);
1094 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001095 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001096 authctxt->user, p);
1097 fail++;
1098 }
1099 xfree(p);
1100 buffer_skip_string(&b);
1101 if (datafellows & SSH_BUG_PKAUTH) {
1102 if (!buffer_get_char(&b))
1103 fail++;
1104 } else {
1105 p = buffer_get_string(&b, NULL);
1106 if (strcmp("publickey", p) != 0)
1107 fail++;
1108 xfree(p);
1109 if (!buffer_get_char(&b))
1110 fail++;
1111 buffer_skip_string(&b);
1112 }
1113 buffer_skip_string(&b);
1114 if (buffer_len(&b) != 0)
1115 fail++;
1116 buffer_free(&b);
1117 return (fail == 0);
1118}
1119
1120static int
Damien Millera10f5612002-09-12 09:49:15 +10001121monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1122 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001123{
1124 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001125 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001126 u_int len;
1127 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001128
1129 buffer_init(&b);
1130 buffer_append(&b, data, datalen);
1131
Ben Lindstromf67e0772002-06-06 20:58:19 +00001132 p = buffer_get_string(&b, &len);
1133 if ((session_id2 == NULL) ||
1134 (len != session_id2_len) ||
1135 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001136 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001137 xfree(p);
1138
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001139 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1140 fail++;
1141 p = buffer_get_string(&b, NULL);
1142 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001143 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001144 authctxt->user, p);
1145 fail++;
1146 }
1147 xfree(p);
1148 buffer_skip_string(&b); /* service */
1149 p = buffer_get_string(&b, NULL);
1150 if (strcmp(p, "hostbased") != 0)
1151 fail++;
1152 xfree(p);
1153 buffer_skip_string(&b); /* pkalg */
1154 buffer_skip_string(&b); /* pkblob */
1155
1156 /* verify client host, strip trailing dot if necessary */
1157 p = buffer_get_string(&b, NULL);
1158 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1159 p[len - 1] = '\0';
1160 if (strcmp(p, chost) != 0)
1161 fail++;
1162 xfree(p);
1163
1164 /* verify client user */
1165 p = buffer_get_string(&b, NULL);
1166 if (strcmp(p, cuser) != 0)
1167 fail++;
1168 xfree(p);
1169
1170 if (buffer_len(&b) != 0)
1171 fail++;
1172 buffer_free(&b);
1173 return (fail == 0);
1174}
1175
1176int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001177mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001178{
1179 Key *key;
1180 u_char *signature, *data, *blob;
1181 u_int signaturelen, datalen, bloblen;
1182 int verified = 0;
1183 int valid_data = 0;
1184
1185 blob = buffer_get_string(m, &bloblen);
1186 signature = buffer_get_string(m, &signaturelen);
1187 data = buffer_get_string(m, &datalen);
1188
1189 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001190 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001191 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001192
1193 key = key_from_blob(blob, bloblen);
1194 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001195 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001196
1197 switch (key_blobtype) {
1198 case MM_USERKEY:
1199 valid_data = monitor_valid_userblob(data, datalen);
1200 break;
1201 case MM_HOSTKEY:
1202 valid_data = monitor_valid_hostbasedblob(data, datalen,
1203 hostbased_cuser, hostbased_chost);
1204 break;
1205 default:
1206 valid_data = 0;
1207 break;
1208 }
1209 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001210 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001211
1212 verified = key_verify(key, signature, signaturelen, data, datalen);
1213 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001214 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001215
1216 key_free(key);
1217 xfree(blob);
1218 xfree(signature);
1219 xfree(data);
1220
Ben Lindstrome1c09122002-06-23 00:38:24 +00001221 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1222
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001223 monitor_reset_key_state();
1224
1225 buffer_clear(m);
1226 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001227 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229 return (verified);
1230}
1231
1232static void
1233mm_record_login(Session *s, struct passwd *pw)
1234{
1235 socklen_t fromlen;
1236 struct sockaddr_storage from;
1237
1238 /*
1239 * Get IP address of client. If the connection is not a socket, let
1240 * the address be 0.0.0.0.
1241 */
1242 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001243 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001246 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001248 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249 }
1250 }
1251 /* Record that there was a login on that tty from the remote host. */
1252 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001253 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001254 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255}
1256
1257static void
1258mm_session_close(Session *s)
1259{
Damien Miller04bd8b02003-05-25 14:38:33 +10001260 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001262 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001263 session_pty_cleanup2(s);
1264 }
1265 s->used = 0;
1266}
1267
1268int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001269mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001270{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001271 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001272 Session *s;
1273 int res, fd0;
1274
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001275 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001276
1277 buffer_clear(m);
1278 s = session_new();
1279 if (s == NULL)
1280 goto error;
1281 s->authctxt = authctxt;
1282 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001283 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1285 if (res == 0)
1286 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287 pty_setowner(authctxt->pw, s->tty);
1288
1289 buffer_put_int(m, 1);
1290 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001291
1292 /* We need to trick ttyslot */
1293 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001294 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295
1296 mm_record_login(s, authctxt->pw);
1297
1298 /* Now we can close the file descriptor again */
1299 close(0);
1300
Darren Tucker09991742004-07-17 17:05:14 +10001301 /* send messages generated by record_login */
1302 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1303 buffer_clear(&loginmsg);
1304
1305 mm_request_send(sock, MONITOR_ANS_PTY, m);
1306
1307 mm_send_fd(sock, s->ptyfd);
1308 mm_send_fd(sock, s->ttyfd);
1309
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001310 /* make sure nothing uses fd 0 */
1311 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001312 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001313 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001314 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001315
1316 /* slave is not needed */
1317 close(s->ttyfd);
1318 s->ttyfd = s->ptyfd;
1319 /* no need to dup() because nobody closes ptyfd */
1320 s->ptymaster = s->ptyfd;
1321
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001322 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001323
1324 return (0);
1325
1326 error:
1327 if (s != NULL)
1328 mm_session_close(s);
1329 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001330 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001331 return (0);
1332}
1333
1334int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001335mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001336{
1337 Session *s;
1338 char *tty;
1339
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001340 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341
1342 tty = buffer_get_string(m, NULL);
1343 if ((s = session_by_tty(tty)) != NULL)
1344 mm_session_close(s);
1345 buffer_clear(m);
1346 xfree(tty);
1347 return (0);
1348}
1349
1350int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001351mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001352{
1353 BIGNUM *p;
1354 int rsafail;
1355
1356 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001357 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001358
1359 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001360 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001361
1362 buffer_get_bignum2(m, p);
1363
1364 rsafail = ssh1_session_key(p);
1365
1366 buffer_clear(m);
1367 buffer_put_int(m, rsafail);
1368 buffer_put_bignum2(m, p);
1369
1370 BN_clear_free(p);
1371
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001372 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001373
1374 /* Turn on permissions for sessid passing */
1375 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1376
1377 return (0);
1378}
1379
1380int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001381mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382{
1383 int i;
1384
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001385 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386
1387 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001388 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001389 for (i = 0; i < 16; i++)
1390 session_id[i] = buffer_get_char(m);
1391
1392 /* Turn on permissions for getpwnam */
1393 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1394
1395 return (0);
1396}
1397
1398int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001399mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400{
1401 BIGNUM *client_n;
1402 Key *key = NULL;
1403 u_char *blob = NULL;
1404 u_int blen = 0;
1405 int allowed = 0;
1406
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001407 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408
Damien Miller7a8f5b32006-03-31 23:14:23 +11001409 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001410 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001411 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001412 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001413 buffer_get_bignum2(m, client_n);
1414 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1415 BN_clear_free(client_n);
1416 }
1417 buffer_clear(m);
1418 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001419 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420
1421 /* clear temporarily storage (used by generate challenge) */
1422 monitor_reset_key_state();
1423
1424 if (allowed && key != NULL) {
1425 key->type = KEY_RSA; /* cheat for key_to_blob */
1426 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001427 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428 buffer_put_string(m, blob, blen);
1429
1430 /* Save temporarily for comparison in verify */
1431 key_blob = blob;
1432 key_bloblen = blen;
1433 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434 }
Damien Miller00111382003-03-10 11:21:17 +11001435 if (key != NULL)
1436 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001437
1438 mm_append_debug(m);
1439
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001440 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001441
1442 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1443 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1444 return (0);
1445}
1446
1447int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001448mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001449{
1450 Key *key = NULL;
1451 u_char *blob;
1452 u_int blen;
1453
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001454 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455
1456 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001457 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001458 blob = buffer_get_string(m, &blen);
1459 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001460 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001462 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001464 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465
1466 if (ssh1_challenge)
1467 BN_clear_free(ssh1_challenge);
1468 ssh1_challenge = auth_rsa_generate_challenge(key);
1469
1470 buffer_clear(m);
1471 buffer_put_bignum2(m, ssh1_challenge);
1472
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001473 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001474 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001475
1476 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001477
1478 xfree(blob);
1479 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001480 return (0);
1481}
1482
1483int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001484mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001485{
1486 Key *key = NULL;
1487 u_char *blob, *response;
1488 u_int blen, len;
1489 int success;
1490
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001491 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492
1493 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001494 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001496 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001497
1498 blob = buffer_get_string(m, &blen);
1499 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001500 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001501 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001502 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001504 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505 response = buffer_get_string(m, &len);
1506 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001507 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1509
Damien Miller00111382003-03-10 11:21:17 +11001510 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511 key_free(key);
1512 xfree(response);
1513
1514 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1515
1516 /* reset state */
1517 BN_clear_free(ssh1_challenge);
1518 ssh1_challenge = NULL;
1519 monitor_reset_key_state();
1520
1521 buffer_clear(m);
1522 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001523 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524
1525 return (success);
1526}
1527
1528int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001529mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001531 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532 int res, status;
1533
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001534 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001535
1536 /* The child is terminating */
1537 session_destroy_all(&mm_session_close);
1538
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001539 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001540 if (errno != EINTR)
1541 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542
1543 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1544
1545 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001546 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547}
1548
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001549#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001550/* Report that an audit event occurred */
1551int
1552mm_answer_audit_event(int socket, Buffer *m)
1553{
1554 ssh_audit_event_t event;
1555
1556 debug3("%s entering", __func__);
1557
1558 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001559 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001560 case SSH_AUTH_FAIL_PUBKEY:
1561 case SSH_AUTH_FAIL_HOSTBASED:
1562 case SSH_AUTH_FAIL_GSSAPI:
1563 case SSH_LOGIN_EXCEED_MAXTRIES:
1564 case SSH_LOGIN_ROOT_DENIED:
1565 case SSH_CONNECTION_CLOSE:
1566 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001567 audit_event(event);
1568 break;
1569 default:
1570 fatal("Audit event type %d not permitted", event);
1571 }
1572
1573 return (0);
1574}
1575
1576int
1577mm_answer_audit_command(int socket, Buffer *m)
1578{
1579 u_int len;
1580 char *cmd;
1581
1582 debug3("%s entering", __func__);
1583 cmd = buffer_get_string(m, &len);
1584 /* sanity check command, if so how? */
1585 audit_run_command(cmd);
1586 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001587 return (0);
1588}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001589#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001590
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001591void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001592monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001593{
1594 if (compat20) {
1595 set_newkeys(MODE_IN);
1596 set_newkeys(MODE_OUT);
1597 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001598 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001599 packet_set_encryption_key(child_state.ssh1key,
1600 child_state.ssh1keylen, child_state.ssh1cipher);
1601 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001602 }
1603
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001604 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001605 packet_set_keycontext(MODE_OUT, child_state.keyout);
1606 xfree(child_state.keyout);
1607 packet_set_keycontext(MODE_IN, child_state.keyin);
1608 xfree(child_state.keyin);
1609
1610 if (!compat20) {
1611 packet_set_iv(MODE_OUT, child_state.ivout);
1612 xfree(child_state.ivout);
1613 packet_set_iv(MODE_IN, child_state.ivin);
1614 xfree(child_state.ivin);
1615 }
1616
1617 memcpy(&incoming_stream, &child_state.incoming,
1618 sizeof(incoming_stream));
1619 memcpy(&outgoing_stream, &child_state.outgoing,
1620 sizeof(outgoing_stream));
1621
1622 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001623 if (options.compression)
1624 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625
1626 /* Network I/O buffers */
1627 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1628 buffer_clear(&input);
1629 buffer_append(&input, child_state.input, child_state.ilen);
1630 memset(child_state.input, 0, child_state.ilen);
1631 xfree(child_state.input);
1632
1633 buffer_clear(&output);
1634 buffer_append(&output, child_state.output, child_state.olen);
1635 memset(child_state.output, 0, child_state.olen);
1636 xfree(child_state.output);
1637}
1638
1639static Kex *
1640mm_get_kex(Buffer *m)
1641{
1642 Kex *kex;
1643 void *blob;
1644 u_int bloblen;
1645
Damien Miller07d86be2006-03-26 14:19:21 +11001646 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001647 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001648 if ((session_id2 == NULL) ||
1649 (kex->session_id_len != session_id2_len) ||
1650 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1651 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001652 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001653 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001654 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001655 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001656 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001657 kex->server = 1;
1658 kex->hostkey_type = buffer_get_int(m);
1659 kex->kex_type = buffer_get_int(m);
1660 blob = buffer_get_string(m, &bloblen);
1661 buffer_init(&kex->my);
1662 buffer_append(&kex->my, blob, bloblen);
1663 xfree(blob);
1664 blob = buffer_get_string(m, &bloblen);
1665 buffer_init(&kex->peer);
1666 buffer_append(&kex->peer, blob, bloblen);
1667 xfree(blob);
1668 kex->done = 1;
1669 kex->flags = buffer_get_int(m);
1670 kex->client_version_string = buffer_get_string(m, NULL);
1671 kex->server_version_string = buffer_get_string(m, NULL);
1672 kex->load_host_key=&get_hostkey_by_type;
1673 kex->host_key_index=&get_hostkey_index;
1674
1675 return (kex);
1676}
1677
1678/* This function requries careful sanity checking */
1679
1680void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001681mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001682{
1683 Buffer m;
1684 u_char *blob, *p;
1685 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001686 u_int32_t seqnr, packets;
1687 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001688
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001689 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001690
1691 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001692 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001693 if (!compat20) {
1694 child_state.ssh1protoflags = buffer_get_int(&m);
1695 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001696 child_state.ssh1key = buffer_get_string(&m,
1697 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001698 child_state.ivout = buffer_get_string(&m,
1699 &child_state.ivoutlen);
1700 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1701 goto skip;
1702 } else {
1703 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001704 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705 }
1706
1707 blob = buffer_get_string(&m, &bloblen);
1708 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1709 xfree(blob);
1710
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001711 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712 blob = buffer_get_string(&m, &bloblen);
1713 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1714 xfree(blob);
1715
1716 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001717 seqnr = buffer_get_int(&m);
1718 blocks = buffer_get_int64(&m);
1719 packets = buffer_get_int(&m);
1720 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1721 seqnr = buffer_get_int(&m);
1722 blocks = buffer_get_int64(&m);
1723 packets = buffer_get_int(&m);
1724 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001725
1726 skip:
1727 /* Get the key context */
1728 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1729 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1730
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001731 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001732 /* Get compression state */
1733 p = buffer_get_string(&m, &plen);
1734 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001735 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001736 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1737 xfree(p);
1738
1739 p = buffer_get_string(&m, &plen);
1740 if (plen != sizeof(child_state.incoming))
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.incoming, p, sizeof(child_state.incoming));
1743 xfree(p);
1744
1745 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001746 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001747 child_state.input = buffer_get_string(&m, &child_state.ilen);
1748 child_state.output = buffer_get_string(&m, &child_state.olen);
1749
1750 buffer_free(&m);
1751}
1752
1753
1754/* Allocation functions for zlib */
1755void *
1756mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1757{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001758 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001759 void *address;
1760
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001761 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001762 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1763
1764 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001765
1766 return (address);
1767}
1768
1769void
1770mm_zfree(struct mm_master *mm, void *address)
1771{
1772 mm_free(mm, address);
1773}
1774
1775void
1776mm_init_compression(struct mm_master *mm)
1777{
1778 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1779 outgoing_stream.zfree = (free_func)mm_zfree;
1780 outgoing_stream.opaque = mm;
1781
1782 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1783 incoming_stream.zfree = (free_func)mm_zfree;
1784 incoming_stream.opaque = mm;
1785}
1786
1787/* XXX */
1788
1789#define FD_CLOSEONEXEC(x) do { \
1790 if (fcntl(x, F_SETFD, 1) == -1) \
1791 fatal("fcntl(%d, F_SETFD)", x); \
1792} while (0)
1793
1794static void
1795monitor_socketpair(int *pair)
1796{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001797#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001798 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001799 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001800#else
1801 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001802 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001803#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001804 FD_CLOSEONEXEC(pair[0]);
1805 FD_CLOSEONEXEC(pair[1]);
1806}
1807
1808#define MM_MEMSIZE 65536
1809
1810struct monitor *
1811monitor_init(void)
1812{
1813 struct monitor *mon;
1814 int pair[2];
1815
Damien Miller07d86be2006-03-26 14:19:21 +11001816 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001817
1818 monitor_socketpair(pair);
1819
1820 mon->m_recvfd = pair[0];
1821 mon->m_sendfd = pair[1];
1822
1823 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001824 if (options.compression) {
1825 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1826 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001827
Damien Miller2d6b8332002-06-21 15:59:49 +10001828 /* Compression needs to share state across borders */
1829 mm_init_compression(mon->m_zlib);
1830 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001831
1832 return mon;
1833}
1834
1835void
1836monitor_reinit(struct monitor *mon)
1837{
1838 int pair[2];
1839
1840 monitor_socketpair(pair);
1841
1842 mon->m_recvfd = pair[0];
1843 mon->m_sendfd = pair[1];
1844}
Darren Tucker0efd1552003-08-26 11:49:55 +10001845
1846#ifdef GSSAPI
1847int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001848mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001849{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001850 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001851 OM_uint32 major;
1852 u_int len;
1853
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001854 goid.elements = buffer_get_string(m, &len);
1855 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001856
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001857 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001858
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001859 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001860
1861 buffer_clear(m);
1862 buffer_put_int(m, major);
1863
Damien Miller6fd6def2005-11-05 15:07:05 +11001864 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001865
1866 /* Now we have a context, enable the step */
1867 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1868
1869 return (0);
1870}
1871
1872int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001873mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001874{
1875 gss_buffer_desc in;
1876 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001877 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001878 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001879 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001880
Darren Tucker600ad8d2003-08-26 12:10:48 +10001881 in.value = buffer_get_string(m, &len);
1882 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001883 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1884 xfree(in.value);
1885
1886 buffer_clear(m);
1887 buffer_put_int(m, major);
1888 buffer_put_string(m, out.value, out.length);
1889 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001890 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001891
1892 gss_release_buffer(&minor, &out);
1893
Damien Miller6fd6def2005-11-05 15:07:05 +11001894 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001895 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1896 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001897 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001898 }
1899 return (0);
1900}
1901
1902int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001903mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001904{
1905 gss_buffer_desc gssbuf, mic;
1906 OM_uint32 ret;
1907 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001908
Damien Miller0425d402003-11-17 22:18:21 +11001909 gssbuf.value = buffer_get_string(m, &len);
1910 gssbuf.length = len;
1911 mic.value = buffer_get_string(m, &len);
1912 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001913
Damien Miller0425d402003-11-17 22:18:21 +11001914 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001915
Damien Miller0425d402003-11-17 22:18:21 +11001916 xfree(gssbuf.value);
1917 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001918
Damien Miller0425d402003-11-17 22:18:21 +11001919 buffer_clear(m);
1920 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001921
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001922 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001923
Damien Miller0425d402003-11-17 22:18:21 +11001924 if (!GSS_ERROR(ret))
1925 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001926
Damien Miller0425d402003-11-17 22:18:21 +11001927 return (0);
1928}
1929
1930int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001931mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001932{
1933 int authenticated;
1934
1935 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1936
1937 buffer_clear(m);
1938 buffer_put_int(m, authenticated);
1939
1940 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001941 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001942
Damien Miller6fd6def2005-11-05 15:07:05 +11001943 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001944
1945 /* Monitor loop will terminate if authenticated */
1946 return (authenticated);
1947}
1948#endif /* GSSAPI */