blob: c21c0fab8d796cd77243707de16cd21e57777e36 [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110028RCSID("$OpenBSD: monitor.c,v 1.67 2006/02/10 01:44:26 stevesk Exp $");
29
30#include <sys/types.h>
31#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000032
Damien Miller03e20032006-03-15 11:16:59 +110033#include <paths.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000034
35#ifdef SKEY
36#include <skey.h>
37#endif
38
Damien Miller03e20032006-03-15 11:16:59 +110039#include <openssl/dh.h>
40
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000041#include "ssh.h"
42#include "auth.h"
43#include "kex.h"
44#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000045#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
46#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000047#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000048#define TARGET_OS_MAC 1
49#else
50#include "zlib.h"
51#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000052#include "packet.h"
53#include "auth-options.h"
54#include "sshpty.h"
55#include "channels.h"
56#include "session.h"
57#include "sshlogin.h"
58#include "canohost.h"
59#include "log.h"
60#include "servconf.h"
61#include "monitor.h"
62#include "monitor_mm.h"
63#include "monitor_wrap.h"
64#include "monitor_fdpass.h"
65#include "xmalloc.h"
66#include "misc.h"
67#include "buffer.h"
68#include "bufaux.h"
69#include "compat.h"
70#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071
Darren Tucker0efd1552003-08-26 11:49:55 +100072#ifdef GSSAPI
73#include "ssh-gss.h"
74static Gssctxt *gsscontext = NULL;
75#endif
76
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077/* Imports */
78extern ServerOptions options;
79extern u_int utmp_len;
80extern Newkeys *current_keys[];
81extern z_stream incoming_stream;
82extern z_stream outgoing_stream;
83extern u_char session_id[];
84extern Buffer input, output;
85extern Buffer auth_debug;
86extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +100087extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088
89/* State exported from the child */
90
91struct {
92 z_stream incoming;
93 z_stream outgoing;
94 u_char *keyin;
95 u_int keyinlen;
96 u_char *keyout;
97 u_int keyoutlen;
98 u_char *ivin;
99 u_int ivinlen;
100 u_char *ivout;
101 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000102 u_char *ssh1key;
103 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104 int ssh1cipher;
105 int ssh1protoflags;
106 u_char *input;
107 u_int ilen;
108 u_char *output;
109 u_int olen;
110} child_state;
111
Damien Miller469954d2003-06-18 20:25:33 +1000112/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113
114int mm_answer_moduli(int, Buffer *);
115int mm_answer_sign(int, Buffer *);
116int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000117int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118int mm_answer_authserv(int, Buffer *);
119int mm_answer_authpassword(int, Buffer *);
120int mm_answer_bsdauthquery(int, Buffer *);
121int mm_answer_bsdauthrespond(int, Buffer *);
122int mm_answer_skeyquery(int, Buffer *);
123int mm_answer_skeyrespond(int, Buffer *);
124int mm_answer_keyallowed(int, Buffer *);
125int mm_answer_keyverify(int, Buffer *);
126int mm_answer_pty(int, Buffer *);
127int mm_answer_pty_cleanup(int, Buffer *);
128int mm_answer_term(int, Buffer *);
129int mm_answer_rsa_keyallowed(int, Buffer *);
130int mm_answer_rsa_challenge(int, Buffer *);
131int mm_answer_rsa_response(int, Buffer *);
132int mm_answer_sesskey(int, Buffer *);
133int mm_answer_sessid(int, Buffer *);
134
Damien Miller79418552002-04-23 20:28:48 +1000135#ifdef USE_PAM
136int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000137int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000138int mm_answer_pam_init_ctx(int, Buffer *);
139int mm_answer_pam_query(int, Buffer *);
140int mm_answer_pam_respond(int, Buffer *);
141int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000142#endif
143
Darren Tucker0efd1552003-08-26 11:49:55 +1000144#ifdef GSSAPI
145int mm_answer_gss_setup_ctx(int, Buffer *);
146int mm_answer_gss_accept_ctx(int, Buffer *);
147int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100148int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000149#endif
Damien Miller25162f22002-09-12 09:47:29 +1000150
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100151#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100152int mm_answer_audit_event(int, Buffer *);
153int mm_answer_audit_command(int, Buffer *);
154#endif
155
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000156static Authctxt *authctxt;
157static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
158
159/* local state for key verify */
160static u_char *key_blob = NULL;
161static u_int key_bloblen = 0;
162static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000163static char *hostbased_cuser = NULL;
164static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000166static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000167static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000168static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169
170struct mon_table {
171 enum monitor_reqtype type;
172 int flags;
173 int (*f)(int, Buffer *);
174};
175
176#define MON_ISAUTH 0x0004 /* Required for Authentication */
177#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
178#define MON_ONCE 0x0010 /* Disable after calling */
179
180#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
181
182#define MON_PERMIT 0x1000 /* Request is permitted */
183
184struct mon_table mon_dispatch_proto20[] = {
185 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
186 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
187 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
188 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000189 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000190 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000191#ifdef USE_PAM
192 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000193 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000194 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
195 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
196 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
197 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000198#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100199#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100200 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100201#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202#ifdef BSD_AUTH
203 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100204 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205#endif
206#ifdef SKEY
207 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
208 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
209#endif
210 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
211 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000212#ifdef GSSAPI
213 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
214 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
215 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100216 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000217#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218 {0, 0, NULL}
219};
220
221struct mon_table mon_dispatch_postauth20[] = {
222 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
223 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
224 {MONITOR_REQ_PTY, 0, mm_answer_pty},
225 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
226 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100227#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100228 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
229 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
230#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000231 {0, 0, NULL}
232};
233
234struct mon_table mon_dispatch_proto15[] = {
235 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
236 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
237 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
238 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
239 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
240 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
241 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
242 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
243#ifdef BSD_AUTH
244 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100245 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000246#endif
247#ifdef SKEY
248 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
249 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
250#endif
Damien Millera33501b2002-05-08 12:24:42 +1000251#ifdef USE_PAM
252 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000253 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000254 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
255 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
256 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
257 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000258#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100259#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100260 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100261#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000262 {0, 0, NULL}
263};
264
265struct mon_table mon_dispatch_postauth15[] = {
266 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
267 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
268 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100269#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100270 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100271 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100272#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000273 {0, 0, NULL}
274};
275
276struct mon_table *mon_dispatch;
277
278/* Specifies if a certain message is allowed at the moment */
279
280static void
281monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
282{
283 while (ent->f != NULL) {
284 if (ent->type == type) {
285 ent->flags &= ~MON_PERMIT;
286 ent->flags |= permit ? MON_PERMIT : 0;
287 return;
288 }
289 ent++;
290 }
291}
292
293static void
294monitor_permit_authentications(int permit)
295{
296 struct mon_table *ent = mon_dispatch;
297
298 while (ent->f != NULL) {
299 if (ent->flags & MON_AUTH) {
300 ent->flags &= ~MON_PERMIT;
301 ent->flags |= permit ? MON_PERMIT : 0;
302 }
303 ent++;
304 }
305}
306
Darren Tucker3e33cec2003-10-02 16:12:36 +1000307void
308monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000309{
310 struct mon_table *ent;
311 int authenticated = 0;
312
313 debug3("preauth child monitor started");
314
Darren Tucker3e33cec2003-10-02 16:12:36 +1000315 authctxt = _authctxt;
316 memset(authctxt, 0, sizeof(*authctxt));
317
Darren Tuckerde0de392005-03-31 23:52:04 +1000318 authctxt->loginmsg = &loginmsg;
319
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000320 if (compat20) {
321 mon_dispatch = mon_dispatch_proto20;
322
323 /* Permit requests for moduli and signatures */
324 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
325 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
326 } else {
327 mon_dispatch = mon_dispatch_proto15;
328
329 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
330 }
331
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000332 /* The first few requests do not require asynchronous access */
333 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000334 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335 if (authenticated) {
336 if (!(ent->flags & MON_AUTHDECIDE))
337 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000338 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339 if (authctxt->pw->pw_uid == 0 &&
340 !auth_root_allowed(auth_method))
341 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000342#ifdef USE_PAM
343 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100344 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000345 Buffer m;
346
347 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100348 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000349 MONITOR_REQ_PAM_ACCOUNT, &m);
350 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
351 buffer_free(&m);
352 }
353#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000354 }
355
356 if (ent->flags & MON_AUTHDECIDE) {
357 auth_log(authctxt, authenticated, auth_method,
358 compat20 ? " ssh2" : "");
359 if (!authenticated)
360 authctxt->failures++;
361 }
362 }
363
364 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000365 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366
367 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000368 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000370 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371}
372
Damien Millerbe64d432003-05-14 19:31:12 +1000373static void
374monitor_set_child_handler(pid_t pid)
375{
376 monitor_child_pid = pid;
377}
378
379static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000380monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000381{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000382 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000383}
384
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000386monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387{
Damien Millerbe64d432003-05-14 19:31:12 +1000388 monitor_set_child_handler(pmonitor->m_pid);
389 signal(SIGHUP, &monitor_child_handler);
390 signal(SIGTERM, &monitor_child_handler);
391
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000392 if (compat20) {
393 mon_dispatch = mon_dispatch_postauth20;
394
395 /* Permit requests for moduli and signatures */
396 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
397 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
398 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399 } else {
400 mon_dispatch = mon_dispatch_postauth15;
401 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
402 }
403 if (!no_pty_flag) {
404 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
405 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
406 }
407
408 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000409 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410}
411
412void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000413monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000414{
Damien Miller2d6b8332002-06-21 15:59:49 +1000415 if (options.compression) {
416 /* The member allocation is not visible, so sync it */
417 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
418 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000419}
420
421int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000422monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000423 struct mon_table **pent)
424{
425 Buffer m;
426 int ret;
427 u_char type;
428
429 buffer_init(&m);
430
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000431 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000432 type = buffer_get_char(&m);
433
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000434 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000435
436 while (ent->f != NULL) {
437 if (ent->type == type)
438 break;
439 ent++;
440 }
441
442 if (ent->f != NULL) {
443 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000444 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000445 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000446 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000447 buffer_free(&m);
448
449 /* The child may use this request only once, disable it */
450 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000451 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000452 type);
453 ent->flags &= ~MON_PERMIT;
454 }
455
456 if (pent != NULL)
457 *pent = ent;
458
459 return ret;
460 }
461
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000462 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463
464 /* NOTREACHED */
465 return (-1);
466}
467
468/* allowed key state */
469static int
470monitor_allowed_key(u_char *blob, u_int bloblen)
471{
472 /* make sure key is allowed */
473 if (key_blob == NULL || key_bloblen != bloblen ||
474 memcmp(key_blob, blob, key_bloblen))
475 return (0);
476 return (1);
477}
478
479static void
480monitor_reset_key_state(void)
481{
482 /* reset state */
483 if (key_blob != NULL)
484 xfree(key_blob);
485 if (hostbased_cuser != NULL)
486 xfree(hostbased_cuser);
487 if (hostbased_chost != NULL)
488 xfree(hostbased_chost);
489 key_blob = NULL;
490 key_bloblen = 0;
491 key_blobtype = MM_NOKEY;
492 hostbased_cuser = NULL;
493 hostbased_chost = NULL;
494}
495
496int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000497mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498{
499 DH *dh;
500 int min, want, max;
501
502 min = buffer_get_int(m);
503 want = buffer_get_int(m);
504 max = buffer_get_int(m);
505
506 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000507 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000508 /* We need to check here, too, in case the child got corrupted */
509 if (max < min || want < min || max < want)
510 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000511 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000512
513 buffer_clear(m);
514
515 dh = choose_dh(min, want, max);
516 if (dh == NULL) {
517 buffer_put_char(m, 0);
518 return (0);
519 } else {
520 /* Send first bignum */
521 buffer_put_char(m, 1);
522 buffer_put_bignum2(m, dh->p);
523 buffer_put_bignum2(m, dh->g);
524
525 DH_free(dh);
526 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000527 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000528 return (0);
529}
530
531int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000532mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000533{
534 Key *key;
535 u_char *p;
536 u_char *signature;
537 u_int siglen, datlen;
538 int keyid;
539
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000540 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000541
542 keyid = buffer_get_int(m);
543 p = buffer_get_string(m, &datlen);
544
545 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000546 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000547
Ben Lindstromf67e0772002-06-06 20:58:19 +0000548 /* save session id, it will be passed on the first call */
549 if (session_id2_len == 0) {
550 session_id2_len = datlen;
551 session_id2 = xmalloc(session_id2_len);
552 memcpy(session_id2, p, session_id2_len);
553 }
554
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000555 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000556 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000558 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000559
Ben Lindstromd5502182002-06-27 00:12:57 +0000560 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561
562 buffer_clear(m);
563 buffer_put_string(m, signature, siglen);
564
565 xfree(p);
566 xfree(signature);
567
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000568 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569
570 /* Turn on permissions for getpwnam */
571 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
572
573 return (0);
574}
575
576/* Retrieves the password entry and also checks if the user is permitted */
577
578int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000579mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000580{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000581 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582 struct passwd *pwent;
583 int allowed = 0;
584
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000585 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
587 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000588 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589
Darren Tuckerb09b6772004-06-22 15:06:46 +1000590 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591
Darren Tuckerb09b6772004-06-22 15:06:46 +1000592 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593
Darren Tuckerb09b6772004-06-22 15:06:46 +1000594 authctxt->user = xstrdup(username);
595 setproctitle("%s [priv]", pwent ? username : "unknown");
596 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597
598 buffer_clear(m);
599
600 if (pwent == NULL) {
601 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100602 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603 goto out;
604 }
605
606 allowed = 1;
607 authctxt->pw = pwent;
608 authctxt->valid = 1;
609
610 buffer_put_char(m, 1);
611 buffer_put_string(m, pwent, sizeof(struct passwd));
612 buffer_put_cstring(m, pwent->pw_name);
613 buffer_put_cstring(m, "*");
614 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000615#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000617#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618 buffer_put_cstring(m, pwent->pw_dir);
619 buffer_put_cstring(m, pwent->pw_shell);
620
621 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000622 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000623 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624
625 /* For SSHv1 allow authentication now */
626 if (!compat20)
627 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000628 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000629 /* Allow service/style information on the auth context */
630 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000631 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
632 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000633
Damien Millera33501b2002-05-08 12:24:42 +1000634#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000635 if (options.use_pam)
636 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000637#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100638#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100639 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100640#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000641
642 return (0);
643}
644
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000645int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000646{
647 char *banner;
648
649 buffer_clear(m);
650 banner = auth2_read_banner();
651 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000652 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000653
654 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000655 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000656
657 return (0);
658}
659
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000661mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000662{
663 monitor_permit_authentications(1);
664
665 authctxt->service = buffer_get_string(m, NULL);
666 authctxt->style = buffer_get_string(m, NULL);
667 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000668 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669
670 if (strlen(authctxt->style) == 0) {
671 xfree(authctxt->style);
672 authctxt->style = NULL;
673 }
674
675 return (0);
676}
677
678int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000679mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680{
681 static int call_count;
682 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000683 int authenticated;
684 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685
686 passwd = buffer_get_string(m, &plen);
687 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000688 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000689 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690 memset(passwd, 0, strlen(passwd));
691 xfree(passwd);
692
693 buffer_clear(m);
694 buffer_put_int(m, authenticated);
695
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000696 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000697 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000698
699 call_count++;
700 if (plen == 0 && call_count == 1)
701 auth_method = "none";
702 else
703 auth_method = "password";
704
705 /* Causes monitor loop to terminate if authenticated */
706 return (authenticated);
707}
708
709#ifdef BSD_AUTH
710int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000711mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712{
713 char *name, *infotxt;
714 u_int numprompts;
715 u_int *echo_on;
716 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100717 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718
Damien Millerb7df3af2003-02-24 11:55:46 +1100719 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
720 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000721
722 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100723 buffer_put_int(m, success);
724 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725 buffer_put_cstring(m, prompts[0]);
726
Damien Millerb7df3af2003-02-24 11:55:46 +1100727 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000728 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000729
Damien Millerb7df3af2003-02-24 11:55:46 +1100730 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731 xfree(name);
732 xfree(infotxt);
733 xfree(prompts);
734 xfree(echo_on);
735 }
736
737 return (0);
738}
739
740int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000741mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000742{
743 char *response;
744 int authok;
745
746 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000747 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748
749 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000750 authok = options.challenge_response_authentication &&
751 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000752 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000753 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754 xfree(response);
755
756 buffer_clear(m);
757 buffer_put_int(m, authok);
758
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000759 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000760 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761
762 auth_method = "bsdauth";
763
764 return (authok != 0);
765}
766#endif
767
768#ifdef SKEY
769int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000770mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000771{
772 struct skey skey;
773 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100774 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000775
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000776 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
777 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000778
779 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100780 buffer_put_int(m, success);
781 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000782 buffer_put_cstring(m, challenge);
783
Damien Millerb7df3af2003-02-24 11:55:46 +1100784 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000785 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000786
787 return (0);
788}
789
790int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000791mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792{
793 char *response;
794 int authok;
795
796 response = buffer_get_string(m, NULL);
797
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000798 authok = (options.challenge_response_authentication &&
799 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000800 skey_haskey(authctxt->pw->pw_name) == 0 &&
801 skey_passcheck(authctxt->pw->pw_name, response) != -1);
802
803 xfree(response);
804
805 buffer_clear(m);
806 buffer_put_int(m, authok);
807
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000808 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000809 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000810
811 auth_method = "skey";
812
813 return (authok != 0);
814}
815#endif
816
Damien Miller79418552002-04-23 20:28:48 +1000817#ifdef USE_PAM
818int
Darren Tucker7eda5922004-06-22 13:22:50 +1000819mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000820{
Damien Miller4e448a32003-05-14 15:11:48 +1000821 if (!options.use_pam)
822 fatal("UsePAM not set, but ended up in %s anyway", __func__);
823
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100824 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000825
Damien Miller1f499fd2003-08-25 13:08:49 +1000826 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
827
Damien Miller79418552002-04-23 20:28:48 +1000828 return (0);
829}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000830
Damien Miller1f499fd2003-08-25 13:08:49 +1000831int
Darren Tucker7eda5922004-06-22 13:22:50 +1000832mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000833{
834 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100835
Damien Miller1f499fd2003-08-25 13:08:49 +1000836 if (!options.use_pam)
837 fatal("UsePAM not set, but ended up in %s anyway", __func__);
838
839 ret = do_pam_account();
840
841 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000842 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000843
Darren Tucker7eda5922004-06-22 13:22:50 +1000844 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000845
846 return (ret);
847}
848
Damien Miller4f9f42a2003-05-10 19:28:02 +1000849static void *sshpam_ctxt, *sshpam_authok;
850extern KbdintDevice sshpam_device;
851
852int
Darren Tucker7eda5922004-06-22 13:22:50 +1000853mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000854{
855
856 debug3("%s", __func__);
857 authctxt->user = buffer_get_string(m, NULL);
858 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
859 sshpam_authok = NULL;
860 buffer_clear(m);
861 if (sshpam_ctxt != NULL) {
862 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
863 buffer_put_int(m, 1);
864 } else {
865 buffer_put_int(m, 0);
866 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000867 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000868 return (0);
869}
870
871int
Darren Tucker7eda5922004-06-22 13:22:50 +1000872mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000873{
874 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000875 u_int i, num, *echo_on;
876 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000877
878 debug3("%s", __func__);
879 sshpam_authok = NULL;
880 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
881 if (ret == 0 && num == 0)
882 sshpam_authok = sshpam_ctxt;
883 if (num > 1 || name == NULL || info == NULL)
884 ret = -1;
885 buffer_clear(m);
886 buffer_put_int(m, ret);
887 buffer_put_cstring(m, name);
888 xfree(name);
889 buffer_put_cstring(m, info);
890 xfree(info);
891 buffer_put_int(m, num);
892 for (i = 0; i < num; ++i) {
893 buffer_put_cstring(m, prompts[i]);
894 xfree(prompts[i]);
895 buffer_put_int(m, echo_on[i]);
896 }
897 if (prompts != NULL)
898 xfree(prompts);
899 if (echo_on != NULL)
900 xfree(echo_on);
Darren Tucker7eda5922004-06-22 13:22:50 +1000901 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000902 return (0);
903}
904
905int
Darren Tucker7eda5922004-06-22 13:22:50 +1000906mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000907{
908 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000909 u_int i, num;
910 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000911
912 debug3("%s", __func__);
913 sshpam_authok = NULL;
914 num = buffer_get_int(m);
915 if (num > 0) {
916 resp = xmalloc(num * sizeof(char *));
917 for (i = 0; i < num; ++i)
918 resp[i] = buffer_get_string(m, NULL);
919 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
920 for (i = 0; i < num; ++i)
921 xfree(resp[i]);
922 xfree(resp);
923 } else {
924 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
925 }
926 buffer_clear(m);
927 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000928 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000929 auth_method = "keyboard-interactive/pam";
930 if (ret == 0)
931 sshpam_authok = sshpam_ctxt;
932 return (0);
933}
934
935int
Darren Tucker7eda5922004-06-22 13:22:50 +1000936mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000937{
938
939 debug3("%s", __func__);
940 (sshpam_device.free_ctx)(sshpam_ctxt);
941 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000942 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000943 return (sshpam_authok == sshpam_ctxt);
944}
Damien Miller79418552002-04-23 20:28:48 +1000945#endif
946
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000947static void
948mm_append_debug(Buffer *m)
949{
950 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000951 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952 buffer_append(m, buffer_ptr(&auth_debug),
953 buffer_len(&auth_debug));
954 buffer_clear(&auth_debug);
955 }
956}
957
958int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000959mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000960{
961 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000962 char *cuser, *chost;
963 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964 u_int bloblen;
965 enum mm_keytype type = 0;
966 int allowed = 0;
967
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000968 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969
970 type = buffer_get_int(m);
971 cuser = buffer_get_string(m, NULL);
972 chost = buffer_get_string(m, NULL);
973 blob = buffer_get_string(m, &bloblen);
974
975 key = key_from_blob(blob, bloblen);
976
977 if ((compat20 && type == MM_RSAHOSTKEY) ||
978 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000979 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000981 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000982
Damien Miller3e3b5142003-11-17 21:13:40 +1100983 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +1100984 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000986 allowed = options.pubkey_authentication &&
987 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000988 break;
989 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000990 allowed = options.hostbased_authentication &&
991 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000992 cuser, chost, key);
993 break;
994 case MM_RSAHOSTKEY:
995 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000996 allowed = options.rhosts_rsa_authentication &&
997 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998 cuser, chost, key);
999 break;
1000 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001001 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001002 break;
1003 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004 }
Damien Miller00111382003-03-10 11:21:17 +11001005 if (key != NULL)
1006 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007
1008 /* clear temporarily storage (used by verify) */
1009 monitor_reset_key_state();
1010
1011 if (allowed) {
1012 /* Save temporarily for comparison in verify */
1013 key_blob = blob;
1014 key_bloblen = bloblen;
1015 key_blobtype = type;
1016 hostbased_cuser = cuser;
1017 hostbased_chost = chost;
1018 }
1019
1020 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001021 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022
1023 buffer_clear(m);
1024 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001025 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001026
1027 mm_append_debug(m);
1028
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001029 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001030
1031 if (type == MM_RSAHOSTKEY)
1032 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1033
1034 return (0);
1035}
1036
1037static int
1038monitor_valid_userblob(u_char *data, u_int datalen)
1039{
1040 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001041 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001042 u_int len;
1043 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001044
1045 buffer_init(&b);
1046 buffer_append(&b, data, datalen);
1047
1048 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001049 p = buffer_ptr(&b);
1050 len = buffer_len(&b);
1051 if ((session_id2 == NULL) ||
1052 (len < session_id2_len) ||
1053 (memcmp(p, session_id2, session_id2_len) != 0))
1054 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001055 buffer_consume(&b, session_id2_len);
1056 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001057 p = buffer_get_string(&b, &len);
1058 if ((session_id2 == NULL) ||
1059 (len != session_id2_len) ||
1060 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001061 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001062 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001063 }
1064 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1065 fail++;
1066 p = buffer_get_string(&b, NULL);
1067 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001068 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001069 authctxt->user, p);
1070 fail++;
1071 }
1072 xfree(p);
1073 buffer_skip_string(&b);
1074 if (datafellows & SSH_BUG_PKAUTH) {
1075 if (!buffer_get_char(&b))
1076 fail++;
1077 } else {
1078 p = buffer_get_string(&b, NULL);
1079 if (strcmp("publickey", p) != 0)
1080 fail++;
1081 xfree(p);
1082 if (!buffer_get_char(&b))
1083 fail++;
1084 buffer_skip_string(&b);
1085 }
1086 buffer_skip_string(&b);
1087 if (buffer_len(&b) != 0)
1088 fail++;
1089 buffer_free(&b);
1090 return (fail == 0);
1091}
1092
1093static int
Damien Millera10f5612002-09-12 09:49:15 +10001094monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1095 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001096{
1097 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001098 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001099 u_int len;
1100 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001101
1102 buffer_init(&b);
1103 buffer_append(&b, data, datalen);
1104
Ben Lindstromf67e0772002-06-06 20:58:19 +00001105 p = buffer_get_string(&b, &len);
1106 if ((session_id2 == NULL) ||
1107 (len != session_id2_len) ||
1108 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001109 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001110 xfree(p);
1111
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001112 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1113 fail++;
1114 p = buffer_get_string(&b, NULL);
1115 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001116 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001117 authctxt->user, p);
1118 fail++;
1119 }
1120 xfree(p);
1121 buffer_skip_string(&b); /* service */
1122 p = buffer_get_string(&b, NULL);
1123 if (strcmp(p, "hostbased") != 0)
1124 fail++;
1125 xfree(p);
1126 buffer_skip_string(&b); /* pkalg */
1127 buffer_skip_string(&b); /* pkblob */
1128
1129 /* verify client host, strip trailing dot if necessary */
1130 p = buffer_get_string(&b, NULL);
1131 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1132 p[len - 1] = '\0';
1133 if (strcmp(p, chost) != 0)
1134 fail++;
1135 xfree(p);
1136
1137 /* verify client user */
1138 p = buffer_get_string(&b, NULL);
1139 if (strcmp(p, cuser) != 0)
1140 fail++;
1141 xfree(p);
1142
1143 if (buffer_len(&b) != 0)
1144 fail++;
1145 buffer_free(&b);
1146 return (fail == 0);
1147}
1148
1149int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001150mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001151{
1152 Key *key;
1153 u_char *signature, *data, *blob;
1154 u_int signaturelen, datalen, bloblen;
1155 int verified = 0;
1156 int valid_data = 0;
1157
1158 blob = buffer_get_string(m, &bloblen);
1159 signature = buffer_get_string(m, &signaturelen);
1160 data = buffer_get_string(m, &datalen);
1161
1162 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001163 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001164 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001165
1166 key = key_from_blob(blob, bloblen);
1167 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001168 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001169
1170 switch (key_blobtype) {
1171 case MM_USERKEY:
1172 valid_data = monitor_valid_userblob(data, datalen);
1173 break;
1174 case MM_HOSTKEY:
1175 valid_data = monitor_valid_hostbasedblob(data, datalen,
1176 hostbased_cuser, hostbased_chost);
1177 break;
1178 default:
1179 valid_data = 0;
1180 break;
1181 }
1182 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001183 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184
1185 verified = key_verify(key, signature, signaturelen, data, datalen);
1186 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001187 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001188
1189 key_free(key);
1190 xfree(blob);
1191 xfree(signature);
1192 xfree(data);
1193
Ben Lindstrome1c09122002-06-23 00:38:24 +00001194 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1195
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001196 monitor_reset_key_state();
1197
1198 buffer_clear(m);
1199 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001200 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001201
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 return (verified);
1203}
1204
1205static void
1206mm_record_login(Session *s, struct passwd *pw)
1207{
1208 socklen_t fromlen;
1209 struct sockaddr_storage from;
1210
1211 /*
1212 * Get IP address of client. If the connection is not a socket, let
1213 * the address be 0.0.0.0.
1214 */
1215 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001216 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001218 if (getpeername(packet_get_connection_in(),
1219 (struct sockaddr *) & from, &fromlen) < 0) {
1220 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001221 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222 }
1223 }
1224 /* Record that there was a login on that tty from the remote host. */
1225 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001226 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001227 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228}
1229
1230static void
1231mm_session_close(Session *s)
1232{
Damien Miller04bd8b02003-05-25 14:38:33 +10001233 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001235 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001236 session_pty_cleanup2(s);
1237 }
1238 s->used = 0;
1239}
1240
1241int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001242mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001244 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 Session *s;
1246 int res, fd0;
1247
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001248 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249
1250 buffer_clear(m);
1251 s = session_new();
1252 if (s == NULL)
1253 goto error;
1254 s->authctxt = authctxt;
1255 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001256 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001257 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1258 if (res == 0)
1259 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001260 pty_setowner(authctxt->pw, s->tty);
1261
1262 buffer_put_int(m, 1);
1263 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001264
1265 /* We need to trick ttyslot */
1266 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001267 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001268
1269 mm_record_login(s, authctxt->pw);
1270
1271 /* Now we can close the file descriptor again */
1272 close(0);
1273
Darren Tucker09991742004-07-17 17:05:14 +10001274 /* send messages generated by record_login */
1275 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1276 buffer_clear(&loginmsg);
1277
1278 mm_request_send(sock, MONITOR_ANS_PTY, m);
1279
1280 mm_send_fd(sock, s->ptyfd);
1281 mm_send_fd(sock, s->ttyfd);
1282
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283 /* make sure nothing uses fd 0 */
1284 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001285 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001286 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001287 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001288
1289 /* slave is not needed */
1290 close(s->ttyfd);
1291 s->ttyfd = s->ptyfd;
1292 /* no need to dup() because nobody closes ptyfd */
1293 s->ptymaster = s->ptyfd;
1294
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001295 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001296
1297 return (0);
1298
1299 error:
1300 if (s != NULL)
1301 mm_session_close(s);
1302 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001303 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304 return (0);
1305}
1306
1307int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001308mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309{
1310 Session *s;
1311 char *tty;
1312
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001313 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314
1315 tty = buffer_get_string(m, NULL);
1316 if ((s = session_by_tty(tty)) != NULL)
1317 mm_session_close(s);
1318 buffer_clear(m);
1319 xfree(tty);
1320 return (0);
1321}
1322
1323int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001324mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001325{
1326 BIGNUM *p;
1327 int rsafail;
1328
1329 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001330 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001331
1332 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001333 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334
1335 buffer_get_bignum2(m, p);
1336
1337 rsafail = ssh1_session_key(p);
1338
1339 buffer_clear(m);
1340 buffer_put_int(m, rsafail);
1341 buffer_put_bignum2(m, p);
1342
1343 BN_clear_free(p);
1344
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001345 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001346
1347 /* Turn on permissions for sessid passing */
1348 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1349
1350 return (0);
1351}
1352
1353int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001354mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001355{
1356 int i;
1357
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001358 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359
1360 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001361 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362 for (i = 0; i < 16; i++)
1363 session_id[i] = buffer_get_char(m);
1364
1365 /* Turn on permissions for getpwnam */
1366 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1367
1368 return (0);
1369}
1370
1371int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001372mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001373{
1374 BIGNUM *client_n;
1375 Key *key = NULL;
1376 u_char *blob = NULL;
1377 u_int blen = 0;
1378 int allowed = 0;
1379
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001380 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001382 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001383 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001384 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001385 buffer_get_bignum2(m, client_n);
1386 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1387 BN_clear_free(client_n);
1388 }
1389 buffer_clear(m);
1390 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001391 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001392
1393 /* clear temporarily storage (used by generate challenge) */
1394 monitor_reset_key_state();
1395
1396 if (allowed && key != NULL) {
1397 key->type = KEY_RSA; /* cheat for key_to_blob */
1398 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001399 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400 buffer_put_string(m, blob, blen);
1401
1402 /* Save temporarily for comparison in verify */
1403 key_blob = blob;
1404 key_bloblen = blen;
1405 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001406 }
Damien Miller00111382003-03-10 11:21:17 +11001407 if (key != NULL)
1408 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001409
1410 mm_append_debug(m);
1411
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001412 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001413
1414 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1415 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1416 return (0);
1417}
1418
1419int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001420mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001421{
1422 Key *key = NULL;
1423 u_char *blob;
1424 u_int blen;
1425
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001426 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427
1428 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001429 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001430 blob = buffer_get_string(m, &blen);
1431 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001432 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001434 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001436 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001437
1438 if (ssh1_challenge)
1439 BN_clear_free(ssh1_challenge);
1440 ssh1_challenge = auth_rsa_generate_challenge(key);
1441
1442 buffer_clear(m);
1443 buffer_put_bignum2(m, ssh1_challenge);
1444
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001445 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001446 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447
1448 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001449
1450 xfree(blob);
1451 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452 return (0);
1453}
1454
1455int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001456mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457{
1458 Key *key = NULL;
1459 u_char *blob, *response;
1460 u_int blen, len;
1461 int success;
1462
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001463 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001464
1465 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001466 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001468 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001469
1470 blob = buffer_get_string(m, &blen);
1471 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001472 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001473 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001474 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001475 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001476 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001477 response = buffer_get_string(m, &len);
1478 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001479 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001480 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1481
Damien Miller00111382003-03-10 11:21:17 +11001482 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001483 key_free(key);
1484 xfree(response);
1485
1486 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1487
1488 /* reset state */
1489 BN_clear_free(ssh1_challenge);
1490 ssh1_challenge = NULL;
1491 monitor_reset_key_state();
1492
1493 buffer_clear(m);
1494 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001495 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496
1497 return (success);
1498}
1499
1500int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001501mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001503 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504 int res, status;
1505
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001506 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507
1508 /* The child is terminating */
1509 session_destroy_all(&mm_session_close);
1510
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001511 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001512 if (errno != EINTR)
1513 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514
1515 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1516
1517 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001518 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519}
1520
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001521#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001522/* Report that an audit event occurred */
1523int
1524mm_answer_audit_event(int socket, Buffer *m)
1525{
1526 ssh_audit_event_t event;
1527
1528 debug3("%s entering", __func__);
1529
1530 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001531 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001532 case SSH_AUTH_FAIL_PUBKEY:
1533 case SSH_AUTH_FAIL_HOSTBASED:
1534 case SSH_AUTH_FAIL_GSSAPI:
1535 case SSH_LOGIN_EXCEED_MAXTRIES:
1536 case SSH_LOGIN_ROOT_DENIED:
1537 case SSH_CONNECTION_CLOSE:
1538 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001539 audit_event(event);
1540 break;
1541 default:
1542 fatal("Audit event type %d not permitted", event);
1543 }
1544
1545 return (0);
1546}
1547
1548int
1549mm_answer_audit_command(int socket, Buffer *m)
1550{
1551 u_int len;
1552 char *cmd;
1553
1554 debug3("%s entering", __func__);
1555 cmd = buffer_get_string(m, &len);
1556 /* sanity check command, if so how? */
1557 audit_run_command(cmd);
1558 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001559 return (0);
1560}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001561#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001562
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001563void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001564monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001565{
1566 if (compat20) {
1567 set_newkeys(MODE_IN);
1568 set_newkeys(MODE_OUT);
1569 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001570 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001571 packet_set_encryption_key(child_state.ssh1key,
1572 child_state.ssh1keylen, child_state.ssh1cipher);
1573 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001574 }
1575
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001576 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001577 packet_set_keycontext(MODE_OUT, child_state.keyout);
1578 xfree(child_state.keyout);
1579 packet_set_keycontext(MODE_IN, child_state.keyin);
1580 xfree(child_state.keyin);
1581
1582 if (!compat20) {
1583 packet_set_iv(MODE_OUT, child_state.ivout);
1584 xfree(child_state.ivout);
1585 packet_set_iv(MODE_IN, child_state.ivin);
1586 xfree(child_state.ivin);
1587 }
1588
1589 memcpy(&incoming_stream, &child_state.incoming,
1590 sizeof(incoming_stream));
1591 memcpy(&outgoing_stream, &child_state.outgoing,
1592 sizeof(outgoing_stream));
1593
1594 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001595 if (options.compression)
1596 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001597
1598 /* Network I/O buffers */
1599 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1600 buffer_clear(&input);
1601 buffer_append(&input, child_state.input, child_state.ilen);
1602 memset(child_state.input, 0, child_state.ilen);
1603 xfree(child_state.input);
1604
1605 buffer_clear(&output);
1606 buffer_append(&output, child_state.output, child_state.olen);
1607 memset(child_state.output, 0, child_state.olen);
1608 xfree(child_state.output);
1609}
1610
1611static Kex *
1612mm_get_kex(Buffer *m)
1613{
1614 Kex *kex;
1615 void *blob;
1616 u_int bloblen;
1617
1618 kex = xmalloc(sizeof(*kex));
1619 memset(kex, 0, sizeof(*kex));
1620 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001621 if ((session_id2 == NULL) ||
1622 (kex->session_id_len != session_id2_len) ||
1623 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1624 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001626 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001627 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001628 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001629 kex->server = 1;
1630 kex->hostkey_type = buffer_get_int(m);
1631 kex->kex_type = buffer_get_int(m);
1632 blob = buffer_get_string(m, &bloblen);
1633 buffer_init(&kex->my);
1634 buffer_append(&kex->my, blob, bloblen);
1635 xfree(blob);
1636 blob = buffer_get_string(m, &bloblen);
1637 buffer_init(&kex->peer);
1638 buffer_append(&kex->peer, blob, bloblen);
1639 xfree(blob);
1640 kex->done = 1;
1641 kex->flags = buffer_get_int(m);
1642 kex->client_version_string = buffer_get_string(m, NULL);
1643 kex->server_version_string = buffer_get_string(m, NULL);
1644 kex->load_host_key=&get_hostkey_by_type;
1645 kex->host_key_index=&get_hostkey_index;
1646
1647 return (kex);
1648}
1649
1650/* This function requries careful sanity checking */
1651
1652void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001653mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001654{
1655 Buffer m;
1656 u_char *blob, *p;
1657 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001658 u_int32_t seqnr, packets;
1659 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001660
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001661 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001662
1663 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001664 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001665 if (!compat20) {
1666 child_state.ssh1protoflags = buffer_get_int(&m);
1667 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001668 child_state.ssh1key = buffer_get_string(&m,
1669 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001670 child_state.ivout = buffer_get_string(&m,
1671 &child_state.ivoutlen);
1672 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1673 goto skip;
1674 } else {
1675 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001676 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677 }
1678
1679 blob = buffer_get_string(&m, &bloblen);
1680 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1681 xfree(blob);
1682
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001683 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001684 blob = buffer_get_string(&m, &bloblen);
1685 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1686 xfree(blob);
1687
1688 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001689 seqnr = buffer_get_int(&m);
1690 blocks = buffer_get_int64(&m);
1691 packets = buffer_get_int(&m);
1692 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1693 seqnr = buffer_get_int(&m);
1694 blocks = buffer_get_int64(&m);
1695 packets = buffer_get_int(&m);
1696 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001697
1698 skip:
1699 /* Get the key context */
1700 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1701 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1702
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001703 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704 /* Get compression state */
1705 p = buffer_get_string(&m, &plen);
1706 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001707 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001708 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1709 xfree(p);
1710
1711 p = buffer_get_string(&m, &plen);
1712 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001713 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001714 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1715 xfree(p);
1716
1717 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001718 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001719 child_state.input = buffer_get_string(&m, &child_state.ilen);
1720 child_state.output = buffer_get_string(&m, &child_state.olen);
1721
1722 buffer_free(&m);
1723}
1724
1725
1726/* Allocation functions for zlib */
1727void *
1728mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1729{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001730 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001731 void *address;
1732
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001733 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001734 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1735
1736 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001737
1738 return (address);
1739}
1740
1741void
1742mm_zfree(struct mm_master *mm, void *address)
1743{
1744 mm_free(mm, address);
1745}
1746
1747void
1748mm_init_compression(struct mm_master *mm)
1749{
1750 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1751 outgoing_stream.zfree = (free_func)mm_zfree;
1752 outgoing_stream.opaque = mm;
1753
1754 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1755 incoming_stream.zfree = (free_func)mm_zfree;
1756 incoming_stream.opaque = mm;
1757}
1758
1759/* XXX */
1760
1761#define FD_CLOSEONEXEC(x) do { \
1762 if (fcntl(x, F_SETFD, 1) == -1) \
1763 fatal("fcntl(%d, F_SETFD)", x); \
1764} while (0)
1765
1766static void
1767monitor_socketpair(int *pair)
1768{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001769#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001770 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001771 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001772#else
1773 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001774 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001775#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776 FD_CLOSEONEXEC(pair[0]);
1777 FD_CLOSEONEXEC(pair[1]);
1778}
1779
1780#define MM_MEMSIZE 65536
1781
1782struct monitor *
1783monitor_init(void)
1784{
1785 struct monitor *mon;
1786 int pair[2];
1787
1788 mon = xmalloc(sizeof(*mon));
1789
Darren Tuckera8be9e22004-02-06 16:40:27 +11001790 mon->m_pid = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001791 monitor_socketpair(pair);
1792
1793 mon->m_recvfd = pair[0];
1794 mon->m_sendfd = pair[1];
1795
1796 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001797 if (options.compression) {
1798 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1799 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001800
Damien Miller2d6b8332002-06-21 15:59:49 +10001801 /* Compression needs to share state across borders */
1802 mm_init_compression(mon->m_zlib);
1803 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001804
1805 return mon;
1806}
1807
1808void
1809monitor_reinit(struct monitor *mon)
1810{
1811 int pair[2];
1812
1813 monitor_socketpair(pair);
1814
1815 mon->m_recvfd = pair[0];
1816 mon->m_sendfd = pair[1];
1817}
Darren Tucker0efd1552003-08-26 11:49:55 +10001818
1819#ifdef GSSAPI
1820int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001821mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001822{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001823 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001824 OM_uint32 major;
1825 u_int len;
1826
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001827 goid.elements = buffer_get_string(m, &len);
1828 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001829
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001830 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001831
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001832 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001833
1834 buffer_clear(m);
1835 buffer_put_int(m, major);
1836
Damien Miller6fd6def2005-11-05 15:07:05 +11001837 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001838
1839 /* Now we have a context, enable the step */
1840 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1841
1842 return (0);
1843}
1844
1845int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001846mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001847{
1848 gss_buffer_desc in;
1849 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001850 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001851 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001852 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001853
Darren Tucker600ad8d2003-08-26 12:10:48 +10001854 in.value = buffer_get_string(m, &len);
1855 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001856 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1857 xfree(in.value);
1858
1859 buffer_clear(m);
1860 buffer_put_int(m, major);
1861 buffer_put_string(m, out.value, out.length);
1862 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001863 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001864
1865 gss_release_buffer(&minor, &out);
1866
Damien Miller6fd6def2005-11-05 15:07:05 +11001867 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001868 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1869 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001870 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001871 }
1872 return (0);
1873}
1874
1875int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001876mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001877{
1878 gss_buffer_desc gssbuf, mic;
1879 OM_uint32 ret;
1880 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001881
Damien Miller0425d402003-11-17 22:18:21 +11001882 gssbuf.value = buffer_get_string(m, &len);
1883 gssbuf.length = len;
1884 mic.value = buffer_get_string(m, &len);
1885 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001886
Damien Miller0425d402003-11-17 22:18:21 +11001887 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001888
Damien Miller0425d402003-11-17 22:18:21 +11001889 xfree(gssbuf.value);
1890 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001891
Damien Miller0425d402003-11-17 22:18:21 +11001892 buffer_clear(m);
1893 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001894
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001895 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001896
Damien Miller0425d402003-11-17 22:18:21 +11001897 if (!GSS_ERROR(ret))
1898 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001899
Damien Miller0425d402003-11-17 22:18:21 +11001900 return (0);
1901}
1902
1903int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001904mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001905{
1906 int authenticated;
1907
1908 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1909
1910 buffer_clear(m);
1911 buffer_put_int(m, authenticated);
1912
1913 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001914 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001915
Damien Miller6fd6def2005-11-05 15:07:05 +11001916 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001917
1918 /* Monitor loop will terminate if authenticated */
1919 return (authenticated);
1920}
1921#endif /* GSSAPI */