blob: 6e97def1cfd8503ae2bac14570107003741090a1 [file] [log] [blame]
djm@openbsd.org523463a2015-02-16 22:13:32 +00001/* $OpenBSD: monitor.c,v 1.144 2015/02/16 22:13:32 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110033#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000034
Darren Tucker39972492006-07-12 22:22:46 +100035#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100036#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110037#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110038#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110039#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100040#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110041#include <signal.h>
millert@openbsd.orgfd368342015-02-06 23:21:59 +000042#include <stdint.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100043#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100044#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100045#include <stdarg.h>
46#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100047#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100048#ifdef HAVE_POLL_H
49#include <poll.h>
50#else
51# ifdef HAVE_SYS_POLL_H
52# include <sys/poll.h>
53# endif
54#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000055
56#ifdef SKEY
57#include <skey.h>
58#endif
59
Damien Miller1f0311c2014-05-15 14:24:09 +100060#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110061#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100062#endif
Damien Miller03e20032006-03-15 11:16:59 +110063
Damien Millerb84886b2008-05-19 15:05:07 +100064#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100065#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100066#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000067#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100068#include "key.h"
69#include "buffer.h"
70#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100072#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000073#include "kex.h"
74#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000075#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
76#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000077#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000078#define TARGET_OS_MAC 1
79#else
80#include "zlib.h"
81#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082#include "packet.h"
83#include "auth-options.h"
84#include "sshpty.h"
85#include "channels.h"
86#include "session.h"
87#include "sshlogin.h"
88#include "canohost.h"
89#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100090#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091#include "servconf.h"
92#include "monitor.h"
93#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100094#ifdef GSSAPI
95#include "ssh-gss.h"
96#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000097#include "monitor_wrap.h"
98#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000099#include "compat.h"
100#include "ssh2.h"
Darren Tuckerb422afa2009-06-21 18:58:46 +1000101#include "roaming.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000102#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000103#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000104#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105
Darren Tucker0efd1552003-08-26 11:49:55 +1000106#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000107static Gssctxt *gsscontext = NULL;
108#endif
109
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000110/* Imports */
111extern ServerOptions options;
112extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000114extern Buffer auth_debug;
115extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000116extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000117
118/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000119static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120
Damien Miller469954d2003-06-18 20:25:33 +1000121/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000122
123int mm_answer_moduli(int, Buffer *);
124int mm_answer_sign(int, Buffer *);
125int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000126int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000127int mm_answer_authserv(int, Buffer *);
128int mm_answer_authpassword(int, Buffer *);
129int mm_answer_bsdauthquery(int, Buffer *);
130int mm_answer_bsdauthrespond(int, Buffer *);
131int mm_answer_skeyquery(int, Buffer *);
132int mm_answer_skeyrespond(int, Buffer *);
133int mm_answer_keyallowed(int, Buffer *);
134int mm_answer_keyverify(int, Buffer *);
135int mm_answer_pty(int, Buffer *);
136int mm_answer_pty_cleanup(int, Buffer *);
137int mm_answer_term(int, Buffer *);
138int mm_answer_rsa_keyallowed(int, Buffer *);
139int mm_answer_rsa_challenge(int, Buffer *);
140int mm_answer_rsa_response(int, Buffer *);
141int mm_answer_sesskey(int, Buffer *);
142int mm_answer_sessid(int, Buffer *);
143
Damien Miller79418552002-04-23 20:28:48 +1000144#ifdef USE_PAM
145int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000146int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000147int mm_answer_pam_init_ctx(int, Buffer *);
148int mm_answer_pam_query(int, Buffer *);
149int mm_answer_pam_respond(int, Buffer *);
150int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000151#endif
152
Darren Tucker0efd1552003-08-26 11:49:55 +1000153#ifdef GSSAPI
154int mm_answer_gss_setup_ctx(int, Buffer *);
155int mm_answer_gss_accept_ctx(int, Buffer *);
156int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100157int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000158#endif
Damien Miller25162f22002-09-12 09:47:29 +1000159
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100160#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100161int mm_answer_audit_event(int, Buffer *);
162int mm_answer_audit_command(int, Buffer *);
163#endif
164
Damien Miller8f0bf232011-06-20 14:42:23 +1000165static int monitor_read_log(struct monitor *);
166
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000168
169#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000170static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
Damien Miller1f0311c2014-05-15 14:24:09 +1000171#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172
173/* local state for key verify */
174static u_char *key_blob = NULL;
175static u_int key_bloblen = 0;
176static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000177static char *hostbased_cuser = NULL;
178static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000179static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100180static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000181static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000182static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000183static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000184
185struct mon_table {
186 enum monitor_reqtype type;
187 int flags;
188 int (*f)(int, Buffer *);
189};
190
191#define MON_ISAUTH 0x0004 /* Required for Authentication */
192#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
193#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100194#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195
196#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
197
198#define MON_PERMIT 0x1000 /* Request is permitted */
199
200struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000201#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000203#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
205 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
206 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000207 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000209#ifdef USE_PAM
210 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000211 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000212 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
213 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
214 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
215 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000216#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100217#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100218 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100219#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220#ifdef BSD_AUTH
221 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100222 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223#endif
224#ifdef SKEY
225 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
226 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
227#endif
228 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
229 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000230#ifdef GSSAPI
231 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
232 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
233 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100234 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000235#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000236 {0, 0, NULL}
237};
238
239struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000240#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000241 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000242#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
244 {MONITOR_REQ_PTY, 0, mm_answer_pty},
245 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
246 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100247#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100248 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
249 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
250#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000251 {0, 0, NULL}
252};
253
254struct mon_table mon_dispatch_proto15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000255#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000256 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
257 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
258 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
259 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100260 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
261 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000262 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
263 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
264#ifdef BSD_AUTH
265 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100266 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000267#endif
268#ifdef SKEY
269 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
270 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
271#endif
Damien Millera33501b2002-05-08 12:24:42 +1000272#ifdef USE_PAM
273 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000274 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000275 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
276 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
277 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
278 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000279#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100280#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100281 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100282#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000283#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000284 {0, 0, NULL}
285};
286
287struct mon_table mon_dispatch_postauth15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000288#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000289 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
290 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
291 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100292#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100293 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000294 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100295#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000296#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000297 {0, 0, NULL}
298};
299
300struct mon_table *mon_dispatch;
301
302/* Specifies if a certain message is allowed at the moment */
303
304static void
305monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
306{
307 while (ent->f != NULL) {
308 if (ent->type == type) {
309 ent->flags &= ~MON_PERMIT;
310 ent->flags |= permit ? MON_PERMIT : 0;
311 return;
312 }
313 ent++;
314 }
315}
316
317static void
318monitor_permit_authentications(int permit)
319{
320 struct mon_table *ent = mon_dispatch;
321
322 while (ent->f != NULL) {
323 if (ent->flags & MON_AUTH) {
324 ent->flags &= ~MON_PERMIT;
325 ent->flags |= permit ? MON_PERMIT : 0;
326 }
327 ent++;
328 }
329}
330
Darren Tucker3e33cec2003-10-02 16:12:36 +1000331void
332monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000333{
334 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100335 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336
337 debug3("preauth child monitor started");
338
Damien Miller8f0bf232011-06-20 14:42:23 +1000339 close(pmonitor->m_recvfd);
340 close(pmonitor->m_log_sendfd);
341 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
342
Darren Tucker3e33cec2003-10-02 16:12:36 +1000343 authctxt = _authctxt;
344 memset(authctxt, 0, sizeof(*authctxt));
345
Darren Tuckerde0de392005-03-31 23:52:04 +1000346 authctxt->loginmsg = &loginmsg;
347
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348 if (compat20) {
349 mon_dispatch = mon_dispatch_proto20;
350
351 /* Permit requests for moduli and signatures */
352 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
353 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
354 } else {
355 mon_dispatch = mon_dispatch_proto15;
356
357 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
358 }
359
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360 /* The first few requests do not require asynchronous access */
361 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100362 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100363 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100364 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100365 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100366
367 /* Special handling for multiple required authentications */
368 if (options.num_auth_methods != 0) {
369 if (!compat20)
370 fatal("AuthenticationMethods is not supported"
371 "with SSH protocol 1");
372 if (authenticated &&
373 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000374 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100375 debug3("%s: method %s: partial", __func__,
376 auth_method);
377 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100378 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100379 }
380 }
381
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000382 if (authenticated) {
383 if (!(ent->flags & MON_AUTHDECIDE))
384 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000385 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386 if (authctxt->pw->pw_uid == 0 &&
387 !auth_root_allowed(auth_method))
388 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000389#ifdef USE_PAM
390 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100391 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000392 Buffer m;
393
394 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100395 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000396 MONITOR_REQ_PAM_ACCOUNT, &m);
397 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
398 buffer_free(&m);
399 }
400#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000401 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100402 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100403 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000404 auth_method, auth_submethod);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000405 if (!authenticated)
406 authctxt->failures++;
407 }
408 }
409
410 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000411 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100412 if (strcmp(auth_method, "unknown") == 0)
413 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000414
415 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000416 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000417
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000418 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000419
Damien Miller6a1937e2012-12-12 10:44:38 +1100420 /* Drain any buffered messages from the child */
421 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
422 ;
423
Damien Miller8f0bf232011-06-20 14:42:23 +1000424 close(pmonitor->m_sendfd);
425 close(pmonitor->m_log_recvfd);
426 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000427}
428
Damien Millerbe64d432003-05-14 19:31:12 +1000429static void
430monitor_set_child_handler(pid_t pid)
431{
432 monitor_child_pid = pid;
433}
434
435static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000436monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000437{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000438 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000439}
440
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000441void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000442monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000443{
Damien Miller8f0bf232011-06-20 14:42:23 +1000444 close(pmonitor->m_recvfd);
445 pmonitor->m_recvfd = -1;
446
Damien Millerbe64d432003-05-14 19:31:12 +1000447 monitor_set_child_handler(pmonitor->m_pid);
448 signal(SIGHUP, &monitor_child_handler);
449 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000450 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000451#ifdef SIGXFSZ
452 signal(SIGXFSZ, SIG_IGN);
453#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000454
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000455 if (compat20) {
456 mon_dispatch = mon_dispatch_postauth20;
457
458 /* Permit requests for moduli and signatures */
459 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
460 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
461 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462 } else {
463 mon_dispatch = mon_dispatch_postauth15;
464 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
465 }
466 if (!no_pty_flag) {
467 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
468 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
469 }
470
471 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000472 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473}
474
475void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000476monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000477{
Damien Miller2d6b8332002-06-21 15:59:49 +1000478 if (options.compression) {
479 /* The member allocation is not visible, so sync it */
480 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
481 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000482}
483
markus@openbsd.org091c3022015-01-19 19:52:16 +0000484/* Allocation functions for zlib */
485static void *
486mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
487{
488 size_t len = (size_t) size * ncount;
489 void *address;
490
millert@openbsd.orgfd368342015-02-06 23:21:59 +0000491 if (len == 0 || ncount > SIZE_MAX / size)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000492 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
493
494 address = mm_malloc(mm, len);
495
496 return (address);
497}
498
499static void
500mm_zfree(struct mm_master *mm, void *address)
501{
502 mm_free(mm, address);
503}
504
Damien Miller8f0bf232011-06-20 14:42:23 +1000505static int
506monitor_read_log(struct monitor *pmonitor)
507{
508 Buffer logmsg;
509 u_int len, level;
510 char *msg;
511
512 buffer_init(&logmsg);
513
514 /* Read length */
515 buffer_append_space(&logmsg, 4);
516 if (atomicio(read, pmonitor->m_log_recvfd,
517 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
518 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100519 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000520 debug("%s: child log fd closed", __func__);
521 close(pmonitor->m_log_recvfd);
522 pmonitor->m_log_recvfd = -1;
523 return -1;
524 }
525 fatal("%s: log fd read: %s", __func__, strerror(errno));
526 }
527 len = buffer_get_int(&logmsg);
528 if (len <= 4 || len > 8192)
529 fatal("%s: invalid log message length %u", __func__, len);
530
531 /* Read severity, message */
532 buffer_clear(&logmsg);
533 buffer_append_space(&logmsg, len);
534 if (atomicio(read, pmonitor->m_log_recvfd,
535 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
536 fatal("%s: log fd read: %s", __func__, strerror(errno));
537
538 /* Log it */
539 level = buffer_get_int(&logmsg);
540 msg = buffer_get_string(&logmsg, NULL);
541 if (log_level_name(level) == NULL)
542 fatal("%s: invalid log level %u (corrupted message?)",
543 __func__, level);
544 do_log2(level, "%s [preauth]", msg);
545
546 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000547 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000548
549 return 0;
550}
551
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000553monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554 struct mon_table **pent)
555{
556 Buffer m;
557 int ret;
558 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000559 struct pollfd pfd[2];
560
561 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100562 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000563 pfd[0].fd = pmonitor->m_sendfd;
564 pfd[0].events = POLLIN;
565 pfd[1].fd = pmonitor->m_log_recvfd;
566 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000567 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
568 if (errno == EINTR || errno == EAGAIN)
569 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000570 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000571 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000572 if (pfd[1].revents) {
573 /*
574 * Drain all log messages before processing next
575 * monitor request.
576 */
577 monitor_read_log(pmonitor);
578 continue;
579 }
580 if (pfd[0].revents)
581 break; /* Continues below */
582 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583
584 buffer_init(&m);
585
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000586 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587 type = buffer_get_char(&m);
588
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000589 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
591 while (ent->f != NULL) {
592 if (ent->type == type)
593 break;
594 ent++;
595 }
596
597 if (ent->f != NULL) {
598 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000599 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000601 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602 buffer_free(&m);
603
604 /* The child may use this request only once, disable it */
605 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000606 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607 type);
608 ent->flags &= ~MON_PERMIT;
609 }
610
611 if (pent != NULL)
612 *pent = ent;
613
614 return ret;
615 }
616
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000617 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618
619 /* NOTREACHED */
620 return (-1);
621}
622
623/* allowed key state */
624static int
625monitor_allowed_key(u_char *blob, u_int bloblen)
626{
627 /* make sure key is allowed */
628 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000629 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630 return (0);
631 return (1);
632}
633
634static void
635monitor_reset_key_state(void)
636{
637 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000638 free(key_blob);
639 free(hostbased_cuser);
640 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000641 key_blob = NULL;
642 key_bloblen = 0;
643 key_blobtype = MM_NOKEY;
644 hostbased_cuser = NULL;
645 hostbased_chost = NULL;
646}
647
Damien Miller1f0311c2014-05-15 14:24:09 +1000648#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000650mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000651{
652 DH *dh;
653 int min, want, max;
654
655 min = buffer_get_int(m);
656 want = buffer_get_int(m);
657 max = buffer_get_int(m);
658
659 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000660 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661 /* We need to check here, too, in case the child got corrupted */
662 if (max < min || want < min || max < want)
663 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000664 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000665
666 buffer_clear(m);
667
668 dh = choose_dh(min, want, max);
669 if (dh == NULL) {
670 buffer_put_char(m, 0);
671 return (0);
672 } else {
673 /* Send first bignum */
674 buffer_put_char(m, 1);
675 buffer_put_bignum2(m, dh->p);
676 buffer_put_bignum2(m, dh->g);
677
678 DH_free(dh);
679 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000680 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000681 return (0);
682}
Damien Miller1f0311c2014-05-15 14:24:09 +1000683#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684
685int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000686mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000687{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000688 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000689 extern int auth_sock; /* XXX move to state struct? */
690 struct sshkey *key;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000691 struct sshbuf *sigbuf;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692 u_char *p;
693 u_char *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000694 size_t datlen, siglen;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000695 int r, keyid, is_proof = 0;
696 const char proof_req[] = "hostkeys-prove@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000697
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000698 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699
djm@openbsd.org141efe42015-01-14 20:05:27 +0000700 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
701 (r = sshbuf_get_string(m, &p, &datlen)) != 0)
702 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703
Damien Millera63128d2006-03-15 12:08:28 +1100704 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000705 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
706 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000707 *
708 * Otherwise, verify the signature request is for a hostkey
709 * proof.
710 *
711 * XXX perform similar check for KEX signature requests too?
712 * it's not trivial, since what is signed is the hash, rather
713 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100714 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000715 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
716 /*
717 * Construct expected hostkey proof and compare it to what
718 * the client sent us.
719 */
720 if (session_id2_len == 0) /* hostkeys is never first */
721 fatal("%s: bad data length: %zu", __func__, datlen);
722 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
723 fatal("%s: no hostkey for index %d", __func__, keyid);
724 if ((sigbuf = sshbuf_new()) == NULL)
725 fatal("%s: sshbuf_new", __func__);
726 if ((r = sshbuf_put_string(sigbuf, session_id2,
727 session_id2_len) != 0) ||
728 (r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
729 (r = sshkey_puts(key, sigbuf)) != 0)
730 fatal("%s: couldn't prepare private key "
731 "proof buffer: %s", __func__, ssh_err(r));
732 if (datlen != sshbuf_len(sigbuf) ||
733 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
734 fatal("%s: bad data length: %zu, hostkey proof len %zu",
735 __func__, datlen, sshbuf_len(sigbuf));
736 sshbuf_free(sigbuf);
737 is_proof = 1;
738 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739
Ben Lindstromf67e0772002-06-06 20:58:19 +0000740 /* save session id, it will be passed on the first call */
741 if (session_id2_len == 0) {
742 session_id2_len = datlen;
743 session_id2 = xmalloc(session_id2_len);
744 memcpy(session_id2, p, session_id2_len);
745 }
746
Damien Miller85b45e02013-07-20 13:21:52 +1000747 if ((key = get_hostkey_by_index(keyid)) != NULL) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000748 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen,
749 datafellows)) != 0)
750 fatal("%s: sshkey_sign failed: %s",
751 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000752 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000753 auth_sock > 0) {
754 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
755 p, datlen, datafellows)) != 0) {
756 fatal("%s: ssh_agent_sign failed: %s",
757 __func__, ssh_err(r));
758 }
Damien Miller85b45e02013-07-20 13:21:52 +1000759 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000760 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761
djm@openbsd.org523463a2015-02-16 22:13:32 +0000762 debug3("%s: %s signature %p(%zu)", __func__,
763 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000764
djm@openbsd.org141efe42015-01-14 20:05:27 +0000765 sshbuf_reset(m);
766 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
767 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768
Darren Tuckera627d422013-06-02 07:31:17 +1000769 free(p);
770 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000771
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000772 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000773
774 /* Turn on permissions for getpwnam */
775 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
776
777 return (0);
778}
779
780/* Retrieves the password entry and also checks if the user is permitted */
781
782int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000783mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000785 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000786 struct passwd *pwent;
787 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000788 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000790 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791
792 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000793 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000794
Darren Tuckerb09b6772004-06-22 15:06:46 +1000795 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000796
Darren Tuckerb09b6772004-06-22 15:06:46 +1000797 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000798
Darren Tuckerb09b6772004-06-22 15:06:46 +1000799 authctxt->user = xstrdup(username);
800 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000801 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000802
803 buffer_clear(m);
804
805 if (pwent == NULL) {
806 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100807 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000808 goto out;
809 }
810
811 allowed = 1;
812 authctxt->pw = pwent;
813 authctxt->valid = 1;
814
815 buffer_put_char(m, 1);
816 buffer_put_string(m, pwent, sizeof(struct passwd));
817 buffer_put_cstring(m, pwent->pw_name);
818 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000819#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000820 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000821#endif
822#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000824#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000825 buffer_put_cstring(m, pwent->pw_dir);
826 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100827
828 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100829 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000830
831#define M_CP_STROPT(x) do { \
832 if (options.x != NULL) \
833 buffer_put_cstring(m, options.x); \
834 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000835#define M_CP_STRARRAYOPT(x, nx) do { \
836 for (i = 0; i < options.nx; i++) \
837 buffer_put_cstring(m, options.x[i]); \
838 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000839 /* See comment in servconf.h */
840 COPY_MATCH_STRING_OPTS();
841#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000842#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100843
844 /* Create valid auth method lists */
845 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
846 /*
847 * The monitor will continue long enough to let the child
848 * run to it's packet_disconnect(), but it must not allow any
849 * authentication to succeed.
850 */
851 debug("%s: no valid authentication method lists", __func__);
852 }
853
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000854 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000855 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000856
857 /* For SSHv1 allow authentication now */
858 if (!compat20)
859 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000860 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000861 /* Allow service/style information on the auth context */
862 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000863 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
864 }
Damien Millera33501b2002-05-08 12:24:42 +1000865#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000866 if (options.use_pam)
867 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000868#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000869
870 return (0);
871}
872
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000873int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000874{
875 char *banner;
876
877 buffer_clear(m);
878 banner = auth2_read_banner();
879 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000880 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000881 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000882
883 return (0);
884}
885
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000887mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000888{
889 monitor_permit_authentications(1);
890
891 authctxt->service = buffer_get_string(m, NULL);
892 authctxt->style = buffer_get_string(m, NULL);
893 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000894 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000895
896 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000897 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000898 authctxt->style = NULL;
899 }
900
901 return (0);
902}
903
904int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000905mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000906{
907 static int call_count;
908 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000909 int authenticated;
910 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911
912 passwd = buffer_get_string(m, &plen);
913 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000914 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000915 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100916 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000917 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000918
919 buffer_clear(m);
920 buffer_put_int(m, authenticated);
921
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000922 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000923 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000924
925 call_count++;
926 if (plen == 0 && call_count == 1)
927 auth_method = "none";
928 else
929 auth_method = "password";
930
931 /* Causes monitor loop to terminate if authenticated */
932 return (authenticated);
933}
934
935#ifdef BSD_AUTH
936int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000937mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938{
939 char *name, *infotxt;
940 u_int numprompts;
941 u_int *echo_on;
942 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100943 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
Damien Millerb7df3af2003-02-24 11:55:46 +1100945 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
946 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000947
948 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100949 buffer_put_int(m, success);
950 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000951 buffer_put_cstring(m, prompts[0]);
952
Damien Millerb7df3af2003-02-24 11:55:46 +1100953 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000954 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000955
Damien Millerb7df3af2003-02-24 11:55:46 +1100956 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000957 free(name);
958 free(infotxt);
959 free(prompts);
960 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961 }
962
963 return (0);
964}
965
966int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000967mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968{
969 char *response;
970 int authok;
971
972 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000973 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000974
975 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000976 authok = options.challenge_response_authentication &&
977 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000979 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000980 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981
982 buffer_clear(m);
983 buffer_put_int(m, authok);
984
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000985 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000986 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
Damien Miller91a55f22013-04-23 15:18:10 +1000988 if (compat20) {
989 auth_method = "keyboard-interactive";
990 auth_submethod = "bsdauth";
991 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100992 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993
994 return (authok != 0);
995}
996#endif
997
998#ifdef SKEY
999int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001000mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001001{
1002 struct skey skey;
1003 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +11001004 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005
Darren Tucker06a8cfe2004-04-14 17:24:30 +10001006 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
1007 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001008
1009 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +11001010 buffer_put_int(m, success);
1011 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001012 buffer_put_cstring(m, challenge);
1013
Damien Millerb7df3af2003-02-24 11:55:46 +11001014 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001015 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016
1017 return (0);
1018}
1019
1020int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001021mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022{
1023 char *response;
1024 int authok;
1025
1026 response = buffer_get_string(m, NULL);
1027
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001028 authok = (options.challenge_response_authentication &&
1029 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001030 skey_haskey(authctxt->pw->pw_name) == 0 &&
1031 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1032
Darren Tuckerf60845f2013-06-02 08:07:31 +10001033 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034
1035 buffer_clear(m);
1036 buffer_put_int(m, authok);
1037
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001038 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001039 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001040
1041 auth_method = "skey";
1042
1043 return (authok != 0);
1044}
1045#endif
1046
Damien Miller79418552002-04-23 20:28:48 +10001047#ifdef USE_PAM
1048int
Darren Tucker7eda5922004-06-22 13:22:50 +10001049mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001050{
Damien Miller4e448a32003-05-14 15:11:48 +10001051 if (!options.use_pam)
1052 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1053
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001054 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001055
Damien Miller1f499fd2003-08-25 13:08:49 +10001056 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1057
Damien Miller79418552002-04-23 20:28:48 +10001058 return (0);
1059}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001060
Damien Miller1f499fd2003-08-25 13:08:49 +10001061int
Darren Tucker7eda5922004-06-22 13:22:50 +10001062mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001063{
1064 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001065
Damien Miller1f499fd2003-08-25 13:08:49 +10001066 if (!options.use_pam)
1067 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1068
1069 ret = do_pam_account();
1070
1071 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001072 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001073
Darren Tucker7eda5922004-06-22 13:22:50 +10001074 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001075
1076 return (ret);
1077}
1078
Damien Miller4f9f42a2003-05-10 19:28:02 +10001079static void *sshpam_ctxt, *sshpam_authok;
1080extern KbdintDevice sshpam_device;
1081
1082int
Darren Tucker7eda5922004-06-22 13:22:50 +10001083mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001084{
1085
1086 debug3("%s", __func__);
1087 authctxt->user = buffer_get_string(m, NULL);
1088 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1089 sshpam_authok = NULL;
1090 buffer_clear(m);
1091 if (sshpam_ctxt != NULL) {
1092 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1093 buffer_put_int(m, 1);
1094 } else {
1095 buffer_put_int(m, 0);
1096 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001097 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001098 return (0);
1099}
1100
1101int
Darren Tucker7eda5922004-06-22 13:22:50 +10001102mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001103{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001104 char *name = NULL, *info = NULL, **prompts = NULL;
1105 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001106 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001107
1108 debug3("%s", __func__);
1109 sshpam_authok = NULL;
1110 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1111 if (ret == 0 && num == 0)
1112 sshpam_authok = sshpam_ctxt;
1113 if (num > 1 || name == NULL || info == NULL)
1114 ret = -1;
1115 buffer_clear(m);
1116 buffer_put_int(m, ret);
1117 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001118 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001119 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001120 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001121 buffer_put_int(m, num);
1122 for (i = 0; i < num; ++i) {
1123 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001124 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001125 buffer_put_int(m, echo_on[i]);
1126 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001127 free(prompts);
1128 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001129 auth_method = "keyboard-interactive";
1130 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001131 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001132 return (0);
1133}
1134
1135int
Darren Tucker7eda5922004-06-22 13:22:50 +10001136mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001137{
1138 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001139 u_int i, num;
1140 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001141
1142 debug3("%s", __func__);
1143 sshpam_authok = NULL;
1144 num = buffer_get_int(m);
1145 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001146 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001147 for (i = 0; i < num; ++i)
1148 resp[i] = buffer_get_string(m, NULL);
1149 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1150 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001151 free(resp[i]);
1152 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001153 } else {
1154 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1155 }
1156 buffer_clear(m);
1157 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001158 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001159 auth_method = "keyboard-interactive";
1160 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001161 if (ret == 0)
1162 sshpam_authok = sshpam_ctxt;
1163 return (0);
1164}
1165
1166int
Darren Tucker7eda5922004-06-22 13:22:50 +10001167mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001168{
1169
1170 debug3("%s", __func__);
1171 (sshpam_device.free_ctx)(sshpam_ctxt);
1172 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001173 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001174 auth_method = "keyboard-interactive";
1175 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001176 return (sshpam_authok == sshpam_ctxt);
1177}
Damien Miller79418552002-04-23 20:28:48 +10001178#endif
1179
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001181mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001182{
1183 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001184 char *cuser, *chost;
1185 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001186 u_int bloblen;
1187 enum mm_keytype type = 0;
1188 int allowed = 0;
1189
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001190 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001191
1192 type = buffer_get_int(m);
1193 cuser = buffer_get_string(m, NULL);
1194 chost = buffer_get_string(m, NULL);
1195 blob = buffer_get_string(m, &bloblen);
1196
1197 key = key_from_blob(blob, bloblen);
1198
1199 if ((compat20 && type == MM_RSAHOSTKEY) ||
1200 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001201 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001203 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204
Damien Miller3e3b5142003-11-17 21:13:40 +11001205 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001206 /* These should not make it past the privsep child */
1207 if (key_type_plain(key->type) == KEY_RSA &&
1208 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1209 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1210
Darren Tucker47eede72005-03-14 23:08:12 +11001211 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001213 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001214 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001215 match_pattern_list(sshkey_ssh_name(key),
1216 options.pubkey_key_types,
1217 strlen(options.pubkey_key_types), 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001218 user_key_allowed(authctxt->pw, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001219 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001220 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001221 if (options.pubkey_authentication && allowed != 1)
1222 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001223 break;
1224 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001225 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001226 match_pattern_list(sshkey_ssh_name(key),
1227 options.hostbased_key_types,
1228 strlen(options.hostbased_key_types), 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001229 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001230 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001231 pubkey_auth_info(authctxt, key,
1232 "client user \"%.100s\", client host \"%.100s\"",
1233 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001234 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001236#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001237 case MM_RSAHOSTKEY:
1238 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001239 allowed = options.rhosts_rsa_authentication &&
1240 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001242 if (options.rhosts_rsa_authentication && allowed != 1)
1243 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001244 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001246#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001248 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249 break;
1250 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001251 }
Damien Miller00111382003-03-10 11:21:17 +11001252 if (key != NULL)
1253 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254
1255 /* clear temporarily storage (used by verify) */
1256 monitor_reset_key_state();
1257
1258 if (allowed) {
1259 /* Save temporarily for comparison in verify */
1260 key_blob = blob;
1261 key_bloblen = bloblen;
1262 key_blobtype = type;
1263 hostbased_cuser = cuser;
1264 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001265 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001266 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001267 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001268 free(blob);
1269 free(cuser);
1270 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001271 }
1272
1273 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001274 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001275
1276 buffer_clear(m);
1277 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001278 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001279
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001280 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001281
1282 if (type == MM_RSAHOSTKEY)
1283 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1284
1285 return (0);
1286}
1287
1288static int
1289monitor_valid_userblob(u_char *data, u_int datalen)
1290{
1291 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001292 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293 u_int len;
1294 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295
1296 buffer_init(&b);
1297 buffer_append(&b, data, datalen);
1298
1299 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001300 p = buffer_ptr(&b);
1301 len = buffer_len(&b);
1302 if ((session_id2 == NULL) ||
1303 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001304 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001305 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001306 buffer_consume(&b, session_id2_len);
1307 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001308 p = buffer_get_string(&b, &len);
1309 if ((session_id2 == NULL) ||
1310 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001311 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001312 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001313 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314 }
1315 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1316 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001317 p = buffer_get_cstring(&b, NULL);
1318 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1319 authctxt->style ? ":" : "",
1320 authctxt->style ? authctxt->style : "");
1321 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001322 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001323 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001324 fail++;
1325 }
Darren Tuckera627d422013-06-02 07:31:17 +10001326 free(userstyle);
1327 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001328 buffer_skip_string(&b);
1329 if (datafellows & SSH_BUG_PKAUTH) {
1330 if (!buffer_get_char(&b))
1331 fail++;
1332 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001333 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334 if (strcmp("publickey", p) != 0)
1335 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001336 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001337 if (!buffer_get_char(&b))
1338 fail++;
1339 buffer_skip_string(&b);
1340 }
1341 buffer_skip_string(&b);
1342 if (buffer_len(&b) != 0)
1343 fail++;
1344 buffer_free(&b);
1345 return (fail == 0);
1346}
1347
1348static int
Damien Millera10f5612002-09-12 09:49:15 +10001349monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1350 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351{
1352 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001353 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354 u_int len;
1355 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001356
1357 buffer_init(&b);
1358 buffer_append(&b, data, datalen);
1359
Ben Lindstromf67e0772002-06-06 20:58:19 +00001360 p = buffer_get_string(&b, &len);
1361 if ((session_id2 == NULL) ||
1362 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001363 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001365 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001366
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001367 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1368 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001369 p = buffer_get_cstring(&b, NULL);
1370 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1371 authctxt->style ? ":" : "",
1372 authctxt->style ? authctxt->style : "");
1373 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001374 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001375 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001376 fail++;
1377 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001378 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001379 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001381 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382 if (strcmp(p, "hostbased") != 0)
1383 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001384 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001385 buffer_skip_string(&b); /* pkalg */
1386 buffer_skip_string(&b); /* pkblob */
1387
1388 /* verify client host, strip trailing dot if necessary */
1389 p = buffer_get_string(&b, NULL);
1390 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1391 p[len - 1] = '\0';
1392 if (strcmp(p, chost) != 0)
1393 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001394 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395
1396 /* verify client user */
1397 p = buffer_get_string(&b, NULL);
1398 if (strcmp(p, cuser) != 0)
1399 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001400 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401
1402 if (buffer_len(&b) != 0)
1403 fail++;
1404 buffer_free(&b);
1405 return (fail == 0);
1406}
1407
1408int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001409mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410{
1411 Key *key;
1412 u_char *signature, *data, *blob;
1413 u_int signaturelen, datalen, bloblen;
1414 int verified = 0;
1415 int valid_data = 0;
1416
1417 blob = buffer_get_string(m, &bloblen);
1418 signature = buffer_get_string(m, &signaturelen);
1419 data = buffer_get_string(m, &datalen);
1420
1421 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001422 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001423 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001424
1425 key = key_from_blob(blob, bloblen);
1426 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001427 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428
1429 switch (key_blobtype) {
1430 case MM_USERKEY:
1431 valid_data = monitor_valid_userblob(data, datalen);
1432 break;
1433 case MM_HOSTKEY:
1434 valid_data = monitor_valid_hostbasedblob(data, datalen,
1435 hostbased_cuser, hostbased_chost);
1436 break;
1437 default:
1438 valid_data = 0;
1439 break;
1440 }
1441 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001442 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443
1444 verified = key_verify(key, signature, signaturelen, data, datalen);
1445 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001446 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001448 /* If auth was successful then record key to ensure it isn't reused */
1449 if (verified == 1)
1450 auth2_record_userkey(authctxt, key);
1451 else
1452 key_free(key);
1453
Darren Tuckera627d422013-06-02 07:31:17 +10001454 free(blob);
1455 free(signature);
1456 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457
Ben Lindstrome1c09122002-06-23 00:38:24 +00001458 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1459
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460 monitor_reset_key_state();
1461
1462 buffer_clear(m);
1463 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001464 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465
Darren Tuckerfbba7352006-11-07 23:16:08 +11001466 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467}
1468
1469static void
1470mm_record_login(Session *s, struct passwd *pw)
1471{
1472 socklen_t fromlen;
1473 struct sockaddr_storage from;
1474
1475 /*
1476 * Get IP address of client. If the connection is not a socket, let
1477 * the address be 0.0.0.0.
1478 */
1479 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001480 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001482 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001483 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001485 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486 }
1487 }
1488 /* Record that there was a login on that tty from the remote host. */
1489 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001490 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001491 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492}
1493
1494static void
1495mm_session_close(Session *s)
1496{
Damien Miller04bd8b02003-05-25 14:38:33 +10001497 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001499 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500 session_pty_cleanup2(s);
1501 }
Damien Miller7207f642008-05-19 15:34:50 +10001502 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503}
1504
1505int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001506mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001508 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509 Session *s;
1510 int res, fd0;
1511
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001512 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513
1514 buffer_clear(m);
1515 s = session_new();
1516 if (s == NULL)
1517 goto error;
1518 s->authctxt = authctxt;
1519 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001520 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001521 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1522 if (res == 0)
1523 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524 pty_setowner(authctxt->pw, s->tty);
1525
1526 buffer_put_int(m, 1);
1527 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001528
1529 /* We need to trick ttyslot */
1530 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001531 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532
1533 mm_record_login(s, authctxt->pw);
1534
1535 /* Now we can close the file descriptor again */
1536 close(0);
1537
Darren Tucker09991742004-07-17 17:05:14 +10001538 /* send messages generated by record_login */
1539 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1540 buffer_clear(&loginmsg);
1541
1542 mm_request_send(sock, MONITOR_ANS_PTY, m);
1543
Damien Miller54fd7cf2007-09-17 12:04:08 +10001544 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1545 mm_send_fd(sock, s->ttyfd) == -1)
1546 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001547
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001548 /* make sure nothing uses fd 0 */
1549 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001550 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001551 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001552 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001553
1554 /* slave is not needed */
1555 close(s->ttyfd);
1556 s->ttyfd = s->ptyfd;
1557 /* no need to dup() because nobody closes ptyfd */
1558 s->ptymaster = s->ptyfd;
1559
Damien Miller9ab00b42006-08-05 12:40:11 +10001560 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001561
1562 return (0);
1563
1564 error:
1565 if (s != NULL)
1566 mm_session_close(s);
1567 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001568 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001569 return (0);
1570}
1571
1572int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001573mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001574{
1575 Session *s;
1576 char *tty;
1577
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001578 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001579
1580 tty = buffer_get_string(m, NULL);
1581 if ((s = session_by_tty(tty)) != NULL)
1582 mm_session_close(s);
1583 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001584 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585 return (0);
1586}
1587
Damien Miller1f0311c2014-05-15 14:24:09 +10001588#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001589int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001590mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001591{
1592 BIGNUM *p;
1593 int rsafail;
1594
1595 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001596 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001597
1598 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001599 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001600
1601 buffer_get_bignum2(m, p);
1602
1603 rsafail = ssh1_session_key(p);
1604
1605 buffer_clear(m);
1606 buffer_put_int(m, rsafail);
1607 buffer_put_bignum2(m, p);
1608
1609 BN_clear_free(p);
1610
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001611 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001612
1613 /* Turn on permissions for sessid passing */
1614 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1615
1616 return (0);
1617}
1618
1619int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001620mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001621{
1622 int i;
1623
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001624 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625
1626 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001627 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001628 for (i = 0; i < 16; i++)
1629 session_id[i] = buffer_get_char(m);
1630
1631 /* Turn on permissions for getpwnam */
1632 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1633
1634 return (0);
1635}
1636
1637int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001638mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639{
1640 BIGNUM *client_n;
1641 Key *key = NULL;
1642 u_char *blob = NULL;
1643 u_int blen = 0;
1644 int allowed = 0;
1645
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001646 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001647
Damien Miller7a8f5b32006-03-31 23:14:23 +11001648 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001649 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001650 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001651 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001652 buffer_get_bignum2(m, client_n);
1653 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1654 BN_clear_free(client_n);
1655 }
1656 buffer_clear(m);
1657 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001658 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001659
1660 /* clear temporarily storage (used by generate challenge) */
1661 monitor_reset_key_state();
1662
1663 if (allowed && key != NULL) {
1664 key->type = KEY_RSA; /* cheat for key_to_blob */
1665 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001666 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001667 buffer_put_string(m, blob, blen);
1668
1669 /* Save temporarily for comparison in verify */
1670 key_blob = blob;
1671 key_bloblen = blen;
1672 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001673 }
Damien Miller00111382003-03-10 11:21:17 +11001674 if (key != NULL)
1675 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001676
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001677 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001678
1679 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1680 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1681 return (0);
1682}
1683
1684int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001685mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001686{
1687 Key *key = NULL;
1688 u_char *blob;
1689 u_int blen;
1690
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001691 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001692
1693 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001694 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001695 blob = buffer_get_string(m, &blen);
1696 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001697 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001698 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001699 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001700 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001701 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001702 if (key->type != KEY_RSA)
1703 fatal("%s: received bad key type %d", __func__, key->type);
1704 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705 if (ssh1_challenge)
1706 BN_clear_free(ssh1_challenge);
1707 ssh1_challenge = auth_rsa_generate_challenge(key);
1708
1709 buffer_clear(m);
1710 buffer_put_bignum2(m, ssh1_challenge);
1711
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001712 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001713 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001714
1715 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001716
Darren Tuckera627d422013-06-02 07:31:17 +10001717 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001718 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001719 return (0);
1720}
1721
1722int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001723mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001724{
1725 Key *key = NULL;
1726 u_char *blob, *response;
1727 u_int blen, len;
1728 int success;
1729
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001730 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001731
1732 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001733 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001734 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001735 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001736
1737 blob = buffer_get_string(m, &blen);
1738 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001739 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001740 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001741 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001742 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001743 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001744 response = buffer_get_string(m, &len);
1745 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001746 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001747 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1748
Darren Tuckera627d422013-06-02 07:31:17 +10001749 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001750 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001751 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001752
1753 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1754
1755 /* reset state */
1756 BN_clear_free(ssh1_challenge);
1757 ssh1_challenge = NULL;
1758 monitor_reset_key_state();
1759
1760 buffer_clear(m);
1761 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001762 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001763
1764 return (success);
1765}
Damien Miller1f0311c2014-05-15 14:24:09 +10001766#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001767
1768int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001769mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001770{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001771 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001772 int res, status;
1773
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001774 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001775
1776 /* The child is terminating */
1777 session_destroy_all(&mm_session_close);
1778
Darren Tucker52358d62008-03-11 22:58:25 +11001779#ifdef USE_PAM
1780 if (options.use_pam)
1781 sshpam_cleanup();
1782#endif
1783
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001784 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001785 if (errno != EINTR)
1786 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001787
1788 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1789
1790 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001791 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001792}
1793
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001794#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001795/* Report that an audit event occurred */
1796int
1797mm_answer_audit_event(int socket, Buffer *m)
1798{
1799 ssh_audit_event_t event;
1800
1801 debug3("%s entering", __func__);
1802
1803 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001804 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001805 case SSH_AUTH_FAIL_PUBKEY:
1806 case SSH_AUTH_FAIL_HOSTBASED:
1807 case SSH_AUTH_FAIL_GSSAPI:
1808 case SSH_LOGIN_EXCEED_MAXTRIES:
1809 case SSH_LOGIN_ROOT_DENIED:
1810 case SSH_CONNECTION_CLOSE:
1811 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001812 audit_event(event);
1813 break;
1814 default:
1815 fatal("Audit event type %d not permitted", event);
1816 }
1817
1818 return (0);
1819}
1820
1821int
1822mm_answer_audit_command(int socket, Buffer *m)
1823{
1824 u_int len;
1825 char *cmd;
1826
1827 debug3("%s entering", __func__);
1828 cmd = buffer_get_string(m, &len);
1829 /* sanity check command, if so how? */
1830 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001831 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001832 return (0);
1833}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001834#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001835
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001836void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001837monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001838{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001839 struct ssh *ssh = active_state; /* XXX */
1840 struct kex *kex;
1841 int r;
1842
1843 debug3("%s: packet_set_state", __func__);
1844 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1845 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1846 sshbuf_free(child_state);
1847 child_state = NULL;
1848
1849 if ((kex = ssh->kex) != 0) {
1850 /* XXX set callbacks */
1851 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1852 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1853 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1854 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1855 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1856 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1857 kex->load_host_public_key=&get_hostkey_public_by_type;
1858 kex->load_host_private_key=&get_hostkey_private_by_type;
1859 kex->host_key_index=&get_hostkey_index;
1860 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001861 }
1862
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001863 /* Update with new address */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001864 if (options.compression) {
1865 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib,
1866 (ssh_packet_comp_alloc_func *)mm_zalloc,
1867 (ssh_packet_comp_free_func *)mm_zfree);
1868 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001869}
1870
1871/* This function requries careful sanity checking */
1872
1873void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001874mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001875{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001876 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001877
markus@openbsd.org091c3022015-01-19 19:52:16 +00001878 if ((child_state = sshbuf_new()) == NULL)
1879 fatal("%s: sshbuf_new failed", __func__);
1880 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1881 child_state);
1882 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001883}
1884
1885
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001886/* XXX */
1887
1888#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001889 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001890 fatal("fcntl(%d, F_SETFD)", x); \
1891} while (0)
1892
1893static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001894monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001895{
Damien Miller8f0bf232011-06-20 14:42:23 +10001896 int pair[2];
1897
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001898 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001899 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001900 FD_CLOSEONEXEC(pair[0]);
1901 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001902 mon->m_recvfd = pair[0];
1903 mon->m_sendfd = pair[1];
1904
1905 if (do_logfds) {
1906 if (pipe(pair) == -1)
1907 fatal("%s: pipe: %s", __func__, strerror(errno));
1908 FD_CLOSEONEXEC(pair[0]);
1909 FD_CLOSEONEXEC(pair[1]);
1910 mon->m_log_recvfd = pair[0];
1911 mon->m_log_sendfd = pair[1];
1912 } else
1913 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001914}
1915
1916#define MM_MEMSIZE 65536
1917
1918struct monitor *
1919monitor_init(void)
1920{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001921 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001922 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001923
Damien Miller07d86be2006-03-26 14:19:21 +11001924 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001925
Damien Miller8f0bf232011-06-20 14:42:23 +10001926 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001927
1928 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001929 if (options.compression) {
1930 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1931 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001932
Damien Miller2d6b8332002-06-21 15:59:49 +10001933 /* Compression needs to share state across borders */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001934 ssh_packet_set_compress_hooks(ssh, mon->m_zlib,
1935 (ssh_packet_comp_alloc_func *)mm_zalloc,
1936 (ssh_packet_comp_free_func *)mm_zfree);
Damien Miller2d6b8332002-06-21 15:59:49 +10001937 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001938
1939 return mon;
1940}
1941
1942void
1943monitor_reinit(struct monitor *mon)
1944{
Damien Miller8f0bf232011-06-20 14:42:23 +10001945 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001946}
Darren Tucker0efd1552003-08-26 11:49:55 +10001947
1948#ifdef GSSAPI
1949int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001950mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001951{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001952 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001953 OM_uint32 major;
1954 u_int len;
1955
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001956 goid.elements = buffer_get_string(m, &len);
1957 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001958
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001959 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001960
Darren Tuckera627d422013-06-02 07:31:17 +10001961 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001962
1963 buffer_clear(m);
1964 buffer_put_int(m, major);
1965
Damien Miller6fd6def2005-11-05 15:07:05 +11001966 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001967
1968 /* Now we have a context, enable the step */
1969 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1970
1971 return (0);
1972}
1973
1974int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001975mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001976{
1977 gss_buffer_desc in;
1978 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001979 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001980 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001981 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001982
Darren Tucker600ad8d2003-08-26 12:10:48 +10001983 in.value = buffer_get_string(m, &len);
1984 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001985 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001986 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001987
1988 buffer_clear(m);
1989 buffer_put_int(m, major);
1990 buffer_put_string(m, out.value, out.length);
1991 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001992 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001993
1994 gss_release_buffer(&minor, &out);
1995
Damien Miller6fd6def2005-11-05 15:07:05 +11001996 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001997 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1998 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001999 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002000 }
2001 return (0);
2002}
2003
2004int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002005mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002006{
2007 gss_buffer_desc gssbuf, mic;
2008 OM_uint32 ret;
2009 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002010
Damien Miller0425d402003-11-17 22:18:21 +11002011 gssbuf.value = buffer_get_string(m, &len);
2012 gssbuf.length = len;
2013 mic.value = buffer_get_string(m, &len);
2014 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002015
Damien Miller0425d402003-11-17 22:18:21 +11002016 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002017
Darren Tuckera627d422013-06-02 07:31:17 +10002018 free(gssbuf.value);
2019 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002020
Damien Miller0425d402003-11-17 22:18:21 +11002021 buffer_clear(m);
2022 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002023
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002024 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002025
Damien Miller0425d402003-11-17 22:18:21 +11002026 if (!GSS_ERROR(ret))
2027 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002028
Damien Miller0425d402003-11-17 22:18:21 +11002029 return (0);
2030}
2031
2032int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002033mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002034{
2035 int authenticated;
2036
2037 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2038
2039 buffer_clear(m);
2040 buffer_put_int(m, authenticated);
2041
2042 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002043 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002044
Damien Miller6fd6def2005-11-05 15:07:05 +11002045 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002046
2047 /* Monitor loop will terminate if authenticated */
2048 return (authenticated);
2049}
2050#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002051