blob: 5a28d1b348bbcd20c2e530e9970f4599a7b43faa [file] [log] [blame]
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001/* $OpenBSD: monitor.c,v 1.137 2015/01/13 07:39:19 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 Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100032#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110034#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000035
Darren Tucker39972492006-07-12 22:22:46 +100036#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien 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"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104
Darren Tucker0efd1552003-08-26 11:49:55 +1000105#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000106static Gssctxt *gsscontext = NULL;
107#endif
108
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109/* Imports */
110extern ServerOptions options;
111extern u_int utmp_len;
112extern Newkeys *current_keys[];
113extern z_stream incoming_stream;
114extern z_stream outgoing_stream;
115extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116extern Buffer auth_debug;
117extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000118extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119
120/* State exported from the child */
121
122struct {
123 z_stream incoming;
124 z_stream outgoing;
125 u_char *keyin;
126 u_int keyinlen;
127 u_char *keyout;
128 u_int keyoutlen;
129 u_char *ivin;
130 u_int ivinlen;
131 u_char *ivout;
132 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000133 u_char *ssh1key;
134 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135 int ssh1cipher;
136 int ssh1protoflags;
137 u_char *input;
138 u_int ilen;
139 u_char *output;
140 u_int olen;
Darren Tuckerc5564e12009-06-21 18:53:53 +1000141 u_int64_t sent_bytes;
142 u_int64_t recv_bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000143} child_state;
144
Damien Miller469954d2003-06-18 20:25:33 +1000145/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000146
147int mm_answer_moduli(int, Buffer *);
148int mm_answer_sign(int, Buffer *);
149int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000150int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000151int mm_answer_authserv(int, Buffer *);
152int mm_answer_authpassword(int, Buffer *);
153int mm_answer_bsdauthquery(int, Buffer *);
154int mm_answer_bsdauthrespond(int, Buffer *);
155int mm_answer_skeyquery(int, Buffer *);
156int mm_answer_skeyrespond(int, Buffer *);
157int mm_answer_keyallowed(int, Buffer *);
158int mm_answer_keyverify(int, Buffer *);
159int mm_answer_pty(int, Buffer *);
160int mm_answer_pty_cleanup(int, Buffer *);
161int mm_answer_term(int, Buffer *);
162int mm_answer_rsa_keyallowed(int, Buffer *);
163int mm_answer_rsa_challenge(int, Buffer *);
164int mm_answer_rsa_response(int, Buffer *);
165int mm_answer_sesskey(int, Buffer *);
166int mm_answer_sessid(int, Buffer *);
167
Damien Miller79418552002-04-23 20:28:48 +1000168#ifdef USE_PAM
169int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000170int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000171int mm_answer_pam_init_ctx(int, Buffer *);
172int mm_answer_pam_query(int, Buffer *);
173int mm_answer_pam_respond(int, Buffer *);
174int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000175#endif
176
Darren Tucker0efd1552003-08-26 11:49:55 +1000177#ifdef GSSAPI
178int mm_answer_gss_setup_ctx(int, Buffer *);
179int mm_answer_gss_accept_ctx(int, Buffer *);
180int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100181int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000182#endif
Damien Miller25162f22002-09-12 09:47:29 +1000183
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100184#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100185int mm_answer_audit_event(int, Buffer *);
186int mm_answer_audit_command(int, Buffer *);
187#endif
188
Damien Miller8f0bf232011-06-20 14:42:23 +1000189static int monitor_read_log(struct monitor *);
190
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000191static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000192
193#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000194static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
Damien Miller1f0311c2014-05-15 14:24:09 +1000195#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000196
197/* local state for key verify */
198static u_char *key_blob = NULL;
199static u_int key_bloblen = 0;
200static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000201static char *hostbased_cuser = NULL;
202static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100204static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000205static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000206static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000207static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208
209struct mon_table {
210 enum monitor_reqtype type;
211 int flags;
212 int (*f)(int, Buffer *);
213};
214
215#define MON_ISAUTH 0x0004 /* Required for Authentication */
216#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
217#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100218#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000219
220#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
221
222#define MON_PERMIT 0x1000 /* Request is permitted */
223
224struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000225#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000227#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
229 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
230 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000231 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000232 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000233#ifdef USE_PAM
234 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000235 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000236 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
237 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
238 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
239 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000240#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100241#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100242 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100243#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244#ifdef BSD_AUTH
245 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100246 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000247#endif
248#ifdef SKEY
249 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
250 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
251#endif
252 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
253 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000254#ifdef GSSAPI
255 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
256 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
257 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100258 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000259#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000260 {0, 0, NULL}
261};
262
263struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000264#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000265 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000266#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000267 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
268 {MONITOR_REQ_PTY, 0, mm_answer_pty},
269 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
270 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100271#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100272 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
273 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
274#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275 {0, 0, NULL}
276};
277
278struct mon_table mon_dispatch_proto15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000279#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000280 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
281 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
282 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
283 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100284 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
285 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
287 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
288#ifdef BSD_AUTH
289 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100290 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291#endif
292#ifdef SKEY
293 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
294 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
295#endif
Damien Millera33501b2002-05-08 12:24:42 +1000296#ifdef USE_PAM
297 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000298 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000299 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
300 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
301 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
302 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000303#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100304#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100305 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100306#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000307#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000308 {0, 0, NULL}
309};
310
311struct mon_table mon_dispatch_postauth15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000312#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000313 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
314 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
315 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100316#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100317 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000318 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100319#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000320#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000321 {0, 0, NULL}
322};
323
324struct mon_table *mon_dispatch;
325
326/* Specifies if a certain message is allowed at the moment */
327
328static void
329monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
330{
331 while (ent->f != NULL) {
332 if (ent->type == type) {
333 ent->flags &= ~MON_PERMIT;
334 ent->flags |= permit ? MON_PERMIT : 0;
335 return;
336 }
337 ent++;
338 }
339}
340
341static void
342monitor_permit_authentications(int permit)
343{
344 struct mon_table *ent = mon_dispatch;
345
346 while (ent->f != NULL) {
347 if (ent->flags & MON_AUTH) {
348 ent->flags &= ~MON_PERMIT;
349 ent->flags |= permit ? MON_PERMIT : 0;
350 }
351 ent++;
352 }
353}
354
Darren Tucker3e33cec2003-10-02 16:12:36 +1000355void
356monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357{
358 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100359 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360
361 debug3("preauth child monitor started");
362
Damien Miller8f0bf232011-06-20 14:42:23 +1000363 close(pmonitor->m_recvfd);
364 close(pmonitor->m_log_sendfd);
365 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
366
Darren Tucker3e33cec2003-10-02 16:12:36 +1000367 authctxt = _authctxt;
368 memset(authctxt, 0, sizeof(*authctxt));
369
Darren Tuckerde0de392005-03-31 23:52:04 +1000370 authctxt->loginmsg = &loginmsg;
371
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372 if (compat20) {
373 mon_dispatch = mon_dispatch_proto20;
374
375 /* Permit requests for moduli and signatures */
376 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
377 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
378 } else {
379 mon_dispatch = mon_dispatch_proto15;
380
381 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
382 }
383
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384 /* The first few requests do not require asynchronous access */
385 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100386 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100387 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100388 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100389 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100390
391 /* Special handling for multiple required authentications */
392 if (options.num_auth_methods != 0) {
393 if (!compat20)
394 fatal("AuthenticationMethods is not supported"
395 "with SSH protocol 1");
396 if (authenticated &&
397 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000398 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100399 debug3("%s: method %s: partial", __func__,
400 auth_method);
401 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100402 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100403 }
404 }
405
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406 if (authenticated) {
407 if (!(ent->flags & MON_AUTHDECIDE))
408 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000409 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410 if (authctxt->pw->pw_uid == 0 &&
411 !auth_root_allowed(auth_method))
412 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000413#ifdef USE_PAM
414 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100415 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000416 Buffer m;
417
418 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100419 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000420 MONITOR_REQ_PAM_ACCOUNT, &m);
421 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
422 buffer_free(&m);
423 }
424#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000425 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100426 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100427 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000428 auth_method, auth_submethod);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000429 if (!authenticated)
430 authctxt->failures++;
431 }
432 }
433
434 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000435 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100436 if (strcmp(auth_method, "unknown") == 0)
437 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000438
439 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000440 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000441
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000442 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000443
Damien Miller6a1937e2012-12-12 10:44:38 +1100444 /* Drain any buffered messages from the child */
445 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
446 ;
447
Damien Miller8f0bf232011-06-20 14:42:23 +1000448 close(pmonitor->m_sendfd);
449 close(pmonitor->m_log_recvfd);
450 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000451}
452
Damien Millerbe64d432003-05-14 19:31:12 +1000453static void
454monitor_set_child_handler(pid_t pid)
455{
456 monitor_child_pid = pid;
457}
458
459static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000460monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000461{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000462 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000463}
464
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000465void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000466monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000467{
Damien Miller8f0bf232011-06-20 14:42:23 +1000468 close(pmonitor->m_recvfd);
469 pmonitor->m_recvfd = -1;
470
Damien Millerbe64d432003-05-14 19:31:12 +1000471 monitor_set_child_handler(pmonitor->m_pid);
472 signal(SIGHUP, &monitor_child_handler);
473 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000474 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000475#ifdef SIGXFSZ
476 signal(SIGXFSZ, SIG_IGN);
477#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000478
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000479 if (compat20) {
480 mon_dispatch = mon_dispatch_postauth20;
481
482 /* Permit requests for moduli and signatures */
483 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
484 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
485 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000486 } else {
487 mon_dispatch = mon_dispatch_postauth15;
488 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
489 }
490 if (!no_pty_flag) {
491 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
492 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
493 }
494
495 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000496 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000497}
498
499void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000500monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501{
Damien Miller2d6b8332002-06-21 15:59:49 +1000502 if (options.compression) {
503 /* The member allocation is not visible, so sync it */
504 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
505 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000506}
507
Damien Miller8f0bf232011-06-20 14:42:23 +1000508static int
509monitor_read_log(struct monitor *pmonitor)
510{
511 Buffer logmsg;
512 u_int len, level;
513 char *msg;
514
515 buffer_init(&logmsg);
516
517 /* Read length */
518 buffer_append_space(&logmsg, 4);
519 if (atomicio(read, pmonitor->m_log_recvfd,
520 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
521 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100522 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000523 debug("%s: child log fd closed", __func__);
524 close(pmonitor->m_log_recvfd);
525 pmonitor->m_log_recvfd = -1;
526 return -1;
527 }
528 fatal("%s: log fd read: %s", __func__, strerror(errno));
529 }
530 len = buffer_get_int(&logmsg);
531 if (len <= 4 || len > 8192)
532 fatal("%s: invalid log message length %u", __func__, len);
533
534 /* Read severity, message */
535 buffer_clear(&logmsg);
536 buffer_append_space(&logmsg, len);
537 if (atomicio(read, pmonitor->m_log_recvfd,
538 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
539 fatal("%s: log fd read: %s", __func__, strerror(errno));
540
541 /* Log it */
542 level = buffer_get_int(&logmsg);
543 msg = buffer_get_string(&logmsg, NULL);
544 if (log_level_name(level) == NULL)
545 fatal("%s: invalid log level %u (corrupted message?)",
546 __func__, level);
547 do_log2(level, "%s [preauth]", msg);
548
549 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000550 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000551
552 return 0;
553}
554
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000555int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000556monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557 struct mon_table **pent)
558{
559 Buffer m;
560 int ret;
561 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000562 struct pollfd pfd[2];
563
564 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100565 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000566 pfd[0].fd = pmonitor->m_sendfd;
567 pfd[0].events = POLLIN;
568 pfd[1].fd = pmonitor->m_log_recvfd;
569 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000570 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
571 if (errno == EINTR || errno == EAGAIN)
572 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000573 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000574 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000575 if (pfd[1].revents) {
576 /*
577 * Drain all log messages before processing next
578 * monitor request.
579 */
580 monitor_read_log(pmonitor);
581 continue;
582 }
583 if (pfd[0].revents)
584 break; /* Continues below */
585 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
587 buffer_init(&m);
588
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000589 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590 type = buffer_get_char(&m);
591
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000592 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593
594 while (ent->f != NULL) {
595 if (ent->type == type)
596 break;
597 ent++;
598 }
599
600 if (ent->f != NULL) {
601 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000602 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000604 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605 buffer_free(&m);
606
607 /* The child may use this request only once, disable it */
608 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000609 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610 type);
611 ent->flags &= ~MON_PERMIT;
612 }
613
614 if (pent != NULL)
615 *pent = ent;
616
617 return ret;
618 }
619
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000620 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000621
622 /* NOTREACHED */
623 return (-1);
624}
625
626/* allowed key state */
627static int
628monitor_allowed_key(u_char *blob, u_int bloblen)
629{
630 /* make sure key is allowed */
631 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000632 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000633 return (0);
634 return (1);
635}
636
637static void
638monitor_reset_key_state(void)
639{
640 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000641 free(key_blob);
642 free(hostbased_cuser);
643 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000644 key_blob = NULL;
645 key_bloblen = 0;
646 key_blobtype = MM_NOKEY;
647 hostbased_cuser = NULL;
648 hostbased_chost = NULL;
649}
650
Damien Miller1f0311c2014-05-15 14:24:09 +1000651#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000652int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000653mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654{
655 DH *dh;
656 int min, want, max;
657
658 min = buffer_get_int(m);
659 want = buffer_get_int(m);
660 max = buffer_get_int(m);
661
662 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000663 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000664 /* We need to check here, too, in case the child got corrupted */
665 if (max < min || want < min || max < want)
666 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000667 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668
669 buffer_clear(m);
670
671 dh = choose_dh(min, want, max);
672 if (dh == NULL) {
673 buffer_put_char(m, 0);
674 return (0);
675 } else {
676 /* Send first bignum */
677 buffer_put_char(m, 1);
678 buffer_put_bignum2(m, dh->p);
679 buffer_put_bignum2(m, dh->g);
680
681 DH_free(dh);
682 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000683 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684 return (0);
685}
Damien Miller1f0311c2014-05-15 14:24:09 +1000686#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000687
Damien Miller85b45e02013-07-20 13:21:52 +1000688extern AuthenticationConnection *auth_conn;
689
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000691mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692{
693 Key *key;
694 u_char *p;
695 u_char *signature;
696 u_int siglen, datlen;
697 int keyid;
698
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000699 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700
701 keyid = buffer_get_int(m);
702 p = buffer_get_string(m, &datlen);
703
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).
Damien Millera63128d2006-03-15 12:08:28 +1100707 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000708 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000709 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710
Ben Lindstromf67e0772002-06-06 20:58:19 +0000711 /* save session id, it will be passed on the first call */
712 if (session_id2_len == 0) {
713 session_id2_len = datlen;
714 session_id2 = xmalloc(session_id2_len);
715 memcpy(session_id2, p, session_id2_len);
716 }
717
Damien Miller85b45e02013-07-20 13:21:52 +1000718 if ((key = get_hostkey_by_index(keyid)) != NULL) {
719 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
720 fatal("%s: key_sign failed", __func__);
721 } else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
722 auth_conn != NULL) {
723 if (ssh_agent_sign(auth_conn, key, &signature, &siglen, p,
724 datlen) < 0)
725 fatal("%s: ssh_agent_sign failed", __func__);
726 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000727 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728
Ben Lindstromd5502182002-06-27 00:12:57 +0000729 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
731 buffer_clear(m);
732 buffer_put_string(m, signature, siglen);
733
Darren Tuckera627d422013-06-02 07:31:17 +1000734 free(p);
735 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000737 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738
739 /* Turn on permissions for getpwnam */
740 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
741
742 return (0);
743}
744
745/* Retrieves the password entry and also checks if the user is permitted */
746
747int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000748mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000750 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000751 struct passwd *pwent;
752 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000753 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000755 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756
757 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000758 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000759
Darren Tuckerb09b6772004-06-22 15:06:46 +1000760 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761
Darren Tuckerb09b6772004-06-22 15:06:46 +1000762 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000763
Darren Tuckerb09b6772004-06-22 15:06:46 +1000764 authctxt->user = xstrdup(username);
765 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000766 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000767
768 buffer_clear(m);
769
770 if (pwent == NULL) {
771 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100772 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000773 goto out;
774 }
775
776 allowed = 1;
777 authctxt->pw = pwent;
778 authctxt->valid = 1;
779
780 buffer_put_char(m, 1);
781 buffer_put_string(m, pwent, sizeof(struct passwd));
782 buffer_put_cstring(m, pwent->pw_name);
783 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000784#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000785 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000786#endif
787#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000788 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000789#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000790 buffer_put_cstring(m, pwent->pw_dir);
791 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100792
793 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100794 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000795
796#define M_CP_STROPT(x) do { \
797 if (options.x != NULL) \
798 buffer_put_cstring(m, options.x); \
799 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000800#define M_CP_STRARRAYOPT(x, nx) do { \
801 for (i = 0; i < options.nx; i++) \
802 buffer_put_cstring(m, options.x[i]); \
803 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000804 /* See comment in servconf.h */
805 COPY_MATCH_STRING_OPTS();
806#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000807#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100808
809 /* Create valid auth method lists */
810 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
811 /*
812 * The monitor will continue long enough to let the child
813 * run to it's packet_disconnect(), but it must not allow any
814 * authentication to succeed.
815 */
816 debug("%s: no valid authentication method lists", __func__);
817 }
818
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000819 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000820 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000821
822 /* For SSHv1 allow authentication now */
823 if (!compat20)
824 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000825 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000826 /* Allow service/style information on the auth context */
827 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000828 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
829 }
Damien Millera33501b2002-05-08 12:24:42 +1000830#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000831 if (options.use_pam)
832 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000833#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000834
835 return (0);
836}
837
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000838int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000839{
840 char *banner;
841
842 buffer_clear(m);
843 banner = auth2_read_banner();
844 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000845 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000846 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000847
848 return (0);
849}
850
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000851int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000852mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000853{
854 monitor_permit_authentications(1);
855
856 authctxt->service = buffer_get_string(m, NULL);
857 authctxt->style = buffer_get_string(m, NULL);
858 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000859 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000860
861 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000862 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863 authctxt->style = NULL;
864 }
865
866 return (0);
867}
868
869int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000870mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000871{
872 static int call_count;
873 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000874 int authenticated;
875 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876
877 passwd = buffer_get_string(m, &plen);
878 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000879 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000880 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100881 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000882 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000883
884 buffer_clear(m);
885 buffer_put_int(m, authenticated);
886
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000887 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000888 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889
890 call_count++;
891 if (plen == 0 && call_count == 1)
892 auth_method = "none";
893 else
894 auth_method = "password";
895
896 /* Causes monitor loop to terminate if authenticated */
897 return (authenticated);
898}
899
900#ifdef BSD_AUTH
901int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000902mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000903{
904 char *name, *infotxt;
905 u_int numprompts;
906 u_int *echo_on;
907 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100908 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909
Damien Millerb7df3af2003-02-24 11:55:46 +1100910 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
911 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000912
913 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100914 buffer_put_int(m, success);
915 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000916 buffer_put_cstring(m, prompts[0]);
917
Damien Millerb7df3af2003-02-24 11:55:46 +1100918 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000919 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920
Damien Millerb7df3af2003-02-24 11:55:46 +1100921 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000922 free(name);
923 free(infotxt);
924 free(prompts);
925 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926 }
927
928 return (0);
929}
930
931int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000932mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933{
934 char *response;
935 int authok;
936
937 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000938 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000939
940 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000941 authok = options.challenge_response_authentication &&
942 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000943 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000944 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000945 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946
947 buffer_clear(m);
948 buffer_put_int(m, authok);
949
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000950 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000951 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952
Damien Miller91a55f22013-04-23 15:18:10 +1000953 if (compat20) {
954 auth_method = "keyboard-interactive";
955 auth_submethod = "bsdauth";
956 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100957 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958
959 return (authok != 0);
960}
961#endif
962
963#ifdef SKEY
964int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000965mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966{
967 struct skey skey;
968 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100969 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000970
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000971 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
972 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973
974 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100975 buffer_put_int(m, success);
976 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000977 buffer_put_cstring(m, challenge);
978
Damien Millerb7df3af2003-02-24 11:55:46 +1100979 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000980 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981
982 return (0);
983}
984
985int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000986mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987{
988 char *response;
989 int authok;
990
991 response = buffer_get_string(m, NULL);
992
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000993 authok = (options.challenge_response_authentication &&
994 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000995 skey_haskey(authctxt->pw->pw_name) == 0 &&
996 skey_passcheck(authctxt->pw->pw_name, response) != -1);
997
Darren Tuckerf60845f2013-06-02 08:07:31 +1000998 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999
1000 buffer_clear(m);
1001 buffer_put_int(m, authok);
1002
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001003 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001004 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005
1006 auth_method = "skey";
1007
1008 return (authok != 0);
1009}
1010#endif
1011
Damien Miller79418552002-04-23 20:28:48 +10001012#ifdef USE_PAM
1013int
Darren Tucker7eda5922004-06-22 13:22:50 +10001014mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001015{
Damien Miller4e448a32003-05-14 15:11:48 +10001016 if (!options.use_pam)
1017 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1018
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001019 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001020
Damien Miller1f499fd2003-08-25 13:08:49 +10001021 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1022
Damien Miller79418552002-04-23 20:28:48 +10001023 return (0);
1024}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001025
Damien Miller1f499fd2003-08-25 13:08:49 +10001026int
Darren Tucker7eda5922004-06-22 13:22:50 +10001027mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001028{
1029 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001030
Damien Miller1f499fd2003-08-25 13:08:49 +10001031 if (!options.use_pam)
1032 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1033
1034 ret = do_pam_account();
1035
1036 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001037 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001038
Darren Tucker7eda5922004-06-22 13:22:50 +10001039 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001040
1041 return (ret);
1042}
1043
Damien Miller4f9f42a2003-05-10 19:28:02 +10001044static void *sshpam_ctxt, *sshpam_authok;
1045extern KbdintDevice sshpam_device;
1046
1047int
Darren Tucker7eda5922004-06-22 13:22:50 +10001048mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001049{
1050
1051 debug3("%s", __func__);
1052 authctxt->user = buffer_get_string(m, NULL);
1053 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1054 sshpam_authok = NULL;
1055 buffer_clear(m);
1056 if (sshpam_ctxt != NULL) {
1057 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1058 buffer_put_int(m, 1);
1059 } else {
1060 buffer_put_int(m, 0);
1061 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001062 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001063 return (0);
1064}
1065
1066int
Darren Tucker7eda5922004-06-22 13:22:50 +10001067mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001068{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001069 char *name = NULL, *info = NULL, **prompts = NULL;
1070 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001071 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001072
1073 debug3("%s", __func__);
1074 sshpam_authok = NULL;
1075 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1076 if (ret == 0 && num == 0)
1077 sshpam_authok = sshpam_ctxt;
1078 if (num > 1 || name == NULL || info == NULL)
1079 ret = -1;
1080 buffer_clear(m);
1081 buffer_put_int(m, ret);
1082 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001083 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001084 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001085 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086 buffer_put_int(m, num);
1087 for (i = 0; i < num; ++i) {
1088 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001089 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001090 buffer_put_int(m, echo_on[i]);
1091 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001092 free(prompts);
1093 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001094 auth_method = "keyboard-interactive";
1095 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001096 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001097 return (0);
1098}
1099
1100int
Darren Tucker7eda5922004-06-22 13:22:50 +10001101mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102{
1103 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001104 u_int i, num;
1105 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001106
1107 debug3("%s", __func__);
1108 sshpam_authok = NULL;
1109 num = buffer_get_int(m);
1110 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001111 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001112 for (i = 0; i < num; ++i)
1113 resp[i] = buffer_get_string(m, NULL);
1114 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1115 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001116 free(resp[i]);
1117 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001118 } else {
1119 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1120 }
1121 buffer_clear(m);
1122 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001123 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001124 auth_method = "keyboard-interactive";
1125 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001126 if (ret == 0)
1127 sshpam_authok = sshpam_ctxt;
1128 return (0);
1129}
1130
1131int
Darren Tucker7eda5922004-06-22 13:22:50 +10001132mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001133{
1134
1135 debug3("%s", __func__);
1136 (sshpam_device.free_ctx)(sshpam_ctxt);
1137 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001138 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001139 auth_method = "keyboard-interactive";
1140 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001141 return (sshpam_authok == sshpam_ctxt);
1142}
Damien Miller79418552002-04-23 20:28:48 +10001143#endif
1144
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001145int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001146mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001147{
1148 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001149 char *cuser, *chost;
1150 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001151 u_int bloblen;
1152 enum mm_keytype type = 0;
1153 int allowed = 0;
1154
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001155 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001156
1157 type = buffer_get_int(m);
1158 cuser = buffer_get_string(m, NULL);
1159 chost = buffer_get_string(m, NULL);
1160 blob = buffer_get_string(m, &bloblen);
1161
1162 key = key_from_blob(blob, bloblen);
1163
1164 if ((compat20 && type == MM_RSAHOSTKEY) ||
1165 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001166 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001167
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001168 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001169
Damien Miller3e3b5142003-11-17 21:13:40 +11001170 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001171 /* These should not make it past the privsep child */
1172 if (key_type_plain(key->type) == KEY_RSA &&
1173 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1174 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1175
Darren Tucker47eede72005-03-14 23:08:12 +11001176 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001178 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001179 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001180 match_pattern_list(sshkey_ssh_name(key),
1181 options.pubkey_key_types,
1182 strlen(options.pubkey_key_types), 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001183 user_key_allowed(authctxt->pw, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001184 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001185 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001186 if (options.pubkey_authentication && allowed != 1)
1187 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001188 break;
1189 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001190 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001191 match_pattern_list(sshkey_ssh_name(key),
1192 options.hostbased_key_types,
1193 strlen(options.hostbased_key_types), 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001194 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001196 pubkey_auth_info(authctxt, key,
1197 "client user \"%.100s\", client host \"%.100s\"",
1198 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001199 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001200 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001201#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 case MM_RSAHOSTKEY:
1203 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001204 allowed = options.rhosts_rsa_authentication &&
1205 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001206 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001207 if (options.rhosts_rsa_authentication && allowed != 1)
1208 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001209 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001210 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001211#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001213 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001214 break;
1215 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001216 }
Damien Miller00111382003-03-10 11:21:17 +11001217 if (key != NULL)
1218 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001219
1220 /* clear temporarily storage (used by verify) */
1221 monitor_reset_key_state();
1222
1223 if (allowed) {
1224 /* Save temporarily for comparison in verify */
1225 key_blob = blob;
1226 key_bloblen = bloblen;
1227 key_blobtype = type;
1228 hostbased_cuser = cuser;
1229 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001230 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001231 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001232 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001233 free(blob);
1234 free(cuser);
1235 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001236 }
1237
1238 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001239 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001240
1241 buffer_clear(m);
1242 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001243 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001245 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246
1247 if (type == MM_RSAHOSTKEY)
1248 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1249
1250 return (0);
1251}
1252
1253static int
1254monitor_valid_userblob(u_char *data, u_int datalen)
1255{
1256 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001257 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001258 u_int len;
1259 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001260
1261 buffer_init(&b);
1262 buffer_append(&b, data, datalen);
1263
1264 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001265 p = buffer_ptr(&b);
1266 len = buffer_len(&b);
1267 if ((session_id2 == NULL) ||
1268 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001269 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001270 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001271 buffer_consume(&b, session_id2_len);
1272 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001273 p = buffer_get_string(&b, &len);
1274 if ((session_id2 == NULL) ||
1275 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001276 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001278 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001279 }
1280 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1281 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001282 p = buffer_get_cstring(&b, NULL);
1283 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1284 authctxt->style ? ":" : "",
1285 authctxt->style ? authctxt->style : "");
1286 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001287 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001288 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001289 fail++;
1290 }
Darren Tuckera627d422013-06-02 07:31:17 +10001291 free(userstyle);
1292 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293 buffer_skip_string(&b);
1294 if (datafellows & SSH_BUG_PKAUTH) {
1295 if (!buffer_get_char(&b))
1296 fail++;
1297 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001298 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001299 if (strcmp("publickey", p) != 0)
1300 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001301 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001302 if (!buffer_get_char(&b))
1303 fail++;
1304 buffer_skip_string(&b);
1305 }
1306 buffer_skip_string(&b);
1307 if (buffer_len(&b) != 0)
1308 fail++;
1309 buffer_free(&b);
1310 return (fail == 0);
1311}
1312
1313static int
Damien Millera10f5612002-09-12 09:49:15 +10001314monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1315 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001316{
1317 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001318 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001319 u_int len;
1320 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001321
1322 buffer_init(&b);
1323 buffer_append(&b, data, datalen);
1324
Ben Lindstromf67e0772002-06-06 20:58:19 +00001325 p = buffer_get_string(&b, &len);
1326 if ((session_id2 == NULL) ||
1327 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001328 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001330 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001331
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001332 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1333 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001334 p = buffer_get_cstring(&b, NULL);
1335 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1336 authctxt->style ? ":" : "",
1337 authctxt->style ? authctxt->style : "");
1338 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001339 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001340 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341 fail++;
1342 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001343 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001344 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001345 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001346 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001347 if (strcmp(p, "hostbased") != 0)
1348 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001349 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001350 buffer_skip_string(&b); /* pkalg */
1351 buffer_skip_string(&b); /* pkblob */
1352
1353 /* verify client host, strip trailing dot if necessary */
1354 p = buffer_get_string(&b, NULL);
1355 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1356 p[len - 1] = '\0';
1357 if (strcmp(p, chost) != 0)
1358 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001359 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001360
1361 /* verify client user */
1362 p = buffer_get_string(&b, NULL);
1363 if (strcmp(p, cuser) != 0)
1364 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001365 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366
1367 if (buffer_len(&b) != 0)
1368 fail++;
1369 buffer_free(&b);
1370 return (fail == 0);
1371}
1372
1373int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001374mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001375{
1376 Key *key;
1377 u_char *signature, *data, *blob;
1378 u_int signaturelen, datalen, bloblen;
1379 int verified = 0;
1380 int valid_data = 0;
1381
1382 blob = buffer_get_string(m, &bloblen);
1383 signature = buffer_get_string(m, &signaturelen);
1384 data = buffer_get_string(m, &datalen);
1385
1386 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001387 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001388 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001389
1390 key = key_from_blob(blob, bloblen);
1391 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001392 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001393
1394 switch (key_blobtype) {
1395 case MM_USERKEY:
1396 valid_data = monitor_valid_userblob(data, datalen);
1397 break;
1398 case MM_HOSTKEY:
1399 valid_data = monitor_valid_hostbasedblob(data, datalen,
1400 hostbased_cuser, hostbased_chost);
1401 break;
1402 default:
1403 valid_data = 0;
1404 break;
1405 }
1406 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001407 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408
1409 verified = key_verify(key, signature, signaturelen, data, datalen);
1410 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001411 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001413 /* If auth was successful then record key to ensure it isn't reused */
1414 if (verified == 1)
1415 auth2_record_userkey(authctxt, key);
1416 else
1417 key_free(key);
1418
Darren Tuckera627d422013-06-02 07:31:17 +10001419 free(blob);
1420 free(signature);
1421 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001422
Ben Lindstrome1c09122002-06-23 00:38:24 +00001423 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1424
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425 monitor_reset_key_state();
1426
1427 buffer_clear(m);
1428 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001429 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001430
Darren Tuckerfbba7352006-11-07 23:16:08 +11001431 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001432}
1433
1434static void
1435mm_record_login(Session *s, struct passwd *pw)
1436{
1437 socklen_t fromlen;
1438 struct sockaddr_storage from;
1439
1440 /*
1441 * Get IP address of client. If the connection is not a socket, let
1442 * the address be 0.0.0.0.
1443 */
1444 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001445 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001448 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001449 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001450 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001451 }
1452 }
1453 /* Record that there was a login on that tty from the remote host. */
1454 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001455 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001456 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457}
1458
1459static void
1460mm_session_close(Session *s)
1461{
Damien Miller04bd8b02003-05-25 14:38:33 +10001462 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001464 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465 session_pty_cleanup2(s);
1466 }
Damien Miller7207f642008-05-19 15:34:50 +10001467 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001468}
1469
1470int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001471mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001472{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001473 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474 Session *s;
1475 int res, fd0;
1476
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001477 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001478
1479 buffer_clear(m);
1480 s = session_new();
1481 if (s == NULL)
1482 goto error;
1483 s->authctxt = authctxt;
1484 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001485 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1487 if (res == 0)
1488 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001489 pty_setowner(authctxt->pw, s->tty);
1490
1491 buffer_put_int(m, 1);
1492 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001493
1494 /* We need to trick ttyslot */
1495 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001496 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001497
1498 mm_record_login(s, authctxt->pw);
1499
1500 /* Now we can close the file descriptor again */
1501 close(0);
1502
Darren Tucker09991742004-07-17 17:05:14 +10001503 /* send messages generated by record_login */
1504 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1505 buffer_clear(&loginmsg);
1506
1507 mm_request_send(sock, MONITOR_ANS_PTY, m);
1508
Damien Miller54fd7cf2007-09-17 12:04:08 +10001509 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1510 mm_send_fd(sock, s->ttyfd) == -1)
1511 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001512
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513 /* make sure nothing uses fd 0 */
1514 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001515 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001517 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518
1519 /* slave is not needed */
1520 close(s->ttyfd);
1521 s->ttyfd = s->ptyfd;
1522 /* no need to dup() because nobody closes ptyfd */
1523 s->ptymaster = s->ptyfd;
1524
Damien Miller9ab00b42006-08-05 12:40:11 +10001525 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001526
1527 return (0);
1528
1529 error:
1530 if (s != NULL)
1531 mm_session_close(s);
1532 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001533 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001534 return (0);
1535}
1536
1537int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001538mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001539{
1540 Session *s;
1541 char *tty;
1542
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001543 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544
1545 tty = buffer_get_string(m, NULL);
1546 if ((s = session_by_tty(tty)) != NULL)
1547 mm_session_close(s);
1548 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001549 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001550 return (0);
1551}
1552
Damien Miller1f0311c2014-05-15 14:24:09 +10001553#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001554int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001555mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001556{
1557 BIGNUM *p;
1558 int rsafail;
1559
1560 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001561 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001562
1563 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001564 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001565
1566 buffer_get_bignum2(m, p);
1567
1568 rsafail = ssh1_session_key(p);
1569
1570 buffer_clear(m);
1571 buffer_put_int(m, rsafail);
1572 buffer_put_bignum2(m, p);
1573
1574 BN_clear_free(p);
1575
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001576 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001577
1578 /* Turn on permissions for sessid passing */
1579 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1580
1581 return (0);
1582}
1583
1584int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001585mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001586{
1587 int i;
1588
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001589 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590
1591 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001592 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001593 for (i = 0; i < 16; i++)
1594 session_id[i] = buffer_get_char(m);
1595
1596 /* Turn on permissions for getpwnam */
1597 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1598
1599 return (0);
1600}
1601
1602int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001603mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001604{
1605 BIGNUM *client_n;
1606 Key *key = NULL;
1607 u_char *blob = NULL;
1608 u_int blen = 0;
1609 int allowed = 0;
1610
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001611 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001612
Damien Miller7a8f5b32006-03-31 23:14:23 +11001613 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001614 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001615 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001616 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001617 buffer_get_bignum2(m, client_n);
1618 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1619 BN_clear_free(client_n);
1620 }
1621 buffer_clear(m);
1622 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001623 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001624
1625 /* clear temporarily storage (used by generate challenge) */
1626 monitor_reset_key_state();
1627
1628 if (allowed && key != NULL) {
1629 key->type = KEY_RSA; /* cheat for key_to_blob */
1630 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001631 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632 buffer_put_string(m, blob, blen);
1633
1634 /* Save temporarily for comparison in verify */
1635 key_blob = blob;
1636 key_bloblen = blen;
1637 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638 }
Damien Miller00111382003-03-10 11:21:17 +11001639 if (key != NULL)
1640 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001641
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001642 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001643
1644 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1645 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1646 return (0);
1647}
1648
1649int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001650mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001651{
1652 Key *key = NULL;
1653 u_char *blob;
1654 u_int blen;
1655
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001656 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001657
1658 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001659 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001660 blob = buffer_get_string(m, &blen);
1661 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001662 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001663 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001664 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001665 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001666 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001667 if (key->type != KEY_RSA)
1668 fatal("%s: received bad key type %d", __func__, key->type);
1669 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001670 if (ssh1_challenge)
1671 BN_clear_free(ssh1_challenge);
1672 ssh1_challenge = auth_rsa_generate_challenge(key);
1673
1674 buffer_clear(m);
1675 buffer_put_bignum2(m, ssh1_challenge);
1676
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001677 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001678 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001679
1680 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001681
Darren Tuckera627d422013-06-02 07:31:17 +10001682 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001683 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001684 return (0);
1685}
1686
1687int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001688mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001689{
1690 Key *key = NULL;
1691 u_char *blob, *response;
1692 u_int blen, len;
1693 int success;
1694
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001695 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696
1697 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001698 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001700 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001701
1702 blob = buffer_get_string(m, &blen);
1703 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001704 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001706 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001707 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001708 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001709 response = buffer_get_string(m, &len);
1710 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001711 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1713
Darren Tuckera627d422013-06-02 07:31:17 +10001714 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001715 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001716 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001717
1718 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1719
1720 /* reset state */
1721 BN_clear_free(ssh1_challenge);
1722 ssh1_challenge = NULL;
1723 monitor_reset_key_state();
1724
1725 buffer_clear(m);
1726 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001727 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001728
1729 return (success);
1730}
Damien Miller1f0311c2014-05-15 14:24:09 +10001731#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001732
1733int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001734mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001735{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001736 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001737 int res, status;
1738
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001739 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001740
1741 /* The child is terminating */
1742 session_destroy_all(&mm_session_close);
1743
Darren Tucker52358d62008-03-11 22:58:25 +11001744#ifdef USE_PAM
1745 if (options.use_pam)
1746 sshpam_cleanup();
1747#endif
1748
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001749 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001750 if (errno != EINTR)
1751 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001752
1753 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1754
1755 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001756 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001757}
1758
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001759#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001760/* Report that an audit event occurred */
1761int
1762mm_answer_audit_event(int socket, Buffer *m)
1763{
1764 ssh_audit_event_t event;
1765
1766 debug3("%s entering", __func__);
1767
1768 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001769 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001770 case SSH_AUTH_FAIL_PUBKEY:
1771 case SSH_AUTH_FAIL_HOSTBASED:
1772 case SSH_AUTH_FAIL_GSSAPI:
1773 case SSH_LOGIN_EXCEED_MAXTRIES:
1774 case SSH_LOGIN_ROOT_DENIED:
1775 case SSH_CONNECTION_CLOSE:
1776 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001777 audit_event(event);
1778 break;
1779 default:
1780 fatal("Audit event type %d not permitted", event);
1781 }
1782
1783 return (0);
1784}
1785
1786int
1787mm_answer_audit_command(int socket, Buffer *m)
1788{
1789 u_int len;
1790 char *cmd;
1791
1792 debug3("%s entering", __func__);
1793 cmd = buffer_get_string(m, &len);
1794 /* sanity check command, if so how? */
1795 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001796 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001797 return (0);
1798}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001799#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001800
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001801void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001802monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001803{
1804 if (compat20) {
1805 set_newkeys(MODE_IN);
1806 set_newkeys(MODE_OUT);
1807 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001808 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001809 packet_set_encryption_key(child_state.ssh1key,
1810 child_state.ssh1keylen, child_state.ssh1cipher);
Darren Tuckera627d422013-06-02 07:31:17 +10001811 free(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001812 }
1813
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001814 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001815 packet_set_keycontext(MODE_OUT, child_state.keyout);
Darren Tuckera627d422013-06-02 07:31:17 +10001816 free(child_state.keyout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001817 packet_set_keycontext(MODE_IN, child_state.keyin);
Darren Tuckera627d422013-06-02 07:31:17 +10001818 free(child_state.keyin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001819
1820 if (!compat20) {
1821 packet_set_iv(MODE_OUT, child_state.ivout);
Darren Tuckera627d422013-06-02 07:31:17 +10001822 free(child_state.ivout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001823 packet_set_iv(MODE_IN, child_state.ivin);
Darren Tuckera627d422013-06-02 07:31:17 +10001824 free(child_state.ivin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001825 }
1826
1827 memcpy(&incoming_stream, &child_state.incoming,
1828 sizeof(incoming_stream));
1829 memcpy(&outgoing_stream, &child_state.outgoing,
1830 sizeof(outgoing_stream));
1831
1832 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001833 if (options.compression)
1834 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001835
Damien Millerc31a0cd2014-05-15 14:37:39 +10001836 packet_set_postauth();
1837
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001838 if (options.rekey_limit || options.rekey_interval)
1839 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1840 (time_t)options.rekey_interval);
1841
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001842 /* Network I/O buffers */
1843 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001844 buffer_clear(packet_get_input());
1845 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Damien Millera5103f42014-02-04 11:20:14 +11001846 explicit_bzero(child_state.input, child_state.ilen);
Darren Tuckera627d422013-06-02 07:31:17 +10001847 free(child_state.input);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001848
Darren Tuckerf7288d72009-06-21 18:12:20 +10001849 buffer_clear(packet_get_output());
1850 buffer_append(packet_get_output(), child_state.output,
1851 child_state.olen);
Damien Millera5103f42014-02-04 11:20:14 +11001852 explicit_bzero(child_state.output, child_state.olen);
Darren Tuckera627d422013-06-02 07:31:17 +10001853 free(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001854
1855 /* Roaming */
1856 if (compat20)
1857 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001858}
1859
1860static Kex *
1861mm_get_kex(Buffer *m)
1862{
1863 Kex *kex;
1864 void *blob;
1865 u_int bloblen;
1866
Damien Miller07d86be2006-03-26 14:19:21 +11001867 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001868 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001869 if (session_id2 == NULL ||
1870 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001871 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001872 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001873 kex->we_need = buffer_get_int(m);
Damien Miller1f0311c2014-05-15 14:24:09 +10001874#ifdef WITH_OPENSSL
Damien Millerb062c292003-03-24 09:12:09 +11001875 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001876 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001877 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001878 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001879 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Damien Miller1f0311c2014-05-15 14:24:09 +10001880#endif
Damien Millerc9831972013-11-07 12:00:23 +11001881 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001882 kex->server = 1;
1883 kex->hostkey_type = buffer_get_int(m);
1884 kex->kex_type = buffer_get_int(m);
1885 blob = buffer_get_string(m, &bloblen);
1886 buffer_init(&kex->my);
1887 buffer_append(&kex->my, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001888 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001889 blob = buffer_get_string(m, &bloblen);
1890 buffer_init(&kex->peer);
1891 buffer_append(&kex->peer, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001892 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001893 kex->done = 1;
1894 kex->flags = buffer_get_int(m);
1895 kex->client_version_string = buffer_get_string(m, NULL);
1896 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001897 kex->load_host_public_key=&get_hostkey_public_by_type;
1898 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001899 kex->host_key_index=&get_hostkey_index;
Damien Miller85b45e02013-07-20 13:21:52 +10001900 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001901
1902 return (kex);
1903}
1904
1905/* This function requries careful sanity checking */
1906
1907void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001908mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001909{
1910 Buffer m;
1911 u_char *blob, *p;
1912 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001913 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001914 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001915
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001916 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001917
1918 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001919 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001920 if (!compat20) {
1921 child_state.ssh1protoflags = buffer_get_int(&m);
1922 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001923 child_state.ssh1key = buffer_get_string(&m,
1924 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001925 child_state.ivout = buffer_get_string(&m,
1926 &child_state.ivoutlen);
1927 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1928 goto skip;
1929 } else {
1930 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001931 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001932 }
1933
1934 blob = buffer_get_string(&m, &bloblen);
1935 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001936 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001937
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001938 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001939 blob = buffer_get_string(&m, &bloblen);
1940 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001941 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001942
1943 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001944 seqnr = buffer_get_int(&m);
1945 blocks = buffer_get_int64(&m);
1946 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001947 bytes = buffer_get_int64(&m);
1948 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001949 seqnr = buffer_get_int(&m);
1950 blocks = buffer_get_int64(&m);
1951 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001952 bytes = buffer_get_int64(&m);
1953 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001954
1955 skip:
1956 /* Get the key context */
1957 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1958 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1959
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001960 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001961 /* Get compression state */
1962 p = buffer_get_string(&m, &plen);
1963 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001964 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001965 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
Darren Tuckera627d422013-06-02 07:31:17 +10001966 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001967
1968 p = buffer_get_string(&m, &plen);
1969 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001970 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001971 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
Darren Tuckera627d422013-06-02 07:31:17 +10001972 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001973
1974 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001975 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001976 child_state.input = buffer_get_string(&m, &child_state.ilen);
1977 child_state.output = buffer_get_string(&m, &child_state.olen);
1978
Darren Tuckerc5564e12009-06-21 18:53:53 +10001979 /* Roaming */
1980 if (compat20) {
1981 child_state.sent_bytes = buffer_get_int64(&m);
1982 child_state.recv_bytes = buffer_get_int64(&m);
1983 }
1984
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001985 buffer_free(&m);
1986}
1987
1988
1989/* Allocation functions for zlib */
1990void *
1991mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1992{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001993 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001994 void *address;
1995
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001996 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001997 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1998
1999 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002000
2001 return (address);
2002}
2003
2004void
2005mm_zfree(struct mm_master *mm, void *address)
2006{
2007 mm_free(mm, address);
2008}
2009
2010void
2011mm_init_compression(struct mm_master *mm)
2012{
2013 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
2014 outgoing_stream.zfree = (free_func)mm_zfree;
2015 outgoing_stream.opaque = mm;
2016
2017 incoming_stream.zalloc = (alloc_func)mm_zalloc;
2018 incoming_stream.zfree = (free_func)mm_zfree;
2019 incoming_stream.opaque = mm;
2020}
2021
2022/* XXX */
2023
2024#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10002025 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002026 fatal("fcntl(%d, F_SETFD)", x); \
2027} while (0)
2028
2029static void
Damien Miller8f0bf232011-06-20 14:42:23 +10002030monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002031{
Damien Miller8f0bf232011-06-20 14:42:23 +10002032 int pair[2];
2033
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002034 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10002035 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002036 FD_CLOSEONEXEC(pair[0]);
2037 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10002038 mon->m_recvfd = pair[0];
2039 mon->m_sendfd = pair[1];
2040
2041 if (do_logfds) {
2042 if (pipe(pair) == -1)
2043 fatal("%s: pipe: %s", __func__, strerror(errno));
2044 FD_CLOSEONEXEC(pair[0]);
2045 FD_CLOSEONEXEC(pair[1]);
2046 mon->m_log_recvfd = pair[0];
2047 mon->m_log_sendfd = pair[1];
2048 } else
2049 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002050}
2051
2052#define MM_MEMSIZE 65536
2053
2054struct monitor *
2055monitor_init(void)
2056{
2057 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002058
Damien Miller07d86be2006-03-26 14:19:21 +11002059 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002060
Damien Miller8f0bf232011-06-20 14:42:23 +10002061 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002062
2063 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10002064 if (options.compression) {
2065 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2066 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002067
Damien Miller2d6b8332002-06-21 15:59:49 +10002068 /* Compression needs to share state across borders */
2069 mm_init_compression(mon->m_zlib);
2070 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002071
2072 return mon;
2073}
2074
2075void
2076monitor_reinit(struct monitor *mon)
2077{
Damien Miller8f0bf232011-06-20 14:42:23 +10002078 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002079}
Darren Tucker0efd1552003-08-26 11:49:55 +10002080
2081#ifdef GSSAPI
2082int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002083mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002084{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002085 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002086 OM_uint32 major;
2087 u_int len;
2088
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002089 goid.elements = buffer_get_string(m, &len);
2090 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002091
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002092 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002093
Darren Tuckera627d422013-06-02 07:31:17 +10002094 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002095
2096 buffer_clear(m);
2097 buffer_put_int(m, major);
2098
Damien Miller6fd6def2005-11-05 15:07:05 +11002099 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002100
2101 /* Now we have a context, enable the step */
2102 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2103
2104 return (0);
2105}
2106
2107int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002108mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002109{
2110 gss_buffer_desc in;
2111 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002112 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002113 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002114 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002115
Darren Tucker600ad8d2003-08-26 12:10:48 +10002116 in.value = buffer_get_string(m, &len);
2117 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002118 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10002119 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10002120
2121 buffer_clear(m);
2122 buffer_put_int(m, major);
2123 buffer_put_string(m, out.value, out.length);
2124 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002125 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002126
2127 gss_release_buffer(&minor, &out);
2128
Damien Miller6fd6def2005-11-05 15:07:05 +11002129 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002130 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2131 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002132 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002133 }
2134 return (0);
2135}
2136
2137int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002138mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002139{
2140 gss_buffer_desc gssbuf, mic;
2141 OM_uint32 ret;
2142 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002143
Damien Miller0425d402003-11-17 22:18:21 +11002144 gssbuf.value = buffer_get_string(m, &len);
2145 gssbuf.length = len;
2146 mic.value = buffer_get_string(m, &len);
2147 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002148
Damien Miller0425d402003-11-17 22:18:21 +11002149 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002150
Darren Tuckera627d422013-06-02 07:31:17 +10002151 free(gssbuf.value);
2152 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002153
Damien Miller0425d402003-11-17 22:18:21 +11002154 buffer_clear(m);
2155 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002156
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002157 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002158
Damien Miller0425d402003-11-17 22:18:21 +11002159 if (!GSS_ERROR(ret))
2160 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002161
Damien Miller0425d402003-11-17 22:18:21 +11002162 return (0);
2163}
2164
2165int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002166mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002167{
2168 int authenticated;
2169
2170 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2171
2172 buffer_clear(m);
2173 buffer_put_int(m, authenticated);
2174
2175 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002176 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002177
Damien Miller6fd6def2005-11-05 15:07:05 +11002178 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002179
2180 /* Monitor loop will terminate if authenticated */
2181 return (authenticated);
2182}
2183#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002184