blob: 8006b833c874dad85e0a11c084d6a84541580f1b [file] [log] [blame]
Damien Miller6a1937e2012-12-12 10:44:38 +11001/* $OpenBSD: monitor.c,v 1.120 2012/12/11 22:16:21 markus Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100032#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110034#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000035
Darren Tucker39972492006-07-12 22:22:46 +100036#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100043#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100044#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Miller607aede2006-09-01 15:48:19 +100046#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100047#ifdef HAVE_POLL_H
48#include <poll.h>
49#else
50# ifdef HAVE_SYS_POLL_H
51# include <sys/poll.h>
52# endif
53#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054
55#ifdef SKEY
56#include <skey.h>
57#endif
58
Damien Miller03e20032006-03-15 11:16:59 +110059#include <openssl/dh.h>
60
Damien Millerb84886b2008-05-19 15:05:07 +100061#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100062#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100063#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000064#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100065#include "key.h"
66#include "buffer.h"
67#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100069#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "kex.h"
71#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000072#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
73#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000074#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000075#define TARGET_OS_MAC 1
76#else
77#include "zlib.h"
78#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079#include "packet.h"
80#include "auth-options.h"
81#include "sshpty.h"
82#include "channels.h"
83#include "session.h"
84#include "sshlogin.h"
85#include "canohost.h"
86#include "log.h"
87#include "servconf.h"
88#include "monitor.h"
89#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#ifdef GSSAPI
91#include "ssh-gss.h"
92#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093#include "monitor_wrap.h"
94#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000096#include "compat.h"
97#include "ssh2.h"
Damien Miller01ed2272008-11-05 16:20:46 +110098#include "jpake.h"
Darren Tuckerb422afa2009-06-21 18:58:46 +100099#include "roaming.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100
Darren Tucker0efd1552003-08-26 11:49:55 +1000101#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000102static Gssctxt *gsscontext = NULL;
103#endif
104
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105/* Imports */
106extern ServerOptions options;
107extern u_int utmp_len;
108extern Newkeys *current_keys[];
109extern z_stream incoming_stream;
110extern z_stream outgoing_stream;
111extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112extern Buffer auth_debug;
113extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000114extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115
116/* State exported from the child */
117
118struct {
119 z_stream incoming;
120 z_stream outgoing;
121 u_char *keyin;
122 u_int keyinlen;
123 u_char *keyout;
124 u_int keyoutlen;
125 u_char *ivin;
126 u_int ivinlen;
127 u_char *ivout;
128 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000129 u_char *ssh1key;
130 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131 int ssh1cipher;
132 int ssh1protoflags;
133 u_char *input;
134 u_int ilen;
135 u_char *output;
136 u_int olen;
Darren Tuckerc5564e12009-06-21 18:53:53 +1000137 u_int64_t sent_bytes;
138 u_int64_t recv_bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000139} child_state;
140
Damien Miller469954d2003-06-18 20:25:33 +1000141/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000142
143int mm_answer_moduli(int, Buffer *);
144int mm_answer_sign(int, Buffer *);
145int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000146int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147int mm_answer_authserv(int, Buffer *);
148int mm_answer_authpassword(int, Buffer *);
149int mm_answer_bsdauthquery(int, Buffer *);
150int mm_answer_bsdauthrespond(int, Buffer *);
151int mm_answer_skeyquery(int, Buffer *);
152int mm_answer_skeyrespond(int, Buffer *);
153int mm_answer_keyallowed(int, Buffer *);
154int mm_answer_keyverify(int, Buffer *);
155int mm_answer_pty(int, Buffer *);
156int mm_answer_pty_cleanup(int, Buffer *);
157int mm_answer_term(int, Buffer *);
158int mm_answer_rsa_keyallowed(int, Buffer *);
159int mm_answer_rsa_challenge(int, Buffer *);
160int mm_answer_rsa_response(int, Buffer *);
161int mm_answer_sesskey(int, Buffer *);
162int mm_answer_sessid(int, Buffer *);
Damien Miller01ed2272008-11-05 16:20:46 +1100163int mm_answer_jpake_get_pwdata(int, Buffer *);
164int mm_answer_jpake_step1(int, Buffer *);
165int mm_answer_jpake_step2(int, Buffer *);
166int mm_answer_jpake_key_confirm(int, Buffer *);
167int mm_answer_jpake_check_confirm(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168
Damien Miller79418552002-04-23 20:28:48 +1000169#ifdef USE_PAM
170int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000171int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000172int mm_answer_pam_init_ctx(int, Buffer *);
173int mm_answer_pam_query(int, Buffer *);
174int mm_answer_pam_respond(int, Buffer *);
175int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000176#endif
177
Darren Tucker0efd1552003-08-26 11:49:55 +1000178#ifdef GSSAPI
179int mm_answer_gss_setup_ctx(int, Buffer *);
180int mm_answer_gss_accept_ctx(int, Buffer *);
181int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100182int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000183#endif
Damien Miller25162f22002-09-12 09:47:29 +1000184
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100185#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100186int mm_answer_audit_event(int, Buffer *);
187int mm_answer_audit_command(int, Buffer *);
188#endif
189
Damien Miller8f0bf232011-06-20 14:42:23 +1000190static int monitor_read_log(struct monitor *);
191
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192static Authctxt *authctxt;
193static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
194
195/* local state for key verify */
196static u_char *key_blob = NULL;
197static u_int key_bloblen = 0;
198static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000199static char *hostbased_cuser = NULL;
200static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100202static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000203static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000204static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000205static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206
207struct mon_table {
208 enum monitor_reqtype type;
209 int flags;
210 int (*f)(int, Buffer *);
211};
212
213#define MON_ISAUTH 0x0004 /* Required for Authentication */
214#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
215#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100216#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217
218#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
219
220#define MON_PERMIT 0x1000 /* Request is permitted */
221
222struct mon_table mon_dispatch_proto20[] = {
223 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
224 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
225 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
226 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000227 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000229#ifdef USE_PAM
230 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000231 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000232 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
233 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
234 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
235 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000236#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100237#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100238 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100239#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240#ifdef BSD_AUTH
241 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100242 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243#endif
244#ifdef SKEY
245 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
246 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
247#endif
248 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
249 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000250#ifdef GSSAPI
251 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
252 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
253 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100254 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000255#endif
Damien Miller01ed2272008-11-05 16:20:46 +1100256#ifdef JPAKE
257 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
258 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
259 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
260 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
261 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
262#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000263 {0, 0, NULL}
264};
265
266struct mon_table mon_dispatch_postauth20[] = {
267 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
268 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
269 {MONITOR_REQ_PTY, 0, mm_answer_pty},
270 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
271 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100272#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100273 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
274 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
275#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000276 {0, 0, NULL}
277};
278
279struct mon_table mon_dispatch_proto15[] = {
280 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
281 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
282 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
283 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100284 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
285 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
287 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
288#ifdef BSD_AUTH
289 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100290 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291#endif
292#ifdef SKEY
293 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
294 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
295#endif
Damien Millera33501b2002-05-08 12:24:42 +1000296#ifdef USE_PAM
297 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000298 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000299 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
300 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
301 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
302 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000303#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100304#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100305 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100306#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000307 {0, 0, NULL}
308};
309
310struct mon_table mon_dispatch_postauth15[] = {
311 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
312 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
313 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100314#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100315 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000316 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100317#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000318 {0, 0, NULL}
319};
320
321struct mon_table *mon_dispatch;
322
323/* Specifies if a certain message is allowed at the moment */
324
325static void
326monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
327{
328 while (ent->f != NULL) {
329 if (ent->type == type) {
330 ent->flags &= ~MON_PERMIT;
331 ent->flags |= permit ? MON_PERMIT : 0;
332 return;
333 }
334 ent++;
335 }
336}
337
338static void
339monitor_permit_authentications(int permit)
340{
341 struct mon_table *ent = mon_dispatch;
342
343 while (ent->f != NULL) {
344 if (ent->flags & MON_AUTH) {
345 ent->flags &= ~MON_PERMIT;
346 ent->flags |= permit ? MON_PERMIT : 0;
347 }
348 ent++;
349 }
350}
351
Darren Tucker3e33cec2003-10-02 16:12:36 +1000352void
353monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000354{
355 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100356 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357
358 debug3("preauth child monitor started");
359
Damien Miller8f0bf232011-06-20 14:42:23 +1000360 close(pmonitor->m_recvfd);
361 close(pmonitor->m_log_sendfd);
362 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
363
Darren Tucker3e33cec2003-10-02 16:12:36 +1000364 authctxt = _authctxt;
365 memset(authctxt, 0, sizeof(*authctxt));
366
Darren Tuckerde0de392005-03-31 23:52:04 +1000367 authctxt->loginmsg = &loginmsg;
368
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369 if (compat20) {
370 mon_dispatch = mon_dispatch_proto20;
371
372 /* Permit requests for moduli and signatures */
373 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
374 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
375 } else {
376 mon_dispatch = mon_dispatch_proto15;
377
378 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
379 }
380
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000381 /* The first few requests do not require asynchronous access */
382 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100383 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100384 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100385 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100386 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100387
388 /* Special handling for multiple required authentications */
389 if (options.num_auth_methods != 0) {
390 if (!compat20)
391 fatal("AuthenticationMethods is not supported"
392 "with SSH protocol 1");
393 if (authenticated &&
394 !auth2_update_methods_lists(authctxt,
395 auth_method)) {
396 debug3("%s: method %s: partial", __func__,
397 auth_method);
398 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100399 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100400 }
401 }
402
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403 if (authenticated) {
404 if (!(ent->flags & MON_AUTHDECIDE))
405 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000406 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407 if (authctxt->pw->pw_uid == 0 &&
408 !auth_root_allowed(auth_method))
409 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000410#ifdef USE_PAM
411 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100412 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000413 Buffer m;
414
415 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100416 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000417 MONITOR_REQ_PAM_ACCOUNT, &m);
418 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
419 buffer_free(&m);
420 }
421#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000422 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100423 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100424 auth_log(authctxt, authenticated, partial,
425 auth_method, auth_submethod,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000426 compat20 ? " ssh2" : "");
427 if (!authenticated)
428 authctxt->failures++;
429 }
Damien Miller01ed2272008-11-05 16:20:46 +1100430#ifdef JPAKE
431 /* Cleanup JPAKE context after authentication */
432 if (ent->flags & MON_AUTHDECIDE) {
433 if (authctxt->jpake_ctx != NULL) {
434 jpake_free(authctxt->jpake_ctx);
435 authctxt->jpake_ctx = NULL;
436 }
437 }
438#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000439 }
440
441 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000442 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100443 if (strcmp(auth_method, "unknown") == 0)
444 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000445
446 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000447 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000448
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000449 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000450
Damien Miller6a1937e2012-12-12 10:44:38 +1100451 /* Drain any buffered messages from the child */
452 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
453 ;
454
Damien Miller8f0bf232011-06-20 14:42:23 +1000455 close(pmonitor->m_sendfd);
456 close(pmonitor->m_log_recvfd);
457 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458}
459
Damien Millerbe64d432003-05-14 19:31:12 +1000460static void
461monitor_set_child_handler(pid_t pid)
462{
463 monitor_child_pid = pid;
464}
465
466static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000467monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000468{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000469 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000470}
471
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000473monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474{
Damien Miller8f0bf232011-06-20 14:42:23 +1000475 close(pmonitor->m_recvfd);
476 pmonitor->m_recvfd = -1;
477
Damien Millerbe64d432003-05-14 19:31:12 +1000478 monitor_set_child_handler(pmonitor->m_pid);
479 signal(SIGHUP, &monitor_child_handler);
480 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000481 signal(SIGINT, &monitor_child_handler);
Damien Millerbe64d432003-05-14 19:31:12 +1000482
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483 if (compat20) {
484 mon_dispatch = mon_dispatch_postauth20;
485
486 /* Permit requests for moduli and signatures */
487 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
488 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
489 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000490 } else {
491 mon_dispatch = mon_dispatch_postauth15;
492 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
493 }
494 if (!no_pty_flag) {
495 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
496 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
497 }
498
499 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000500 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501}
502
503void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000504monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000505{
Damien Miller2d6b8332002-06-21 15:59:49 +1000506 if (options.compression) {
507 /* The member allocation is not visible, so sync it */
508 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
509 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510}
511
Damien Miller8f0bf232011-06-20 14:42:23 +1000512static int
513monitor_read_log(struct monitor *pmonitor)
514{
515 Buffer logmsg;
516 u_int len, level;
517 char *msg;
518
519 buffer_init(&logmsg);
520
521 /* Read length */
522 buffer_append_space(&logmsg, 4);
523 if (atomicio(read, pmonitor->m_log_recvfd,
524 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
525 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100526 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000527 debug("%s: child log fd closed", __func__);
528 close(pmonitor->m_log_recvfd);
529 pmonitor->m_log_recvfd = -1;
530 return -1;
531 }
532 fatal("%s: log fd read: %s", __func__, strerror(errno));
533 }
534 len = buffer_get_int(&logmsg);
535 if (len <= 4 || len > 8192)
536 fatal("%s: invalid log message length %u", __func__, len);
537
538 /* Read severity, message */
539 buffer_clear(&logmsg);
540 buffer_append_space(&logmsg, len);
541 if (atomicio(read, pmonitor->m_log_recvfd,
542 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
543 fatal("%s: log fd read: %s", __func__, strerror(errno));
544
545 /* Log it */
546 level = buffer_get_int(&logmsg);
547 msg = buffer_get_string(&logmsg, NULL);
548 if (log_level_name(level) == NULL)
549 fatal("%s: invalid log level %u (corrupted message?)",
550 __func__, level);
551 do_log2(level, "%s [preauth]", msg);
552
553 buffer_free(&logmsg);
554 xfree(msg);
555
556 return 0;
557}
558
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000559int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000560monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561 struct mon_table **pent)
562{
563 Buffer m;
564 int ret;
565 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000566 struct pollfd pfd[2];
567
568 for (;;) {
569 bzero(&pfd, sizeof(pfd));
570 pfd[0].fd = pmonitor->m_sendfd;
571 pfd[0].events = POLLIN;
572 pfd[1].fd = pmonitor->m_log_recvfd;
573 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000574 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
575 if (errno == EINTR || errno == EAGAIN)
576 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000577 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000578 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000579 if (pfd[1].revents) {
580 /*
581 * Drain all log messages before processing next
582 * monitor request.
583 */
584 monitor_read_log(pmonitor);
585 continue;
586 }
587 if (pfd[0].revents)
588 break; /* Continues below */
589 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
591 buffer_init(&m);
592
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000593 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594 type = buffer_get_char(&m);
595
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000596 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597
598 while (ent->f != NULL) {
599 if (ent->type == type)
600 break;
601 ent++;
602 }
603
604 if (ent->f != NULL) {
605 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000606 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000608 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609 buffer_free(&m);
610
611 /* The child may use this request only once, disable it */
612 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000613 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614 type);
615 ent->flags &= ~MON_PERMIT;
616 }
617
618 if (pent != NULL)
619 *pent = ent;
620
621 return ret;
622 }
623
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000624 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000625
626 /* NOTREACHED */
627 return (-1);
628}
629
630/* allowed key state */
631static int
632monitor_allowed_key(u_char *blob, u_int bloblen)
633{
634 /* make sure key is allowed */
635 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000636 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637 return (0);
638 return (1);
639}
640
641static void
642monitor_reset_key_state(void)
643{
644 /* reset state */
645 if (key_blob != NULL)
646 xfree(key_blob);
647 if (hostbased_cuser != NULL)
648 xfree(hostbased_cuser);
649 if (hostbased_chost != NULL)
650 xfree(hostbased_chost);
651 key_blob = NULL;
652 key_bloblen = 0;
653 key_blobtype = MM_NOKEY;
654 hostbased_cuser = NULL;
655 hostbased_chost = NULL;
656}
657
658int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000659mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660{
661 DH *dh;
662 int min, want, max;
663
664 min = buffer_get_int(m);
665 want = buffer_get_int(m);
666 max = buffer_get_int(m);
667
668 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000669 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670 /* We need to check here, too, in case the child got corrupted */
671 if (max < min || want < min || max < want)
672 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000673 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000674
675 buffer_clear(m);
676
677 dh = choose_dh(min, want, max);
678 if (dh == NULL) {
679 buffer_put_char(m, 0);
680 return (0);
681 } else {
682 /* Send first bignum */
683 buffer_put_char(m, 1);
684 buffer_put_bignum2(m, dh->p);
685 buffer_put_bignum2(m, dh->g);
686
687 DH_free(dh);
688 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000689 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690 return (0);
691}
692
693int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000694mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695{
696 Key *key;
697 u_char *p;
698 u_char *signature;
699 u_int siglen, datlen;
700 int keyid;
701
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000702 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703
704 keyid = buffer_get_int(m);
705 p = buffer_get_string(m, &datlen);
706
Damien Millera63128d2006-03-15 12:08:28 +1100707 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000708 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
709 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100710 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000711 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000712 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713
Ben Lindstromf67e0772002-06-06 20:58:19 +0000714 /* save session id, it will be passed on the first call */
715 if (session_id2_len == 0) {
716 session_id2_len = datlen;
717 session_id2 = xmalloc(session_id2_len);
718 memcpy(session_id2, p, session_id2_len);
719 }
720
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000721 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000722 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000724 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725
Ben Lindstromd5502182002-06-27 00:12:57 +0000726 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727
728 buffer_clear(m);
729 buffer_put_string(m, signature, siglen);
730
731 xfree(p);
732 xfree(signature);
733
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000734 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735
736 /* Turn on permissions for getpwnam */
737 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
738
739 return (0);
740}
741
742/* Retrieves the password entry and also checks if the user is permitted */
743
744int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000745mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000746{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000747 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748 struct passwd *pwent;
749 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000750 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000751
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000752 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753
754 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000755 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756
Darren Tuckerb09b6772004-06-22 15:06:46 +1000757 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758
Darren Tuckerb09b6772004-06-22 15:06:46 +1000759 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000760
Darren Tuckerb09b6772004-06-22 15:06:46 +1000761 authctxt->user = xstrdup(username);
762 setproctitle("%s [priv]", pwent ? username : "unknown");
763 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000764
765 buffer_clear(m);
766
767 if (pwent == NULL) {
768 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100769 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770 goto out;
771 }
772
773 allowed = 1;
774 authctxt->pw = pwent;
775 authctxt->valid = 1;
776
777 buffer_put_char(m, 1);
778 buffer_put_string(m, pwent, sizeof(struct passwd));
779 buffer_put_cstring(m, pwent->pw_name);
780 buffer_put_cstring(m, "*");
781 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000782#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000784#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000785 buffer_put_cstring(m, pwent->pw_dir);
786 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100787
788 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100789 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000790
791#define M_CP_STROPT(x) do { \
792 if (options.x != NULL) \
793 buffer_put_cstring(m, options.x); \
794 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000795#define M_CP_STRARRAYOPT(x, nx) do { \
796 for (i = 0; i < options.nx; i++) \
797 buffer_put_cstring(m, options.x[i]); \
798 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000799 /* See comment in servconf.h */
800 COPY_MATCH_STRING_OPTS();
801#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000802#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100803
804 /* Create valid auth method lists */
805 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
806 /*
807 * The monitor will continue long enough to let the child
808 * run to it's packet_disconnect(), but it must not allow any
809 * authentication to succeed.
810 */
811 debug("%s: no valid authentication method lists", __func__);
812 }
813
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000814 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000815 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000816
817 /* For SSHv1 allow authentication now */
818 if (!compat20)
819 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000820 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000821 /* Allow service/style information on the auth context */
822 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000823 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
824 }
Damien Millera33501b2002-05-08 12:24:42 +1000825#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000826 if (options.use_pam)
827 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000828#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000829
830 return (0);
831}
832
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000833int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000834{
835 char *banner;
836
837 buffer_clear(m);
838 banner = auth2_read_banner();
839 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000840 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000841
842 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000843 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000844
845 return (0);
846}
847
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000848int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000849mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000850{
851 monitor_permit_authentications(1);
852
853 authctxt->service = buffer_get_string(m, NULL);
854 authctxt->style = buffer_get_string(m, NULL);
855 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000856 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000857
858 if (strlen(authctxt->style) == 0) {
859 xfree(authctxt->style);
860 authctxt->style = NULL;
861 }
862
863 return (0);
864}
865
866int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000867mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000868{
869 static int call_count;
870 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000871 int authenticated;
872 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000873
874 passwd = buffer_get_string(m, &plen);
875 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000876 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000877 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000878 memset(passwd, 0, strlen(passwd));
879 xfree(passwd);
880
881 buffer_clear(m);
882 buffer_put_int(m, authenticated);
883
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000884 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000885 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886
887 call_count++;
888 if (plen == 0 && call_count == 1)
889 auth_method = "none";
890 else
891 auth_method = "password";
892
893 /* Causes monitor loop to terminate if authenticated */
894 return (authenticated);
895}
896
897#ifdef BSD_AUTH
898int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000899mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000900{
901 char *name, *infotxt;
902 u_int numprompts;
903 u_int *echo_on;
904 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100905 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000906
Damien Millerb7df3af2003-02-24 11:55:46 +1100907 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
908 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909
910 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100911 buffer_put_int(m, success);
912 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000913 buffer_put_cstring(m, prompts[0]);
914
Damien Millerb7df3af2003-02-24 11:55:46 +1100915 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000916 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000917
Damien Millerb7df3af2003-02-24 11:55:46 +1100918 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919 xfree(name);
920 xfree(infotxt);
921 xfree(prompts);
922 xfree(echo_on);
923 }
924
925 return (0);
926}
927
928int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000929mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000930{
931 char *response;
932 int authok;
933
934 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000935 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936
937 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000938 authok = options.challenge_response_authentication &&
939 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000941 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942 xfree(response);
943
944 buffer_clear(m);
945 buffer_put_int(m, authok);
946
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000947 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000948 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949
Damien Millera6e3f012012-11-04 23:21:40 +1100950 if (compat20)
Damien Miller15b05cf2012-12-03 09:53:20 +1100951 auth_method = "keyboard-interactive"; /* XXX auth_submethod */
Damien Millera6e3f012012-11-04 23:21:40 +1100952 else
953 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954
955 return (authok != 0);
956}
957#endif
958
959#ifdef SKEY
960int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000961mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000962{
963 struct skey skey;
964 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100965 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000967 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
968 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969
970 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100971 buffer_put_int(m, success);
972 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973 buffer_put_cstring(m, challenge);
974
Damien Millerb7df3af2003-02-24 11:55:46 +1100975 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000976 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000977
978 return (0);
979}
980
981int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000982mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000983{
984 char *response;
985 int authok;
986
987 response = buffer_get_string(m, NULL);
988
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000989 authok = (options.challenge_response_authentication &&
990 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000991 skey_haskey(authctxt->pw->pw_name) == 0 &&
992 skey_passcheck(authctxt->pw->pw_name, response) != -1);
993
994 xfree(response);
995
996 buffer_clear(m);
997 buffer_put_int(m, authok);
998
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000999 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001000 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001001
1002 auth_method = "skey";
1003
1004 return (authok != 0);
1005}
1006#endif
1007
Damien Miller79418552002-04-23 20:28:48 +10001008#ifdef USE_PAM
1009int
Darren Tucker7eda5922004-06-22 13:22:50 +10001010mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001011{
Damien Miller4e448a32003-05-14 15:11:48 +10001012 if (!options.use_pam)
1013 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1014
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001015 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001016
Damien Miller1f499fd2003-08-25 13:08:49 +10001017 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1018
Damien Miller79418552002-04-23 20:28:48 +10001019 return (0);
1020}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001021
Damien Miller1f499fd2003-08-25 13:08:49 +10001022int
Darren Tucker7eda5922004-06-22 13:22:50 +10001023mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001024{
1025 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001026
Damien Miller1f499fd2003-08-25 13:08:49 +10001027 if (!options.use_pam)
1028 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1029
1030 ret = do_pam_account();
1031
1032 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001033 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001034
Darren Tucker7eda5922004-06-22 13:22:50 +10001035 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001036
1037 return (ret);
1038}
1039
Damien Miller4f9f42a2003-05-10 19:28:02 +10001040static void *sshpam_ctxt, *sshpam_authok;
1041extern KbdintDevice sshpam_device;
1042
1043int
Darren Tucker7eda5922004-06-22 13:22:50 +10001044mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001045{
1046
1047 debug3("%s", __func__);
1048 authctxt->user = buffer_get_string(m, NULL);
1049 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1050 sshpam_authok = NULL;
1051 buffer_clear(m);
1052 if (sshpam_ctxt != NULL) {
1053 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1054 buffer_put_int(m, 1);
1055 } else {
1056 buffer_put_int(m, 0);
1057 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001058 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001059 return (0);
1060}
1061
1062int
Darren Tucker7eda5922004-06-22 13:22:50 +10001063mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001064{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001065 char *name = NULL, *info = NULL, **prompts = NULL;
1066 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001067 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001068
1069 debug3("%s", __func__);
1070 sshpam_authok = NULL;
1071 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1072 if (ret == 0 && num == 0)
1073 sshpam_authok = sshpam_ctxt;
1074 if (num > 1 || name == NULL || info == NULL)
1075 ret = -1;
1076 buffer_clear(m);
1077 buffer_put_int(m, ret);
1078 buffer_put_cstring(m, name);
1079 xfree(name);
1080 buffer_put_cstring(m, info);
1081 xfree(info);
1082 buffer_put_int(m, num);
1083 for (i = 0; i < num; ++i) {
1084 buffer_put_cstring(m, prompts[i]);
1085 xfree(prompts[i]);
1086 buffer_put_int(m, echo_on[i]);
1087 }
1088 if (prompts != NULL)
1089 xfree(prompts);
1090 if (echo_on != NULL)
1091 xfree(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001092 auth_method = "keyboard-interactive";
1093 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001094 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001095 return (0);
1096}
1097
1098int
Darren Tucker7eda5922004-06-22 13:22:50 +10001099mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001100{
1101 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001102 u_int i, num;
1103 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001104
1105 debug3("%s", __func__);
1106 sshpam_authok = NULL;
1107 num = buffer_get_int(m);
1108 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001109 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001110 for (i = 0; i < num; ++i)
1111 resp[i] = buffer_get_string(m, NULL);
1112 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1113 for (i = 0; i < num; ++i)
1114 xfree(resp[i]);
1115 xfree(resp);
1116 } else {
1117 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1118 }
1119 buffer_clear(m);
1120 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001121 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001122 auth_method = "keyboard-interactive";
1123 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001124 if (ret == 0)
1125 sshpam_authok = sshpam_ctxt;
1126 return (0);
1127}
1128
1129int
Darren Tucker7eda5922004-06-22 13:22:50 +10001130mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001131{
1132
1133 debug3("%s", __func__);
1134 (sshpam_device.free_ctx)(sshpam_ctxt);
1135 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001136 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001137 auth_method = "keyboard-interactive";
1138 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001139 return (sshpam_authok == sshpam_ctxt);
1140}
Damien Miller79418552002-04-23 20:28:48 +10001141#endif
1142
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001143int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001144mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001145{
1146 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001147 char *cuser, *chost;
1148 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001149 u_int bloblen;
1150 enum mm_keytype type = 0;
1151 int allowed = 0;
1152
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001153 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001154
1155 type = buffer_get_int(m);
1156 cuser = buffer_get_string(m, NULL);
1157 chost = buffer_get_string(m, NULL);
1158 blob = buffer_get_string(m, &bloblen);
1159
1160 key = key_from_blob(blob, bloblen);
1161
1162 if ((compat20 && type == MM_RSAHOSTKEY) ||
1163 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001164 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001165
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001166 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001167
Damien Miller3e3b5142003-11-17 21:13:40 +11001168 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001169 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001171 allowed = options.pubkey_authentication &&
1172 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001173 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001174 if (options.pubkey_authentication && allowed != 1)
1175 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001176 break;
1177 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001178 allowed = options.hostbased_authentication &&
1179 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001181 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001182 break;
1183 case MM_RSAHOSTKEY:
1184 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001185 allowed = options.rhosts_rsa_authentication &&
1186 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001187 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001188 if (options.rhosts_rsa_authentication && allowed != 1)
1189 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001190 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001191 break;
1192 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001193 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001194 break;
1195 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001196 }
Damien Miller00111382003-03-10 11:21:17 +11001197 if (key != NULL)
1198 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001199
1200 /* clear temporarily storage (used by verify) */
1201 monitor_reset_key_state();
1202
1203 if (allowed) {
1204 /* Save temporarily for comparison in verify */
1205 key_blob = blob;
1206 key_bloblen = bloblen;
1207 key_blobtype = type;
1208 hostbased_cuser = cuser;
1209 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001210 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001211 /* Log failed attempt */
Damien Miller15b05cf2012-12-03 09:53:20 +11001212 auth_log(authctxt, 0, 0, auth_method, NULL,
1213 compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001214 xfree(blob);
1215 xfree(cuser);
1216 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 }
1218
1219 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001220 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001221
1222 buffer_clear(m);
1223 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001224 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001226 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001227
1228 if (type == MM_RSAHOSTKEY)
1229 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1230
1231 return (0);
1232}
1233
1234static int
1235monitor_valid_userblob(u_char *data, u_int datalen)
1236{
1237 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001238 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001239 u_int len;
1240 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241
1242 buffer_init(&b);
1243 buffer_append(&b, data, datalen);
1244
1245 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001246 p = buffer_ptr(&b);
1247 len = buffer_len(&b);
1248 if ((session_id2 == NULL) ||
1249 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001250 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001251 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001252 buffer_consume(&b, session_id2_len);
1253 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001254 p = buffer_get_string(&b, &len);
1255 if ((session_id2 == NULL) ||
1256 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001257 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001258 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001259 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001260 }
1261 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1262 fail++;
1263 p = buffer_get_string(&b, NULL);
1264 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001265 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266 authctxt->user, p);
1267 fail++;
1268 }
1269 xfree(p);
1270 buffer_skip_string(&b);
1271 if (datafellows & SSH_BUG_PKAUTH) {
1272 if (!buffer_get_char(&b))
1273 fail++;
1274 } else {
1275 p = buffer_get_string(&b, NULL);
1276 if (strcmp("publickey", p) != 0)
1277 fail++;
1278 xfree(p);
1279 if (!buffer_get_char(&b))
1280 fail++;
1281 buffer_skip_string(&b);
1282 }
1283 buffer_skip_string(&b);
1284 if (buffer_len(&b) != 0)
1285 fail++;
1286 buffer_free(&b);
1287 return (fail == 0);
1288}
1289
1290static int
Damien Millera10f5612002-09-12 09:49:15 +10001291monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1292 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293{
1294 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001295 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001296 u_int len;
1297 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001298
1299 buffer_init(&b);
1300 buffer_append(&b, data, datalen);
1301
Ben Lindstromf67e0772002-06-06 20:58:19 +00001302 p = buffer_get_string(&b, &len);
1303 if ((session_id2 == NULL) ||
1304 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001305 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001306 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001307 xfree(p);
1308
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1310 fail++;
1311 p = buffer_get_string(&b, NULL);
1312 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001313 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314 authctxt->user, p);
1315 fail++;
1316 }
1317 xfree(p);
1318 buffer_skip_string(&b); /* service */
1319 p = buffer_get_string(&b, NULL);
1320 if (strcmp(p, "hostbased") != 0)
1321 fail++;
1322 xfree(p);
1323 buffer_skip_string(&b); /* pkalg */
1324 buffer_skip_string(&b); /* pkblob */
1325
1326 /* verify client host, strip trailing dot if necessary */
1327 p = buffer_get_string(&b, NULL);
1328 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1329 p[len - 1] = '\0';
1330 if (strcmp(p, chost) != 0)
1331 fail++;
1332 xfree(p);
1333
1334 /* verify client user */
1335 p = buffer_get_string(&b, NULL);
1336 if (strcmp(p, cuser) != 0)
1337 fail++;
1338 xfree(p);
1339
1340 if (buffer_len(&b) != 0)
1341 fail++;
1342 buffer_free(&b);
1343 return (fail == 0);
1344}
1345
1346int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001347mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348{
1349 Key *key;
1350 u_char *signature, *data, *blob;
1351 u_int signaturelen, datalen, bloblen;
1352 int verified = 0;
1353 int valid_data = 0;
1354
1355 blob = buffer_get_string(m, &bloblen);
1356 signature = buffer_get_string(m, &signaturelen);
1357 data = buffer_get_string(m, &datalen);
1358
1359 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001360 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001361 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362
1363 key = key_from_blob(blob, bloblen);
1364 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001365 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366
1367 switch (key_blobtype) {
1368 case MM_USERKEY:
1369 valid_data = monitor_valid_userblob(data, datalen);
1370 break;
1371 case MM_HOSTKEY:
1372 valid_data = monitor_valid_hostbasedblob(data, datalen,
1373 hostbased_cuser, hostbased_chost);
1374 break;
1375 default:
1376 valid_data = 0;
1377 break;
1378 }
1379 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001380 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381
1382 verified = key_verify(key, signature, signaturelen, data, datalen);
1383 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001384 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001385
1386 key_free(key);
1387 xfree(blob);
1388 xfree(signature);
1389 xfree(data);
1390
Ben Lindstrome1c09122002-06-23 00:38:24 +00001391 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1392
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001393 monitor_reset_key_state();
1394
1395 buffer_clear(m);
1396 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001397 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398
Darren Tuckerfbba7352006-11-07 23:16:08 +11001399 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400}
1401
1402static void
1403mm_record_login(Session *s, struct passwd *pw)
1404{
1405 socklen_t fromlen;
1406 struct sockaddr_storage from;
1407
1408 /*
1409 * Get IP address of client. If the connection is not a socket, let
1410 * the address be 0.0.0.0.
1411 */
1412 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001413 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001415 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001416 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001418 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001419 }
1420 }
1421 /* Record that there was a login on that tty from the remote host. */
1422 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001423 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001424 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425}
1426
1427static void
1428mm_session_close(Session *s)
1429{
Damien Miller04bd8b02003-05-25 14:38:33 +10001430 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001432 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433 session_pty_cleanup2(s);
1434 }
Damien Miller7207f642008-05-19 15:34:50 +10001435 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001436}
1437
1438int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001439mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001441 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442 Session *s;
1443 int res, fd0;
1444
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001445 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446
1447 buffer_clear(m);
1448 s = session_new();
1449 if (s == NULL)
1450 goto error;
1451 s->authctxt = authctxt;
1452 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001453 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001454 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1455 if (res == 0)
1456 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457 pty_setowner(authctxt->pw, s->tty);
1458
1459 buffer_put_int(m, 1);
1460 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461
1462 /* We need to trick ttyslot */
1463 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001464 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465
1466 mm_record_login(s, authctxt->pw);
1467
1468 /* Now we can close the file descriptor again */
1469 close(0);
1470
Darren Tucker09991742004-07-17 17:05:14 +10001471 /* send messages generated by record_login */
1472 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1473 buffer_clear(&loginmsg);
1474
1475 mm_request_send(sock, MONITOR_ANS_PTY, m);
1476
Damien Miller54fd7cf2007-09-17 12:04:08 +10001477 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1478 mm_send_fd(sock, s->ttyfd) == -1)
1479 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001480
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481 /* make sure nothing uses fd 0 */
1482 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001483 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001485 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486
1487 /* slave is not needed */
1488 close(s->ttyfd);
1489 s->ttyfd = s->ptyfd;
1490 /* no need to dup() because nobody closes ptyfd */
1491 s->ptymaster = s->ptyfd;
1492
Damien Miller9ab00b42006-08-05 12:40:11 +10001493 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494
1495 return (0);
1496
1497 error:
1498 if (s != NULL)
1499 mm_session_close(s);
1500 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001501 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 return (0);
1503}
1504
1505int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001506mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507{
1508 Session *s;
1509 char *tty;
1510
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001511 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512
1513 tty = buffer_get_string(m, NULL);
1514 if ((s = session_by_tty(tty)) != NULL)
1515 mm_session_close(s);
1516 buffer_clear(m);
1517 xfree(tty);
1518 return (0);
1519}
1520
1521int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001522mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001523{
1524 BIGNUM *p;
1525 int rsafail;
1526
1527 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001528 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529
1530 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001531 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532
1533 buffer_get_bignum2(m, p);
1534
1535 rsafail = ssh1_session_key(p);
1536
1537 buffer_clear(m);
1538 buffer_put_int(m, rsafail);
1539 buffer_put_bignum2(m, p);
1540
1541 BN_clear_free(p);
1542
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001543 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544
1545 /* Turn on permissions for sessid passing */
1546 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1547
1548 return (0);
1549}
1550
1551int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001552mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001553{
1554 int i;
1555
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001556 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001557
1558 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001559 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001560 for (i = 0; i < 16; i++)
1561 session_id[i] = buffer_get_char(m);
1562
1563 /* Turn on permissions for getpwnam */
1564 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1565
1566 return (0);
1567}
1568
1569int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001570mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001571{
1572 BIGNUM *client_n;
1573 Key *key = NULL;
1574 u_char *blob = NULL;
1575 u_int blen = 0;
1576 int allowed = 0;
1577
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001578 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001579
Damien Miller7a8f5b32006-03-31 23:14:23 +11001580 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001581 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001582 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001583 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001584 buffer_get_bignum2(m, client_n);
1585 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1586 BN_clear_free(client_n);
1587 }
1588 buffer_clear(m);
1589 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001590 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001591
1592 /* clear temporarily storage (used by generate challenge) */
1593 monitor_reset_key_state();
1594
1595 if (allowed && key != NULL) {
1596 key->type = KEY_RSA; /* cheat for key_to_blob */
1597 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001598 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001599 buffer_put_string(m, blob, blen);
1600
1601 /* Save temporarily for comparison in verify */
1602 key_blob = blob;
1603 key_bloblen = blen;
1604 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001605 }
Damien Miller00111382003-03-10 11:21:17 +11001606 if (key != NULL)
1607 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001608
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001609 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001610
1611 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1612 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1613 return (0);
1614}
1615
1616int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001617mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001618{
1619 Key *key = NULL;
1620 u_char *blob;
1621 u_int blen;
1622
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001623 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001624
1625 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001626 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001627 blob = buffer_get_string(m, &blen);
1628 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001629 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001631 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001633 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001634 if (key->type != KEY_RSA)
1635 fatal("%s: received bad key type %d", __func__, key->type);
1636 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001637 if (ssh1_challenge)
1638 BN_clear_free(ssh1_challenge);
1639 ssh1_challenge = auth_rsa_generate_challenge(key);
1640
1641 buffer_clear(m);
1642 buffer_put_bignum2(m, ssh1_challenge);
1643
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001644 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001645 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001646
1647 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001648
1649 xfree(blob);
1650 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001651 return (0);
1652}
1653
1654int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001655mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001656{
1657 Key *key = NULL;
1658 u_char *blob, *response;
1659 u_int blen, len;
1660 int success;
1661
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001662 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001663
1664 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001665 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001667 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001668
1669 blob = buffer_get_string(m, &blen);
1670 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001671 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001673 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001674 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001675 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001676 response = buffer_get_string(m, &len);
1677 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001678 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001679 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1680
Damien Miller00111382003-03-10 11:21:17 +11001681 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001682 key_free(key);
1683 xfree(response);
1684
1685 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1686
1687 /* reset state */
1688 BN_clear_free(ssh1_challenge);
1689 ssh1_challenge = NULL;
1690 monitor_reset_key_state();
1691
1692 buffer_clear(m);
1693 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001694 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001695
1696 return (success);
1697}
1698
1699int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001700mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001701{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001702 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001703 int res, status;
1704
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001705 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706
1707 /* The child is terminating */
1708 session_destroy_all(&mm_session_close);
1709
Darren Tucker52358d62008-03-11 22:58:25 +11001710#ifdef USE_PAM
1711 if (options.use_pam)
1712 sshpam_cleanup();
1713#endif
1714
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001715 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001716 if (errno != EINTR)
1717 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001718
1719 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1720
1721 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001722 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001723}
1724
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001725#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001726/* Report that an audit event occurred */
1727int
1728mm_answer_audit_event(int socket, Buffer *m)
1729{
1730 ssh_audit_event_t event;
1731
1732 debug3("%s entering", __func__);
1733
1734 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001735 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001736 case SSH_AUTH_FAIL_PUBKEY:
1737 case SSH_AUTH_FAIL_HOSTBASED:
1738 case SSH_AUTH_FAIL_GSSAPI:
1739 case SSH_LOGIN_EXCEED_MAXTRIES:
1740 case SSH_LOGIN_ROOT_DENIED:
1741 case SSH_CONNECTION_CLOSE:
1742 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001743 audit_event(event);
1744 break;
1745 default:
1746 fatal("Audit event type %d not permitted", event);
1747 }
1748
1749 return (0);
1750}
1751
1752int
1753mm_answer_audit_command(int socket, Buffer *m)
1754{
1755 u_int len;
1756 char *cmd;
1757
1758 debug3("%s entering", __func__);
1759 cmd = buffer_get_string(m, &len);
1760 /* sanity check command, if so how? */
1761 audit_run_command(cmd);
1762 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001763 return (0);
1764}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001765#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001766
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001767void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001768monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001769{
1770 if (compat20) {
1771 set_newkeys(MODE_IN);
1772 set_newkeys(MODE_OUT);
1773 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001774 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001775 packet_set_encryption_key(child_state.ssh1key,
1776 child_state.ssh1keylen, child_state.ssh1cipher);
1777 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001778 }
1779
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001780 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001781 packet_set_keycontext(MODE_OUT, child_state.keyout);
1782 xfree(child_state.keyout);
1783 packet_set_keycontext(MODE_IN, child_state.keyin);
1784 xfree(child_state.keyin);
1785
1786 if (!compat20) {
1787 packet_set_iv(MODE_OUT, child_state.ivout);
1788 xfree(child_state.ivout);
1789 packet_set_iv(MODE_IN, child_state.ivin);
1790 xfree(child_state.ivin);
1791 }
1792
1793 memcpy(&incoming_stream, &child_state.incoming,
1794 sizeof(incoming_stream));
1795 memcpy(&outgoing_stream, &child_state.outgoing,
1796 sizeof(outgoing_stream));
1797
1798 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001799 if (options.compression)
1800 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001801
1802 /* Network I/O buffers */
1803 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001804 buffer_clear(packet_get_input());
1805 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001806 memset(child_state.input, 0, child_state.ilen);
1807 xfree(child_state.input);
1808
Darren Tuckerf7288d72009-06-21 18:12:20 +10001809 buffer_clear(packet_get_output());
1810 buffer_append(packet_get_output(), child_state.output,
1811 child_state.olen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001812 memset(child_state.output, 0, child_state.olen);
1813 xfree(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001814
1815 /* Roaming */
1816 if (compat20)
1817 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001818}
1819
1820static Kex *
1821mm_get_kex(Buffer *m)
1822{
1823 Kex *kex;
1824 void *blob;
1825 u_int bloblen;
1826
Damien Miller07d86be2006-03-26 14:19:21 +11001827 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001828 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001829 if (session_id2 == NULL ||
1830 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001831 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001832 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001833 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001834 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001835 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001836 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001837 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001838 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001839 kex->server = 1;
1840 kex->hostkey_type = buffer_get_int(m);
1841 kex->kex_type = buffer_get_int(m);
1842 blob = buffer_get_string(m, &bloblen);
1843 buffer_init(&kex->my);
1844 buffer_append(&kex->my, blob, bloblen);
1845 xfree(blob);
1846 blob = buffer_get_string(m, &bloblen);
1847 buffer_init(&kex->peer);
1848 buffer_append(&kex->peer, blob, bloblen);
1849 xfree(blob);
1850 kex->done = 1;
1851 kex->flags = buffer_get_int(m);
1852 kex->client_version_string = buffer_get_string(m, NULL);
1853 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001854 kex->load_host_public_key=&get_hostkey_public_by_type;
1855 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001856 kex->host_key_index=&get_hostkey_index;
1857
1858 return (kex);
1859}
1860
1861/* This function requries careful sanity checking */
1862
1863void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001864mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001865{
1866 Buffer m;
1867 u_char *blob, *p;
1868 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001869 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001870 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001871
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001872 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001873
1874 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001875 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001876 if (!compat20) {
1877 child_state.ssh1protoflags = buffer_get_int(&m);
1878 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001879 child_state.ssh1key = buffer_get_string(&m,
1880 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001881 child_state.ivout = buffer_get_string(&m,
1882 &child_state.ivoutlen);
1883 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1884 goto skip;
1885 } else {
1886 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001887 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001888 }
1889
1890 blob = buffer_get_string(&m, &bloblen);
1891 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1892 xfree(blob);
1893
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001894 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001895 blob = buffer_get_string(&m, &bloblen);
1896 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1897 xfree(blob);
1898
1899 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001900 seqnr = buffer_get_int(&m);
1901 blocks = buffer_get_int64(&m);
1902 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001903 bytes = buffer_get_int64(&m);
1904 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001905 seqnr = buffer_get_int(&m);
1906 blocks = buffer_get_int64(&m);
1907 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001908 bytes = buffer_get_int64(&m);
1909 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001910
1911 skip:
1912 /* Get the key context */
1913 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1914 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1915
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001916 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001917 /* Get compression state */
1918 p = buffer_get_string(&m, &plen);
1919 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001920 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001921 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1922 xfree(p);
1923
1924 p = buffer_get_string(&m, &plen);
1925 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001926 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001927 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1928 xfree(p);
1929
1930 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001931 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001932 child_state.input = buffer_get_string(&m, &child_state.ilen);
1933 child_state.output = buffer_get_string(&m, &child_state.olen);
1934
Darren Tuckerc5564e12009-06-21 18:53:53 +10001935 /* Roaming */
1936 if (compat20) {
1937 child_state.sent_bytes = buffer_get_int64(&m);
1938 child_state.recv_bytes = buffer_get_int64(&m);
1939 }
1940
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001941 buffer_free(&m);
1942}
1943
1944
1945/* Allocation functions for zlib */
1946void *
1947mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1948{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001949 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001950 void *address;
1951
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001952 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001953 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1954
1955 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001956
1957 return (address);
1958}
1959
1960void
1961mm_zfree(struct mm_master *mm, void *address)
1962{
1963 mm_free(mm, address);
1964}
1965
1966void
1967mm_init_compression(struct mm_master *mm)
1968{
1969 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1970 outgoing_stream.zfree = (free_func)mm_zfree;
1971 outgoing_stream.opaque = mm;
1972
1973 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1974 incoming_stream.zfree = (free_func)mm_zfree;
1975 incoming_stream.opaque = mm;
1976}
1977
1978/* XXX */
1979
1980#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001981 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001982 fatal("fcntl(%d, F_SETFD)", x); \
1983} while (0)
1984
1985static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001986monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001987{
Damien Miller8f0bf232011-06-20 14:42:23 +10001988 int pair[2];
1989
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001990 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001991 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001992 FD_CLOSEONEXEC(pair[0]);
1993 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001994 mon->m_recvfd = pair[0];
1995 mon->m_sendfd = pair[1];
1996
1997 if (do_logfds) {
1998 if (pipe(pair) == -1)
1999 fatal("%s: pipe: %s", __func__, strerror(errno));
2000 FD_CLOSEONEXEC(pair[0]);
2001 FD_CLOSEONEXEC(pair[1]);
2002 mon->m_log_recvfd = pair[0];
2003 mon->m_log_sendfd = pair[1];
2004 } else
2005 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002006}
2007
2008#define MM_MEMSIZE 65536
2009
2010struct monitor *
2011monitor_init(void)
2012{
2013 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002014
Damien Miller07d86be2006-03-26 14:19:21 +11002015 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002016
Damien Miller8f0bf232011-06-20 14:42:23 +10002017 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002018
2019 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10002020 if (options.compression) {
2021 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2022 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002023
Damien Miller2d6b8332002-06-21 15:59:49 +10002024 /* Compression needs to share state across borders */
2025 mm_init_compression(mon->m_zlib);
2026 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002027
2028 return mon;
2029}
2030
2031void
2032monitor_reinit(struct monitor *mon)
2033{
Damien Miller8f0bf232011-06-20 14:42:23 +10002034 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002035}
Darren Tucker0efd1552003-08-26 11:49:55 +10002036
2037#ifdef GSSAPI
2038int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002039mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002040{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002041 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002042 OM_uint32 major;
2043 u_int len;
2044
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002045 goid.elements = buffer_get_string(m, &len);
2046 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002047
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002048 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002049
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002050 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002051
2052 buffer_clear(m);
2053 buffer_put_int(m, major);
2054
Damien Miller6fd6def2005-11-05 15:07:05 +11002055 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002056
2057 /* Now we have a context, enable the step */
2058 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2059
2060 return (0);
2061}
2062
2063int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002064mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002065{
2066 gss_buffer_desc in;
2067 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002068 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002069 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002070 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002071
Darren Tucker600ad8d2003-08-26 12:10:48 +10002072 in.value = buffer_get_string(m, &len);
2073 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002074 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
2075 xfree(in.value);
2076
2077 buffer_clear(m);
2078 buffer_put_int(m, major);
2079 buffer_put_string(m, out.value, out.length);
2080 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002081 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002082
2083 gss_release_buffer(&minor, &out);
2084
Damien Miller6fd6def2005-11-05 15:07:05 +11002085 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002086 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2087 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002088 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002089 }
2090 return (0);
2091}
2092
2093int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002094mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002095{
2096 gss_buffer_desc gssbuf, mic;
2097 OM_uint32 ret;
2098 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002099
Damien Miller0425d402003-11-17 22:18:21 +11002100 gssbuf.value = buffer_get_string(m, &len);
2101 gssbuf.length = len;
2102 mic.value = buffer_get_string(m, &len);
2103 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002104
Damien Miller0425d402003-11-17 22:18:21 +11002105 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002106
Damien Miller0425d402003-11-17 22:18:21 +11002107 xfree(gssbuf.value);
2108 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002109
Damien Miller0425d402003-11-17 22:18:21 +11002110 buffer_clear(m);
2111 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002112
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002113 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002114
Damien Miller0425d402003-11-17 22:18:21 +11002115 if (!GSS_ERROR(ret))
2116 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002117
Damien Miller0425d402003-11-17 22:18:21 +11002118 return (0);
2119}
2120
2121int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002122mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002123{
2124 int authenticated;
2125
2126 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2127
2128 buffer_clear(m);
2129 buffer_put_int(m, authenticated);
2130
2131 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002132 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002133
Damien Miller6fd6def2005-11-05 15:07:05 +11002134 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002135
2136 /* Monitor loop will terminate if authenticated */
2137 return (authenticated);
2138}
2139#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002140
2141#ifdef JPAKE
2142int
2143mm_answer_jpake_step1(int sock, Buffer *m)
2144{
2145 struct jpake_ctx *pctx;
2146 u_char *x3_proof, *x4_proof;
2147 u_int x3_proof_len, x4_proof_len;
2148
2149 if (!options.zero_knowledge_password_authentication)
2150 fatal("zero_knowledge_password_authentication disabled");
2151
2152 if (authctxt->jpake_ctx != NULL)
2153 fatal("%s: authctxt->jpake_ctx already set (%p)",
2154 __func__, authctxt->jpake_ctx);
2155 authctxt->jpake_ctx = pctx = jpake_new();
2156
2157 jpake_step1(pctx->grp,
2158 &pctx->server_id, &pctx->server_id_len,
2159 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2160 &x3_proof, &x3_proof_len,
2161 &x4_proof, &x4_proof_len);
2162
2163 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2164
2165 buffer_clear(m);
2166
2167 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2168 buffer_put_bignum2(m, pctx->g_x3);
2169 buffer_put_bignum2(m, pctx->g_x4);
2170 buffer_put_string(m, x3_proof, x3_proof_len);
2171 buffer_put_string(m, x4_proof, x4_proof_len);
2172
2173 debug3("%s: sending step1", __func__);
2174 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2175
2176 bzero(x3_proof, x3_proof_len);
2177 bzero(x4_proof, x4_proof_len);
2178 xfree(x3_proof);
2179 xfree(x4_proof);
2180
2181 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2182 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2183
2184 return 0;
2185}
2186
2187int
2188mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2189{
2190 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2191 char *hash_scheme, *salt;
2192
2193 if (pctx == NULL)
2194 fatal("%s: pctx == NULL", __func__);
2195
2196 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2197
2198 buffer_clear(m);
2199 /* pctx->s is sensitive, not returned to slave */
2200 buffer_put_cstring(m, hash_scheme);
2201 buffer_put_cstring(m, salt);
2202
2203 debug3("%s: sending pwdata", __func__);
2204 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2205
2206 bzero(hash_scheme, strlen(hash_scheme));
2207 bzero(salt, strlen(salt));
2208 xfree(hash_scheme);
2209 xfree(salt);
2210
2211 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2212
2213 return 0;
2214}
2215
2216int
2217mm_answer_jpake_step2(int sock, Buffer *m)
2218{
2219 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2220 u_char *x1_proof, *x2_proof, *x4_s_proof;
2221 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2222
2223 if (pctx == NULL)
2224 fatal("%s: pctx == NULL", __func__);
2225
2226 if ((pctx->g_x1 = BN_new()) == NULL ||
2227 (pctx->g_x2 = BN_new()) == NULL)
2228 fatal("%s: BN_new", __func__);
2229 buffer_get_bignum2(m, pctx->g_x1);
2230 buffer_get_bignum2(m, pctx->g_x2);
2231 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2232 x1_proof = buffer_get_string(m, &x1_proof_len);
2233 x2_proof = buffer_get_string(m, &x2_proof_len);
2234
2235 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2236 pctx->g_x1, pctx->g_x2, pctx->x4,
2237 pctx->client_id, pctx->client_id_len,
2238 pctx->server_id, pctx->server_id_len,
2239 x1_proof, x1_proof_len,
2240 x2_proof, x2_proof_len,
2241 &pctx->b,
2242 &x4_s_proof, &x4_s_proof_len);
2243
2244 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2245
2246 bzero(x1_proof, x1_proof_len);
2247 bzero(x2_proof, x2_proof_len);
2248 xfree(x1_proof);
2249 xfree(x2_proof);
2250
2251 buffer_clear(m);
2252
2253 buffer_put_bignum2(m, pctx->b);
2254 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2255
2256 debug3("%s: sending step2", __func__);
2257 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2258
2259 bzero(x4_s_proof, x4_s_proof_len);
2260 xfree(x4_s_proof);
2261
2262 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2263
2264 return 0;
2265}
2266
2267int
2268mm_answer_jpake_key_confirm(int sock, Buffer *m)
2269{
2270 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2271 u_char *x2_s_proof;
2272 u_int x2_s_proof_len;
2273
2274 if (pctx == NULL)
2275 fatal("%s: pctx == NULL", __func__);
2276
2277 if ((pctx->a = BN_new()) == NULL)
2278 fatal("%s: BN_new", __func__);
2279 buffer_get_bignum2(m, pctx->a);
2280 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2281
2282 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2283 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2284 pctx->server_id, pctx->server_id_len,
2285 pctx->client_id, pctx->client_id_len,
2286 session_id2, session_id2_len,
2287 x2_s_proof, x2_s_proof_len,
2288 &pctx->k,
2289 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2290
2291 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2292
2293 bzero(x2_s_proof, x2_s_proof_len);
2294 buffer_clear(m);
2295
2296 /* pctx->k is sensitive, not sent */
2297 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2298
2299 debug3("%s: sending confirmation hash", __func__);
2300 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2301
2302 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2303
2304 return 0;
2305}
2306
2307int
2308mm_answer_jpake_check_confirm(int sock, Buffer *m)
2309{
2310 int authenticated = 0;
2311 u_char *peer_confirm_hash;
2312 u_int peer_confirm_hash_len;
2313 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2314
2315 if (pctx == NULL)
2316 fatal("%s: pctx == NULL", __func__);
2317
2318 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2319
2320 authenticated = jpake_check_confirm(pctx->k,
2321 pctx->client_id, pctx->client_id_len,
2322 session_id2, session_id2_len,
2323 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2324
2325 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2326
2327 bzero(peer_confirm_hash, peer_confirm_hash_len);
2328 xfree(peer_confirm_hash);
2329
2330 buffer_clear(m);
2331 buffer_put_int(m, authenticated);
2332
2333 debug3("%s: sending result %d", __func__, authenticated);
2334 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2335
2336 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2337
2338 auth_method = "jpake-01@openssh.com";
2339 return authenticated;
2340}
2341
2342#endif /* JPAKE */