monitor.c revision 1.52 1 1.48 christos /* $NetBSD: monitor.c,v 1.52 2026/09/21 21:31:00 christos Exp $ */
2 1.52 christos /* $OpenBSD: monitor.c,v 1.256 2026/05/31 11:30:50 djm Exp $ */
3 1.52 christos /* $OpenBSD: monitor.c,v 1.258 2026/07/27 12:28:52 markus Exp $ */
4 1.1 christos /*
5 1.1 christos * Copyright 2002 Niels Provos <provos (at) citi.umich.edu>
6 1.1 christos * Copyright 2002 Markus Friedl <markus (at) openbsd.org>
7 1.1 christos * All rights reserved.
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted provided that the following conditions
11 1.1 christos * are met:
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos *
18 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 christos */
29 1.1 christos
30 1.2 christos #include "includes.h"
31 1.48 christos __RCSID("$NetBSD: monitor.c,v 1.52 2026/09/21 21:31:00 christos Exp $");
32 1.1 christos #include <sys/types.h>
33 1.1 christos #include <sys/wait.h>
34 1.1 christos #include <sys/socket.h>
35 1.1 christos #include <sys/tree.h>
36 1.1 christos #include <sys/queue.h>
37 1.1 christos
38 1.1 christos #include <errno.h>
39 1.1 christos #include <fcntl.h>
40 1.19 christos #include <limits.h>
41 1.1 christos #include <paths.h>
42 1.7 christos #include <poll.h>
43 1.1 christos #include <pwd.h>
44 1.1 christos #include <signal.h>
45 1.13 christos #include <stdarg.h>
46 1.21 christos #include <unistd.h>
47 1.14 christos #include <stdint.h>
48 1.13 christos #include <stdio.h>
49 1.1 christos #include <stdlib.h>
50 1.1 christos #include <string.h>
51 1.27 christos #include <unistd.h>
52 1.1 christos
53 1.51 christos #ifdef WITH_OPENSSL
54 1.51 christos #include <openssl/dh.h>
55 1.51 christos #endif
56 1.51 christos
57 1.51 christos
58 1.7 christos #include "atomicio.h"
59 1.1 christos #include "xmalloc.h"
60 1.1 christos #include "ssh.h"
61 1.27 christos #include "sshkey.h"
62 1.27 christos #include "sshbuf.h"
63 1.1 christos #include "hostfile.h"
64 1.1 christos #include "auth.h"
65 1.1 christos #include "cipher.h"
66 1.1 christos #include "kex.h"
67 1.1 christos #include "dh.h"
68 1.1 christos #include "packet.h"
69 1.1 christos #include "auth-options.h"
70 1.1 christos #include "sshpty.h"
71 1.1 christos #include "channels.h"
72 1.1 christos #include "session.h"
73 1.1 christos #include "sshlogin.h"
74 1.1 christos #include "canohost.h"
75 1.1 christos #include "log.h"
76 1.13 christos #include "misc.h"
77 1.1 christos #include "servconf.h"
78 1.1 christos #include "monitor.h"
79 1.1 christos #ifdef GSSAPI
80 1.1 christos #include "ssh-gss.h"
81 1.1 christos #endif
82 1.1 christos #include "monitor_wrap.h"
83 1.1 christos #include "monitor_fdpass.h"
84 1.1 christos #include "compat.h"
85 1.1 christos #include "ssh2.h"
86 1.11 christos #include "authfd.h"
87 1.14 christos #include "match.h"
88 1.14 christos #include "ssherr.h"
89 1.33 christos #include "sk-api.h"
90 1.46 christos #include "srclimit.h"
91 1.1 christos
92 1.32 christos #include "pfilter.h"
93 1.32 christos
94 1.1 christos #ifdef GSSAPI
95 1.1 christos static Gssctxt *gsscontext = NULL;
96 1.1 christos #endif
97 1.1 christos
98 1.1 christos /* Imports */
99 1.1 christos extern ServerOptions options;
100 1.1 christos extern u_int utmp_len;
101 1.49 christos extern struct sshbuf *cfg;
102 1.27 christos extern struct sshbuf *loginmsg;
103 1.49 christos extern struct include_list includes;
104 1.25 christos extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
105 1.1 christos
106 1.1 christos /* State exported from the child */
107 1.14 christos static struct sshbuf *child_state;
108 1.1 christos
109 1.1 christos /* Functions on the monitor that answer unprivileged requests */
110 1.1 christos
111 1.29 christos int mm_answer_moduli(struct ssh *, int, struct sshbuf *);
112 1.51 christos int mm_answer_setcompat(struct ssh *, int, struct sshbuf *);
113 1.29 christos int mm_answer_sign(struct ssh *, int, struct sshbuf *);
114 1.29 christos int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *);
115 1.29 christos int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *);
116 1.29 christos int mm_answer_authserv(struct ssh *, int, struct sshbuf *);
117 1.29 christos int mm_answer_authpassword(struct ssh *, int, struct sshbuf *);
118 1.29 christos int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *);
119 1.29 christos int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *);
120 1.29 christos int mm_answer_keyallowed(struct ssh *, int, struct sshbuf *);
121 1.29 christos int mm_answer_keyverify(struct ssh *, int, struct sshbuf *);
122 1.29 christos int mm_answer_pty(struct ssh *, int, struct sshbuf *);
123 1.29 christos int mm_answer_pty_cleanup(struct ssh *, int, struct sshbuf *);
124 1.29 christos int mm_answer_term(struct ssh *, int, struct sshbuf *);
125 1.49 christos int mm_answer_state(struct ssh *, int, struct sshbuf *);
126 1.1 christos
127 1.2 christos #ifdef USE_PAM
128 1.29 christos int mm_answer_pam_start(struct ssh *, int, struct sshbuf *);
129 1.29 christos int mm_answer_pam_account(struct ssh *, int, struct sshbuf *);
130 1.29 christos int mm_answer_pam_init_ctx(struct ssh *, int, struct sshbuf *);
131 1.29 christos int mm_answer_pam_query(struct ssh *, int, struct sshbuf *);
132 1.29 christos int mm_answer_pam_respond(struct ssh *, int, struct sshbuf *);
133 1.29 christos int mm_answer_pam_free_ctx(struct ssh *, int, struct sshbuf *);
134 1.2 christos #endif
135 1.2 christos
136 1.2 christos #ifdef KRB5
137 1.29 christos int mm_answer_krb5(struct ssh *, int, struct sshbuf *);
138 1.20 christos #endif
139 1.2 christos
140 1.1 christos #ifdef GSSAPI
141 1.29 christos int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
142 1.29 christos int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
143 1.29 christos int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
144 1.29 christos int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
145 1.1 christos #endif
146 1.1 christos
147 1.45 christos static Authctxt *authctxt;
148 1.45 christos
149 1.1 christos /* local state for key verify */
150 1.1 christos static u_char *key_blob = NULL;
151 1.27 christos static size_t key_bloblen = 0;
152 1.37 christos static u_int key_blobtype = MM_NOKEY;
153 1.25 christos static struct sshauthopt *key_opts = NULL;
154 1.1 christos static char *hostbased_cuser = NULL;
155 1.1 christos static char *hostbased_chost = NULL;
156 1.6 christos static const char *auth_method = "unknown";
157 1.10 christos static const char *auth_submethod = NULL;
158 1.1 christos static u_int session_id2_len = 0;
159 1.1 christos static u_char *session_id2 = NULL;
160 1.1 christos static pid_t monitor_child_pid;
161 1.51 christos static int auth_attempted = 0;
162 1.51 christos static int invalid_user = 0;
163 1.51 christos static int compat_set = 0;
164 1.1 christos
165 1.1 christos struct mon_table {
166 1.1 christos enum monitor_reqtype type;
167 1.1 christos int flags;
168 1.29 christos int (*f)(struct ssh *, int, struct sshbuf *);
169 1.1 christos };
170 1.1 christos
171 1.1 christos #define MON_ISAUTH 0x0004 /* Required for Authentication */
172 1.1 christos #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
173 1.1 christos #define MON_ONCE 0x0010 /* Disable after calling */
174 1.1 christos #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
175 1.1 christos
176 1.1 christos #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
177 1.1 christos
178 1.1 christos #define MON_PERMIT 0x1000 /* Request is permitted */
179 1.1 christos
180 1.29 christos static int monitor_read(struct ssh *, struct monitor *, struct mon_table *,
181 1.29 christos struct mon_table **);
182 1.29 christos static int monitor_read_log(struct monitor *);
183 1.29 christos
184 1.1 christos struct mon_table mon_dispatch_proto20[] = {
185 1.49 christos {MONITOR_REQ_STATE, MON_ONCE, mm_answer_state},
186 1.13 christos #ifdef WITH_OPENSSL
187 1.1 christos {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
188 1.13 christos #endif
189 1.51 christos {MONITOR_REQ_SETCOMPAT, MON_ONCE, mm_answer_setcompat},
190 1.1 christos {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
191 1.1 christos {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
192 1.1 christos {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
193 1.1 christos {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
194 1.1 christos {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
195 1.2 christos #ifdef USE_PAM
196 1.2 christos {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
197 1.2 christos {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
198 1.2 christos {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
199 1.2 christos {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
200 1.2 christos {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
201 1.2 christos {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
202 1.2 christos #endif
203 1.2 christos #ifdef BSD_AUTH
204 1.1 christos {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
205 1.1 christos {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
206 1.2 christos #endif
207 1.1 christos {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
208 1.1 christos {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
209 1.2 christos #ifdef KRB5
210 1.2 christos {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
211 1.2 christos #endif
212 1.1 christos #ifdef GSSAPI
213 1.1 christos {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
214 1.20 christos {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
215 1.20 christos {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
216 1.20 christos {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
217 1.1 christos #endif
218 1.1 christos {0, 0, NULL}
219 1.1 christos };
220 1.1 christos
221 1.1 christos struct mon_table mon_dispatch_postauth20[] = {
222 1.49 christos {MONITOR_REQ_STATE, MON_ONCE, mm_answer_state},
223 1.13 christos #ifdef WITH_OPENSSL
224 1.1 christos {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
225 1.13 christos #endif
226 1.1 christos {MONITOR_REQ_SIGN, 0, mm_answer_sign},
227 1.1 christos {MONITOR_REQ_PTY, 0, mm_answer_pty},
228 1.1 christos {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
229 1.1 christos {MONITOR_REQ_TERM, 0, mm_answer_term},
230 1.1 christos {0, 0, NULL}
231 1.1 christos };
232 1.1 christos
233 1.1 christos struct mon_table *mon_dispatch;
234 1.1 christos
235 1.1 christos /* Specifies if a certain message is allowed at the moment */
236 1.1 christos static void
237 1.1 christos monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
238 1.1 christos {
239 1.1 christos while (ent->f != NULL) {
240 1.1 christos if (ent->type == type) {
241 1.1 christos ent->flags &= ~MON_PERMIT;
242 1.1 christos ent->flags |= permit ? MON_PERMIT : 0;
243 1.1 christos return;
244 1.1 christos }
245 1.1 christos ent++;
246 1.1 christos }
247 1.1 christos }
248 1.1 christos
249 1.1 christos static void
250 1.1 christos monitor_permit_authentications(int permit)
251 1.1 christos {
252 1.1 christos struct mon_table *ent = mon_dispatch;
253 1.1 christos
254 1.1 christos while (ent->f != NULL) {
255 1.1 christos if (ent->flags & MON_AUTH) {
256 1.1 christos ent->flags &= ~MON_PERMIT;
257 1.1 christos ent->flags |= permit ? MON_PERMIT : 0;
258 1.1 christos }
259 1.1 christos ent++;
260 1.1 christos }
261 1.1 christos }
262 1.1 christos
263 1.1 christos void
264 1.29 christos monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
265 1.1 christos {
266 1.1 christos struct mon_table *ent;
267 1.50 christos int status, authenticated = 0, partial = 0;
268 1.1 christos
269 1.1 christos debug3("preauth child monitor started");
270 1.1 christos
271 1.45 christos authctxt = (Authctxt *)ssh->authctxt;
272 1.25 christos if (pmonitor->m_recvfd >= 0)
273 1.25 christos close(pmonitor->m_recvfd);
274 1.25 christos if (pmonitor->m_log_sendfd >= 0)
275 1.25 christos close(pmonitor->m_log_sendfd);
276 1.7 christos pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
277 1.7 christos
278 1.1 christos memset(authctxt, 0, sizeof(*authctxt));
279 1.25 christos ssh->authctxt = authctxt;
280 1.1 christos
281 1.20 christos mon_dispatch = mon_dispatch_proto20;
282 1.49 christos /* Permit requests for state, moduli and signatures */
283 1.49 christos monitor_permit(mon_dispatch, MONITOR_REQ_STATE, 1);
284 1.20 christos monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
285 1.51 christos monitor_permit(mon_dispatch, MONITOR_REQ_SETCOMPAT, 1);
286 1.20 christos monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
287 1.1 christos
288 1.1 christos /* The first few requests do not require asynchronous access */
289 1.1 christos while (!authenticated) {
290 1.10 christos partial = 0;
291 1.1 christos auth_method = "unknown";
292 1.10 christos auth_submethod = NULL;
293 1.23 christos auth2_authctxt_reset_info(authctxt);
294 1.23 christos
295 1.29 christos authenticated = (monitor_read(ssh, pmonitor,
296 1.29 christos mon_dispatch, &ent) == 1);
297 1.10 christos
298 1.45 christos /* Record that auth was attempted to set exit status later */
299 1.45 christos if ((ent->flags & MON_AUTH) != 0)
300 1.45 christos auth_attempted = 1;
301 1.45 christos
302 1.10 christos /* Special handling for multiple required authentications */
303 1.10 christos if (options.num_auth_methods != 0) {
304 1.10 christos if (authenticated &&
305 1.10 christos !auth2_update_methods_lists(authctxt,
306 1.11 christos auth_method, auth_submethod)) {
307 1.36 christos debug3_f("method %s: partial", auth_method);
308 1.10 christos authenticated = 0;
309 1.10 christos partial = 1;
310 1.10 christos }
311 1.10 christos }
312 1.10 christos
313 1.1 christos if (authenticated) {
314 1.1 christos if (!(ent->flags & MON_AUTHDECIDE))
315 1.36 christos fatal_f("unexpected authentication from %d",
316 1.36 christos ent->type);
317 1.1 christos if (authctxt->pw->pw_uid == 0 &&
318 1.25 christos !auth_root_allowed(ssh, auth_method))
319 1.1 christos authenticated = 0;
320 1.2 christos #ifdef USE_PAM
321 1.2 christos /* PAM needs to perform account checks after auth */
322 1.2 christos if (options.use_pam && authenticated) {
323 1.27 christos struct sshbuf *m;
324 1.27 christos
325 1.27 christos if ((m = sshbuf_new()) == NULL)
326 1.27 christos fatal("%s: sshbuf_new failed", __func__);
327 1.2 christos
328 1.2 christos mm_request_receive_expect(pmonitor->m_sendfd,
329 1.27 christos MONITOR_REQ_PAM_ACCOUNT, m);
330 1.29 christos authenticated = mm_answer_pam_account(ssh, pmonitor->m_sendfd, m);
331 1.27 christos sshbuf_free(m);
332 1.2 christos }
333 1.2 christos #endif
334 1.1 christos }
335 1.1 christos if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
336 1.29 christos auth_log(ssh, authenticated, partial,
337 1.11 christos auth_method, auth_submethod);
338 1.45 christos if (!partial && !authenticated)
339 1.1 christos authctxt->failures++;
340 1.23 christos if (authenticated || partial) {
341 1.23 christos auth2_update_session_info(authctxt,
342 1.23 christos auth_method, auth_submethod);
343 1.23 christos }
344 1.1 christos }
345 1.43 christos if (authctxt->failures > options.max_authtries) {
346 1.44 christos pfilter_notify(1);
347 1.43 christos /* Shouldn't happen */
348 1.43 christos fatal_f("privsep child made too many authentication "
349 1.43 christos "attempts");
350 1.43 christos }
351 1.1 christos }
352 1.1 christos
353 1.44 christos if (!authctxt->valid) {
354 1.44 christos pfilter_notify(1);
355 1.36 christos fatal_f("authenticated invalid user");
356 1.44 christos }
357 1.44 christos if (strcmp(auth_method, "unknown") == 0) {
358 1.44 christos pfilter_notify(1);
359 1.36 christos fatal_f("authentication method name unknown");
360 1.44 christos }
361 1.1 christos
362 1.36 christos debug_f("user %s authenticated by privileged process", authctxt->user);
363 1.45 christos auth_attempted = 0;
364 1.25 christos ssh->authctxt = NULL;
365 1.22 christos ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
366 1.1 christos
367 1.29 christos mm_get_keystate(ssh, pmonitor);
368 1.7 christos
369 1.10 christos /* Drain any buffered messages from the child */
370 1.10 christos while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
371 1.10 christos ;
372 1.10 christos
373 1.50 christos /* Wait for the child's exit status */
374 1.50 christos while (waitpid(pmonitor->m_pid, &status, 0) == -1) {
375 1.50 christos if (errno == EINTR)
376 1.50 christos continue;
377 1.50 christos fatal_f("waitpid: %s", strerror(errno));
378 1.50 christos }
379 1.50 christos if (WIFEXITED(status)) {
380 1.50 christos if (WEXITSTATUS(status) != 0)
381 1.50 christos fatal_f("preauth child %ld exited with status %d",
382 1.50 christos (long)pmonitor->m_pid, WEXITSTATUS(status));
383 1.50 christos } else if (WIFSIGNALED(status)) {
384 1.50 christos fatal_f("preauth child %ld terminated by signal %d",
385 1.50 christos (long)pmonitor->m_pid, WTERMSIG(status));
386 1.50 christos }
387 1.50 christos debug3_f("preauth child %ld terminated successfully",
388 1.50 christos (long)pmonitor->m_pid);
389 1.50 christos
390 1.25 christos if (pmonitor->m_recvfd >= 0)
391 1.25 christos close(pmonitor->m_recvfd);
392 1.25 christos if (pmonitor->m_log_sendfd >= 0)
393 1.25 christos close(pmonitor->m_log_sendfd);
394 1.7 christos pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
395 1.50 christos pmonitor->m_pid = -1;
396 1.1 christos }
397 1.1 christos
398 1.1 christos static void
399 1.1 christos monitor_set_child_handler(pid_t pid)
400 1.1 christos {
401 1.1 christos monitor_child_pid = pid;
402 1.1 christos }
403 1.1 christos
404 1.1 christos static void
405 1.1 christos monitor_child_handler(int sig)
406 1.1 christos {
407 1.1 christos kill(monitor_child_pid, sig);
408 1.1 christos }
409 1.1 christos
410 1.1 christos void
411 1.29 christos monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
412 1.1 christos {
413 1.7 christos close(pmonitor->m_recvfd);
414 1.7 christos pmonitor->m_recvfd = -1;
415 1.7 christos
416 1.1 christos monitor_set_child_handler(pmonitor->m_pid);
417 1.33 christos ssh_signal(SIGHUP, &monitor_child_handler);
418 1.33 christos ssh_signal(SIGTERM, &monitor_child_handler);
419 1.33 christos ssh_signal(SIGINT, &monitor_child_handler);
420 1.1 christos
421 1.20 christos mon_dispatch = mon_dispatch_postauth20;
422 1.20 christos
423 1.20 christos /* Permit requests for moduli and signatures */
424 1.49 christos monitor_permit(mon_dispatch, MONITOR_REQ_STATE, 1);
425 1.20 christos monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
426 1.20 christos monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
427 1.20 christos monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
428 1.1 christos
429 1.25 christos if (auth_opts->permit_pty_flag) {
430 1.1 christos monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
431 1.1 christos monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
432 1.1 christos }
433 1.1 christos
434 1.1 christos for (;;)
435 1.29 christos monitor_read(ssh, pmonitor, mon_dispatch, NULL);
436 1.1 christos }
437 1.1 christos
438 1.7 christos static int
439 1.7 christos monitor_read_log(struct monitor *pmonitor)
440 1.7 christos {
441 1.27 christos struct sshbuf *logmsg;
442 1.37 christos u_int len, level, forced;
443 1.37 christos char *msg;
444 1.27 christos u_char *p;
445 1.27 christos int r;
446 1.7 christos
447 1.27 christos if ((logmsg = sshbuf_new()) == NULL)
448 1.36 christos fatal_f("sshbuf_new");
449 1.7 christos
450 1.7 christos /* Read length */
451 1.27 christos if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
452 1.36 christos fatal_fr(r, "reserve len");
453 1.27 christos if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
454 1.7 christos if (errno == EPIPE) {
455 1.27 christos sshbuf_free(logmsg);
456 1.36 christos debug_f("child log fd closed");
457 1.7 christos close(pmonitor->m_log_recvfd);
458 1.7 christos pmonitor->m_log_recvfd = -1;
459 1.7 christos return -1;
460 1.7 christos }
461 1.36 christos fatal_f("log fd read: %s", strerror(errno));
462 1.7 christos }
463 1.27 christos if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
464 1.36 christos fatal_fr(r, "parse len");
465 1.7 christos if (len <= 4 || len > 8192)
466 1.36 christos fatal_f("invalid log message length %u", len);
467 1.7 christos
468 1.7 christos /* Read severity, message */
469 1.27 christos sshbuf_reset(logmsg);
470 1.27 christos if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
471 1.36 christos fatal_fr(r, "reserve msg");
472 1.27 christos if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
473 1.36 christos fatal_f("log fd read: %s", strerror(errno));
474 1.37 christos if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
475 1.37 christos (r = sshbuf_get_u32(logmsg, &forced)) != 0 ||
476 1.27 christos (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
477 1.36 christos fatal_fr(r, "parse");
478 1.7 christos
479 1.7 christos /* Log it */
480 1.7 christos if (log_level_name(level) == NULL)
481 1.36 christos fatal_f("invalid log level %u (corrupted message?)", level);
482 1.49 christos sshlogdirect(level, forced, "%s [%s]", msg,
483 1.49 christos mon_dispatch == mon_dispatch_postauth20 ? "postauth" : "preauth");
484 1.7 christos
485 1.27 christos sshbuf_free(logmsg);
486 1.11 christos free(msg);
487 1.7 christos
488 1.7 christos return 0;
489 1.7 christos }
490 1.7 christos
491 1.29 christos static int
492 1.29 christos monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent,
493 1.1 christos struct mon_table **pent)
494 1.1 christos {
495 1.27 christos struct sshbuf *m;
496 1.27 christos int r, ret;
497 1.1 christos u_char type;
498 1.7 christos struct pollfd pfd[2];
499 1.7 christos
500 1.7 christos for (;;) {
501 1.13 christos memset(&pfd, 0, sizeof(pfd));
502 1.7 christos pfd[0].fd = pmonitor->m_sendfd;
503 1.7 christos pfd[0].events = POLLIN;
504 1.7 christos pfd[1].fd = pmonitor->m_log_recvfd;
505 1.7 christos pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
506 1.7 christos if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
507 1.7 christos if (errno == EINTR || errno == EAGAIN)
508 1.7 christos continue;
509 1.36 christos fatal_f("poll: %s", strerror(errno));
510 1.7 christos }
511 1.7 christos if (pfd[1].revents) {
512 1.7 christos /*
513 1.7 christos * Drain all log messages before processing next
514 1.7 christos * monitor request.
515 1.7 christos */
516 1.7 christos monitor_read_log(pmonitor);
517 1.7 christos continue;
518 1.7 christos }
519 1.7 christos if (pfd[0].revents)
520 1.7 christos break; /* Continues below */
521 1.7 christos }
522 1.1 christos
523 1.27 christos if ((m = sshbuf_new()) == NULL)
524 1.36 christos fatal_f("sshbuf_new");
525 1.1 christos
526 1.27 christos mm_request_receive(pmonitor->m_sendfd, m);
527 1.27 christos if ((r = sshbuf_get_u8(m, &type)) != 0)
528 1.36 christos fatal_fr(r, "parse type");
529 1.1 christos
530 1.36 christos debug3_f("checking request %d", type);
531 1.1 christos
532 1.1 christos while (ent->f != NULL) {
533 1.1 christos if (ent->type == type)
534 1.1 christos break;
535 1.1 christos ent++;
536 1.1 christos }
537 1.1 christos
538 1.1 christos if (ent->f != NULL) {
539 1.1 christos if (!(ent->flags & MON_PERMIT))
540 1.36 christos fatal_f("unpermitted request %d", type);
541 1.29 christos ret = (*ent->f)(ssh, pmonitor->m_sendfd, m);
542 1.27 christos sshbuf_free(m);
543 1.1 christos
544 1.1 christos /* The child may use this request only once, disable it */
545 1.1 christos if (ent->flags & MON_ONCE) {
546 1.36 christos debug2_f("%d used once, disabling now", type);
547 1.1 christos ent->flags &= ~MON_PERMIT;
548 1.1 christos }
549 1.1 christos
550 1.1 christos if (pent != NULL)
551 1.1 christos *pent = ent;
552 1.1 christos
553 1.1 christos return ret;
554 1.1 christos }
555 1.1 christos
556 1.36 christos fatal_f("unsupported request: %d", type);
557 1.1 christos
558 1.1 christos /* NOTREACHED */
559 1.1 christos return (-1);
560 1.1 christos }
561 1.1 christos
562 1.1 christos /* allowed key state */
563 1.1 christos static int
564 1.33 christos monitor_allowed_key(const u_char *blob, u_int bloblen)
565 1.1 christos {
566 1.1 christos /* make sure key is allowed */
567 1.1 christos if (key_blob == NULL || key_bloblen != bloblen ||
568 1.4 adam timingsafe_bcmp(key_blob, blob, key_bloblen))
569 1.1 christos return (0);
570 1.1 christos return (1);
571 1.1 christos }
572 1.1 christos
573 1.1 christos static void
574 1.1 christos monitor_reset_key_state(void)
575 1.1 christos {
576 1.1 christos /* reset state */
577 1.11 christos free(key_blob);
578 1.11 christos free(hostbased_cuser);
579 1.11 christos free(hostbased_chost);
580 1.25 christos sshauthopt_free(key_opts);
581 1.1 christos key_blob = NULL;
582 1.1 christos key_bloblen = 0;
583 1.1 christos key_blobtype = MM_NOKEY;
584 1.25 christos key_opts = NULL;
585 1.1 christos hostbased_cuser = NULL;
586 1.1 christos hostbased_chost = NULL;
587 1.1 christos }
588 1.1 christos
589 1.49 christos int
590 1.50 christos mm_answer_state(struct ssh *ssh, int sock, struct sshbuf *unused)
591 1.49 christos {
592 1.52 christos struct sshbuf *m = NULL, *config = NULL, *hostkeys = NULL;
593 1.49 christos struct sshbuf *opts = NULL, *confdata = NULL;
594 1.49 christos int postauth;
595 1.49 christos int r;
596 1.49 christos
597 1.49 christos debug_f("config len %zu", sshbuf_len(cfg));
598 1.49 christos
599 1.49 christos if ((m = sshbuf_new()) == NULL ||
600 1.52 christos (config = sshbuf_new()) == NULL ||
601 1.49 christos (opts = sshbuf_new()) == NULL ||
602 1.49 christos (confdata = sshbuf_new()) == NULL)
603 1.49 christos fatal_f("sshbuf_new failed");
604 1.49 christos
605 1.49 christos hostkeys = pack_hostkeys();
606 1.52 christos if ((r = serialise_server_options(&options, &config)) != 0)
607 1.52 christos fatal_fr(r, "serialise_server_options");
608 1.49 christos
609 1.49 christos /*
610 1.49 christos * Protocol from monitor to unpriv privsep process:
611 1.52 christos * string configuration_blob
612 1.49 christos * uint64 timing_secret XXX move delays to monitor and remove
613 1.49 christos * string host_keys[] {
614 1.49 christos * string public_key
615 1.49 christos * string certificate
616 1.49 christos * }
617 1.49 christos * string server_banner
618 1.49 christos * string client_banner
619 1.49 christos * string configuration_data (postauth)
620 1.49 christos * string keystate (postauth)
621 1.49 christos * string authenticated_user (postauth)
622 1.49 christos * string session_info (postauth)
623 1.49 christos * string authopts (postauth)
624 1.49 christos */
625 1.52 christos if ((r = sshbuf_put_stringb(m, config)) != 0 ||
626 1.49 christos (r = sshbuf_put_u64(m, options.timing_secret)) != 0 ||
627 1.49 christos (r = sshbuf_put_stringb(m, hostkeys)) != 0 ||
628 1.49 christos (r = sshbuf_put_stringb(m, ssh->kex->server_version)) != 0 ||
629 1.52 christos (r = sshbuf_put_stringb(m, ssh->kex->client_version)) != 0)
630 1.49 christos fatal_fr(r, "compose config");
631 1.49 christos
632 1.49 christos postauth = (authctxt && authctxt->pw && authctxt->authenticated);
633 1.49 christos if (postauth) {
634 1.49 christos /* XXX shouldn't be reachable */
635 1.49 christos fatal_f("internal error: called in postauth");
636 1.49 christos }
637 1.49 christos
638 1.52 christos sshbuf_free(config);
639 1.49 christos sshbuf_free(opts);
640 1.49 christos sshbuf_free(confdata);
641 1.50 christos sshbuf_free(hostkeys);
642 1.49 christos
643 1.49 christos mm_request_send(sock, MONITOR_ANS_STATE, m);
644 1.50 christos sshbuf_free(m);
645 1.49 christos debug3_f("done");
646 1.49 christos
647 1.49 christos return (0);
648 1.49 christos }
649 1.49 christos
650 1.13 christos #ifdef WITH_OPENSSL
651 1.1 christos int
652 1.29 christos mm_answer_moduli(struct ssh *ssh, int sock, struct sshbuf *m)
653 1.1 christos {
654 1.1 christos DH *dh;
655 1.29 christos const BIGNUM *dh_p, *dh_g;
656 1.27 christos int r;
657 1.27 christos u_int min, want, max;
658 1.1 christos
659 1.27 christos if ((r = sshbuf_get_u32(m, &min)) != 0 ||
660 1.27 christos (r = sshbuf_get_u32(m, &want)) != 0 ||
661 1.27 christos (r = sshbuf_get_u32(m, &max)) != 0)
662 1.36 christos fatal_fr(r, "parse");
663 1.1 christos
664 1.36 christos debug3_f("got parameters: %d %d %d", min, want, max);
665 1.1 christos /* We need to check here, too, in case the child got corrupted */
666 1.1 christos if (max < min || want < min || max < want)
667 1.36 christos fatal_f("bad parameters: %d %d %d", min, want, max);
668 1.1 christos
669 1.27 christos sshbuf_reset(m);
670 1.1 christos
671 1.1 christos dh = choose_dh(min, want, max);
672 1.1 christos if (dh == NULL) {
673 1.27 christos if ((r = sshbuf_put_u8(m, 0)) != 0)
674 1.36 christos fatal_fr(r, "assemble empty");
675 1.1 christos } else {
676 1.1 christos /* Send first bignum */
677 1.29 christos DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
678 1.27 christos if ((r = sshbuf_put_u8(m, 1)) != 0 ||
679 1.29 christos (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
680 1.29 christos (r = sshbuf_put_bignum2(m, dh_g)) != 0)
681 1.36 christos fatal_fr(r, "assemble");
682 1.1 christos
683 1.1 christos DH_free(dh);
684 1.1 christos }
685 1.1 christos mm_request_send(sock, MONITOR_ANS_MODULI, m);
686 1.1 christos return (0);
687 1.1 christos }
688 1.13 christos #endif
689 1.1 christos
690 1.1 christos int
691 1.51 christos mm_answer_setcompat(struct ssh *ssh, int sock, struct sshbuf *m)
692 1.51 christos {
693 1.51 christos int r;
694 1.51 christos
695 1.51 christos debug3_f("entering");
696 1.51 christos
697 1.51 christos if ((r = sshbuf_get_u32(m, &ssh->compat)) != 0)
698 1.51 christos fatal_fr(r, "parse");
699 1.51 christos compat_set = 1;
700 1.51 christos
701 1.51 christos return (0);
702 1.51 christos }
703 1.51 christos
704 1.51 christos int
705 1.29 christos mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m)
706 1.1 christos {
707 1.14 christos extern int auth_sock; /* XXX move to state struct? */
708 1.49 christos struct sshkey *pubkey, *key;
709 1.18 christos struct sshbuf *sigbuf = NULL;
710 1.18 christos u_char *p = NULL, *signature = NULL;
711 1.18 christos char *alg = NULL;
712 1.49 christos size_t datlen, siglen;
713 1.49 christos int r, is_proof = 0, keyid;
714 1.49 christos u_int compat;
715 1.14 christos const char proof_req[] = "hostkeys-prove-00 (at) openssh.com";
716 1.52 christos static int nhostkey_proofs_done, *hostkey_proofs_done;
717 1.1 christos
718 1.36 christos debug3_f("entering");
719 1.1 christos
720 1.51 christos /* Make sure the unpriv process sent the compat bits already */
721 1.51 christos if (!compat_set)
722 1.51 christos fatal_f("state error: setcompat never called");
723 1.51 christos
724 1.49 christos if ((r = sshkey_froms(m, &pubkey)) != 0 ||
725 1.18 christos (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
726 1.49 christos (r = sshbuf_get_cstring(m, &alg, NULL)) != 0 ||
727 1.27 christos (r = sshbuf_get_u32(m, &compat)) != 0)
728 1.36 christos fatal_fr(r, "parse");
729 1.49 christos
730 1.49 christos if ((keyid = get_hostkey_index(pubkey, 1, ssh)) == -1)
731 1.49 christos fatal_f("unknown hostkey");
732 1.49 christos debug_f("hostkey %s index %d", sshkey_ssh_name(pubkey), keyid);
733 1.49 christos sshkey_free(pubkey);
734 1.1 christos
735 1.1 christos /*
736 1.6 christos * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
737 1.6 christos * SHA384 (48 bytes) and SHA512 (64 bytes).
738 1.14 christos *
739 1.14 christos * Otherwise, verify the signature request is for a hostkey
740 1.14 christos * proof.
741 1.14 christos *
742 1.14 christos * XXX perform similar check for KEX signature requests too?
743 1.14 christos * it's not trivial, since what is signed is the hash, rather
744 1.14 christos * than the full kex structure...
745 1.1 christos */
746 1.14 christos if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
747 1.14 christos /*
748 1.14 christos * Construct expected hostkey proof and compare it to what
749 1.14 christos * the client sent us.
750 1.14 christos */
751 1.14 christos if (session_id2_len == 0) /* hostkeys is never first */
752 1.36 christos fatal_f("bad data length: %zu", datlen);
753 1.14 christos if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
754 1.36 christos fatal_f("no hostkey for index %d", keyid);
755 1.52 christos if (keyid >= nhostkey_proofs_done) {
756 1.52 christos hostkey_proofs_done = xrecallocarray(
757 1.52 christos hostkey_proofs_done, nhostkey_proofs_done,
758 1.52 christos keyid + 1, sizeof(*hostkey_proofs_done));
759 1.52 christos nhostkey_proofs_done = keyid + 1;
760 1.52 christos }
761 1.52 christos if (hostkey_proofs_done[keyid]) {
762 1.52 christos fatal_f("hostkeys proof requested for %s key %d "
763 1.52 christos "multiple times", sshkey_type(key), keyid);
764 1.52 christos }
765 1.52 christos hostkey_proofs_done[keyid] = 1;
766 1.14 christos if ((sigbuf = sshbuf_new()) == NULL)
767 1.36 christos fatal_f("sshbuf_new");
768 1.14 christos if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
769 1.14 christos (r = sshbuf_put_string(sigbuf, session_id2,
770 1.18 christos session_id2_len)) != 0 ||
771 1.14 christos (r = sshkey_puts(key, sigbuf)) != 0)
772 1.36 christos fatal_fr(r, "assemble private key proof");
773 1.14 christos if (datlen != sshbuf_len(sigbuf) ||
774 1.14 christos memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
775 1.36 christos fatal_f("bad data length: %zu, hostkey proof len %zu",
776 1.36 christos datlen, sshbuf_len(sigbuf));
777 1.14 christos sshbuf_free(sigbuf);
778 1.14 christos is_proof = 1;
779 1.14 christos }
780 1.1 christos
781 1.1 christos /* save session id, it will be passed on the first call */
782 1.1 christos if (session_id2_len == 0) {
783 1.1 christos session_id2_len = datlen;
784 1.1 christos session_id2 = xmalloc(session_id2_len);
785 1.1 christos memcpy(session_id2, p, session_id2_len);
786 1.1 christos }
787 1.1 christos
788 1.11 christos if ((key = get_hostkey_by_index(keyid)) != NULL) {
789 1.18 christos if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
790 1.35 christos options.sk_provider, NULL, compat)) != 0)
791 1.36 christos fatal_fr(r, "sign");
792 1.14 christos } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
793 1.14 christos auth_sock > 0) {
794 1.14 christos if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
795 1.36 christos p, datlen, alg, compat)) != 0)
796 1.36 christos fatal_fr(r, "agent sign");
797 1.11 christos } else
798 1.36 christos fatal_f("no hostkey from index %d", keyid);
799 1.1 christos
800 1.38 christos debug3_f("%s %s signature len=%zu", alg,
801 1.38 christos is_proof ? "hostkey proof" : "KEX", siglen);
802 1.1 christos
803 1.14 christos sshbuf_reset(m);
804 1.14 christos if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
805 1.36 christos fatal_fr(r, "assemble");
806 1.1 christos
807 1.18 christos free(alg);
808 1.11 christos free(p);
809 1.11 christos free(signature);
810 1.1 christos
811 1.1 christos mm_request_send(sock, MONITOR_ANS_SIGN, m);
812 1.1 christos
813 1.1 christos /* Turn on permissions for getpwnam */
814 1.1 christos monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
815 1.1 christos
816 1.1 christos return (0);
817 1.1 christos }
818 1.1 christos
819 1.36 christos #define PUTPW(b, id) \
820 1.36 christos do { \
821 1.36 christos if ((r = sshbuf_put_string(b, \
822 1.36 christos &pwent->id, sizeof(pwent->id))) != 0) \
823 1.36 christos fatal_fr(r, "assemble %s", #id); \
824 1.36 christos } while (0)
825 1.36 christos
826 1.45 christos void
827 1.45 christos mm_encode_server_options(struct sshbuf *m)
828 1.45 christos {
829 1.45 christos int r;
830 1.52 christos struct sshbuf *config;
831 1.45 christos
832 1.52 christos if ((config = sshbuf_new()) == NULL)
833 1.52 christos fatal_f("sshbuf_new failed");
834 1.52 christos if ((r = serialise_server_options(&options, &config)) != 0)
835 1.52 christos fatal_fr(r, "serialise_server_options");
836 1.52 christos if ((r = sshbuf_put_stringb(m, config)) != 0)
837 1.45 christos fatal_fr(r, "assemble options");
838 1.52 christos sshbuf_free(config);
839 1.45 christos }
840 1.45 christos
841 1.1 christos /* Retrieves the password entry and also checks if the user is permitted */
842 1.1 christos int
843 1.29 christos mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
844 1.1 christos {
845 1.1 christos struct passwd *pwent;
846 1.27 christos int r, allowed = 0;
847 1.1 christos
848 1.36 christos debug3_f("entering");
849 1.1 christos
850 1.51 christos /* Make sure the unpriv process sent the compat bits already */
851 1.51 christos if (!compat_set)
852 1.51 christos fatal_f("state error: setcompat never called");
853 1.51 christos
854 1.1 christos if (authctxt->attempt++ != 0)
855 1.36 christos fatal_f("multiple attempts for getpwnam");
856 1.1 christos
857 1.40 christos if ((r = sshbuf_get_cstring(m, &authctxt->user, NULL)) != 0)
858 1.36 christos fatal_fr(r, "parse");
859 1.1 christos
860 1.40 christos pwent = getpwnamallow(ssh, authctxt->user);
861 1.1 christos
862 1.40 christos setproctitle("%s [priv]", pwent ? authctxt->user : "unknown");
863 1.1 christos
864 1.27 christos sshbuf_reset(m);
865 1.1 christos
866 1.1 christos if (pwent == NULL) {
867 1.51 christos invalid_user = 1;
868 1.27 christos if ((r = sshbuf_put_u8(m, 0)) != 0)
869 1.36 christos fatal_fr(r, "assemble fakepw");
870 1.1 christos authctxt->pw = fakepw();
871 1.1 christos goto out;
872 1.1 christos }
873 1.1 christos
874 1.1 christos allowed = 1;
875 1.1 christos authctxt->pw = pwent;
876 1.1 christos authctxt->valid = 1;
877 1.1 christos
878 1.36 christos /* XXX send fake class/dir/shell, etc. */
879 1.36 christos if ((r = sshbuf_put_u8(m, 1)) != 0)
880 1.36 christos fatal_fr(r, "assemble ok");
881 1.36 christos PUTPW(m, pw_uid);
882 1.36 christos PUTPW(m, pw_gid);
883 1.36 christos PUTPW(m, pw_change);
884 1.36 christos PUTPW(m, pw_expire);
885 1.36 christos if ((r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
886 1.27 christos (r = sshbuf_put_cstring(m, "*")) != 0 ||
887 1.27 christos (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
888 1.27 christos (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
889 1.27 christos (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
890 1.27 christos (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
891 1.36 christos fatal_fr(r, "assemble pw");
892 1.1 christos
893 1.1 christos out:
894 1.22 christos ssh_packet_set_log_preamble(ssh, "%suser %s",
895 1.22 christos authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
896 1.7 christos
897 1.46 christos if (options.refuse_connection) {
898 1.46 christos logit("administratively prohibited connection for "
899 1.46 christos "%s%s from %.128s port %d",
900 1.46 christos authctxt->valid ? "" : "invalid user ",
901 1.46 christos authctxt->user, ssh_remote_ipaddr(ssh),
902 1.46 christos ssh_remote_port(ssh));
903 1.46 christos cleanup_exit(EXIT_CONFIG_REFUSED);
904 1.46 christos }
905 1.46 christos
906 1.45 christos /* Send active options to unpriv */
907 1.45 christos mm_encode_server_options(m);
908 1.10 christos
909 1.10 christos /* Create valid auth method lists */
910 1.20 christos if (auth2_setup_methods_lists(authctxt) != 0) {
911 1.10 christos /*
912 1.10 christos * The monitor will continue long enough to let the child
913 1.39 christos * run to its packet_disconnect(), but it must not allow any
914 1.10 christos * authentication to succeed.
915 1.10 christos */
916 1.36 christos debug_f("no valid authentication method lists");
917 1.10 christos }
918 1.10 christos
919 1.36 christos debug3_f("sending MONITOR_ANS_PWNAM: %d", allowed);
920 1.1 christos mm_request_send(sock, MONITOR_ANS_PWNAM, m);
921 1.1 christos
922 1.20 christos /* Allow service/style information on the auth context */
923 1.20 christos monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
924 1.20 christos monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
925 1.1 christos
926 1.2 christos #ifdef USE_PAM
927 1.2 christos if (options.use_pam)
928 1.2 christos monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
929 1.2 christos #endif
930 1.1 christos
931 1.1 christos return (0);
932 1.1 christos }
933 1.1 christos
934 1.29 christos int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m)
935 1.1 christos {
936 1.1 christos char *banner;
937 1.27 christos int r;
938 1.1 christos
939 1.27 christos sshbuf_reset(m);
940 1.1 christos banner = auth2_read_banner();
941 1.27 christos if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
942 1.36 christos fatal_fr(r, "assemble");
943 1.1 christos mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
944 1.11 christos free(banner);
945 1.1 christos
946 1.1 christos return (0);
947 1.1 christos }
948 1.1 christos
949 1.1 christos int
950 1.29 christos mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
951 1.1 christos {
952 1.27 christos int r;
953 1.27 christos
954 1.1 christos monitor_permit_authentications(1);
955 1.1 christos
956 1.27 christos if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
957 1.27 christos (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
958 1.36 christos fatal_fr(r, "parse");
959 1.36 christos debug3_f("service=%s, style=%s", authctxt->service, authctxt->style);
960 1.1 christos
961 1.1 christos if (strlen(authctxt->style) == 0) {
962 1.11 christos free(authctxt->style);
963 1.1 christos authctxt->style = NULL;
964 1.1 christos }
965 1.1 christos
966 1.1 christos return (0);
967 1.1 christos }
968 1.1 christos
969 1.1 christos int
970 1.29 christos mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
971 1.1 christos {
972 1.1 christos static int call_count;
973 1.1 christos char *passwd;
974 1.27 christos int r, authenticated;
975 1.27 christos size_t plen;
976 1.1 christos
977 1.20 christos if (!options.password_authentication)
978 1.36 christos fatal_f("password authentication not enabled");
979 1.27 christos if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
980 1.36 christos fatal_fr(r, "parse");
981 1.1 christos /* Only authenticate if the context is valid */
982 1.1 christos authenticated = options.password_authentication &&
983 1.25 christos auth_password(ssh, passwd);
984 1.34 christos freezero(passwd, plen);
985 1.1 christos
986 1.27 christos sshbuf_reset(m);
987 1.27 christos if ((r = sshbuf_put_u32(m, authenticated)) != 0)
988 1.36 christos fatal_fr(r, "assemble");
989 1.45 christos #ifdef USE_PAM
990 1.45 christos if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
991 1.45 christos fatal_fr(r, "assemble PAM");
992 1.45 christos #endif
993 1.36 christos debug3_f("sending result %d", authenticated);
994 1.1 christos mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
995 1.1 christos
996 1.1 christos call_count++;
997 1.1 christos if (plen == 0 && call_count == 1)
998 1.1 christos auth_method = "none";
999 1.1 christos else
1000 1.1 christos auth_method = "password";
1001 1.1 christos
1002 1.1 christos /* Causes monitor loop to terminate if authenticated */
1003 1.1 christos return (authenticated);
1004 1.1 christos }
1005 1.1 christos
1006 1.2 christos #ifdef BSD_AUTH
1007 1.1 christos int
1008 1.29 christos mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m)
1009 1.1 christos {
1010 1.1 christos char *name, *infotxt;
1011 1.27 christos u_int numprompts, *echo_on, success;
1012 1.1 christos char **prompts;
1013 1.27 christos int r;
1014 1.1 christos
1015 1.20 christos if (!options.kbd_interactive_authentication)
1016 1.36 christos fatal_f("kbd-int authentication not enabled");
1017 1.1 christos success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
1018 1.1 christos &prompts, &echo_on) < 0 ? 0 : 1;
1019 1.1 christos
1020 1.27 christos sshbuf_reset(m);
1021 1.27 christos if ((r = sshbuf_put_u32(m, success)) != 0)
1022 1.36 christos fatal_fr(r, "assemble");
1023 1.27 christos if (success) {
1024 1.27 christos if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
1025 1.36 christos fatal_fr(r, "assemble prompt");
1026 1.27 christos }
1027 1.1 christos
1028 1.36 christos debug3_f("sending challenge success: %u", success);
1029 1.1 christos mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
1030 1.1 christos
1031 1.1 christos if (success) {
1032 1.11 christos free(name);
1033 1.11 christos free(infotxt);
1034 1.11 christos free(prompts);
1035 1.11 christos free(echo_on);
1036 1.1 christos }
1037 1.1 christos
1038 1.1 christos return (0);
1039 1.1 christos }
1040 1.1 christos
1041 1.1 christos int
1042 1.29 christos mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
1043 1.1 christos {
1044 1.1 christos char *response;
1045 1.27 christos int r, authok;
1046 1.1 christos
1047 1.20 christos if (!options.kbd_interactive_authentication)
1048 1.36 christos fatal_f("kbd-int authentication not enabled");
1049 1.18 christos if (authctxt->as == NULL)
1050 1.36 christos fatal_f("no bsd auth session");
1051 1.1 christos
1052 1.27 christos if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
1053 1.36 christos fatal_fr(r, "parse");
1054 1.38 christos authok = options.kbd_interactive_authentication &&
1055 1.1 christos auth_userresponse(authctxt->as, response, 0);
1056 1.1 christos authctxt->as = NULL;
1057 1.36 christos debug3_f("<%s> = <%d>", response, authok);
1058 1.11 christos free(response);
1059 1.1 christos
1060 1.27 christos sshbuf_reset(m);
1061 1.27 christos if ((r = sshbuf_put_u32(m, authok)) != 0)
1062 1.36 christos fatal_fr(r, "assemble");
1063 1.1 christos
1064 1.36 christos debug3_f("sending authenticated: %d", authok);
1065 1.1 christos mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
1066 1.1 christos
1067 1.20 christos auth_method = "keyboard-interactive";
1068 1.20 christos auth_submethod = "bsdauth";
1069 1.1 christos
1070 1.1 christos return (authok != 0);
1071 1.1 christos }
1072 1.2 christos #endif
1073 1.2 christos
1074 1.2 christos #ifdef SKEY
1075 1.2 christos int
1076 1.27 christos mm_answer_skeyquery(int sock, struct sshbuf *m)
1077 1.2 christos {
1078 1.2 christos struct skey skey;
1079 1.2 christos char challenge[1024];
1080 1.2 christos u_int success;
1081 1.2 christos
1082 1.2 christos success = skeychallenge(&skey, authctxt->user, challenge,
1083 1.2 christos sizeof(challenge)) < 0 ? 0 : 1;
1084 1.2 christos
1085 1.27 christos sshbuf_reset(m);
1086 1.27 christos sshbuf_put_int(m, success);
1087 1.2 christos if (success)
1088 1.27 christos sshbuf_put_cstring(m, challenge);
1089 1.2 christos
1090 1.2 christos debug3("%s: sending challenge success: %u", __func__, success);
1091 1.2 christos mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
1092 1.2 christos
1093 1.2 christos return (0);
1094 1.2 christos }
1095 1.2 christos
1096 1.2 christos int
1097 1.27 christos mm_answer_skeyrespond(int sock, struct sshbuf *m)
1098 1.2 christos {
1099 1.2 christos char *response;
1100 1.2 christos int authok;
1101 1.27 christos int r;
1102 1.2 christos
1103 1.27 christos if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
1104 1.27 christos fatal("%s: buffer error: %s", __func__, ssh_err(r));
1105 1.2 christos
1106 1.38 christos authok = (options.kbd_interactive_authentication &&
1107 1.2 christos authctxt->valid &&
1108 1.2 christos skey_haskey(authctxt->pw->pw_name) == 0 &&
1109 1.2 christos skey_passcheck(authctxt->pw->pw_name, response) != -1);
1110 1.2 christos
1111 1.11 christos free(response);
1112 1.2 christos
1113 1.27 christos sshbuf_reset(m);
1114 1.27 christos sshbuf_put_int(m, authok);
1115 1.2 christos
1116 1.2 christos debug3("%s: sending authenticated: %d", __func__, authok);
1117 1.2 christos mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
1118 1.2 christos
1119 1.2 christos auth_method = "skey";
1120 1.20 christos auth_submethod = "bsdauth";
1121 1.2 christos
1122 1.2 christos return (authok != 0);
1123 1.2 christos }
1124 1.2 christos #endif
1125 1.2 christos
1126 1.2 christos #ifdef USE_PAM
1127 1.2 christos int
1128 1.29 christos mm_answer_pam_start(struct ssh *ssh, int sock, struct sshbuf *m)
1129 1.2 christos {
1130 1.2 christos if (!options.use_pam)
1131 1.2 christos fatal("UsePAM not set, but ended up in %s anyway", __func__);
1132 1.2 christos
1133 1.29 christos start_pam(ssh);
1134 1.2 christos
1135 1.45 christos if (options.kbd_interactive_authentication)
1136 1.45 christos monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1137 1.2 christos
1138 1.2 christos return (0);
1139 1.2 christos }
1140 1.2 christos
1141 1.2 christos int
1142 1.29 christos mm_answer_pam_account(struct ssh *ssh, int sock, struct sshbuf *m)
1143 1.2 christos {
1144 1.2 christos u_int ret;
1145 1.45 christos int r;
1146 1.2 christos
1147 1.2 christos if (!options.use_pam)
1148 1.2 christos fatal("UsePAM not set, but ended up in %s anyway", __func__);
1149 1.2 christos
1150 1.2 christos ret = do_pam_account();
1151 1.2 christos
1152 1.45 christos if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1153 1.45 christos (r = sshbuf_put_stringb(m, loginmsg)) != 0)
1154 1.45 christos fatal("%s: buffer error: %s", __func__, ssh_err(r));
1155 1.2 christos
1156 1.2 christos mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1157 1.2 christos
1158 1.2 christos return (ret);
1159 1.2 christos }
1160 1.2 christos
1161 1.2 christos static void *sshpam_ctxt, *sshpam_authok;
1162 1.2 christos extern KbdintDevice sshpam_device;
1163 1.2 christos
1164 1.2 christos int
1165 1.29 christos mm_answer_pam_init_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1166 1.2 christos {
1167 1.45 christos u_int ok = 0;
1168 1.45 christos int r;
1169 1.45 christos
1170 1.2 christos debug3("%s", __func__);
1171 1.45 christos if (!options.kbd_interactive_authentication)
1172 1.45 christos fatal("%s: kbd-int authentication not enabled", __func__);
1173 1.45 christos if (sshpam_ctxt != NULL)
1174 1.45 christos fatal("%s: already called", __func__);
1175 1.29 christos sshpam_ctxt = (sshpam_device.init_ctx)(ssh->authctxt);
1176 1.2 christos sshpam_authok = NULL;
1177 1.27 christos sshbuf_reset(m);
1178 1.2 christos if (sshpam_ctxt != NULL) {
1179 1.2 christos monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1180 1.45 christos monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
1181 1.45 christos ok = 1;
1182 1.2 christos }
1183 1.45 christos if ((r = sshbuf_put_u32(m, ok)) != 0)
1184 1.45 christos fatal("%s: buffer error: %s", __func__, ssh_err(r));
1185 1.2 christos mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1186 1.2 christos return (0);
1187 1.2 christos }
1188 1.2 christos
1189 1.2 christos int
1190 1.29 christos mm_answer_pam_query(struct ssh *ssh, int sock, struct sshbuf *m)
1191 1.2 christos {
1192 1.2 christos char *name, *info, **prompts;
1193 1.6 christos u_int i, num, *echo_on;
1194 1.45 christos int r, ret;
1195 1.2 christos
1196 1.2 christos debug3("%s", __func__);
1197 1.2 christos sshpam_authok = NULL;
1198 1.45 christos if (sshpam_ctxt == NULL)
1199 1.45 christos fatal("%s: no context", __func__);
1200 1.45 christos ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1201 1.45 christos &num, &prompts, &echo_on);
1202 1.2 christos if (ret == 0 && num == 0)
1203 1.2 christos sshpam_authok = sshpam_ctxt;
1204 1.2 christos if (num > 1 || name == NULL || info == NULL)
1205 1.45 christos fatal("sshpam_device.query failed");
1206 1.45 christos monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
1207 1.27 christos sshbuf_reset(m);
1208 1.45 christos if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1209 1.45 christos (r = sshbuf_put_cstring(m, name)) != 0 ||
1210 1.45 christos (r = sshbuf_put_cstring(m, info)) != 0 ||
1211 1.45 christos (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1212 1.45 christos (r = sshbuf_put_u32(m, num)) != 0)
1213 1.45 christos fatal("%s: buffer error: %s", __func__, ssh_err(r));
1214 1.11 christos free(name);
1215 1.11 christos free(info);
1216 1.2 christos for (i = 0; i < num; ++i) {
1217 1.45 christos if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1218 1.45 christos (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1219 1.45 christos fatal("%s: buffer error: %s", __func__, ssh_err(r));
1220 1.11 christos free(prompts[i]);
1221 1.2 christos }
1222 1.45 christos free(prompts);
1223 1.45 christos free(echo_on);
1224 1.2 christos auth_method = "keyboard-interactive/pam";
1225 1.45 christos auth_submethod = "pam";
1226 1.2 christos mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1227 1.2 christos return (0);
1228 1.2 christos }
1229 1.1 christos
1230 1.2 christos int
1231 1.29 christos mm_answer_pam_respond(struct ssh *ssh, int sock, struct sshbuf *m)
1232 1.2 christos {
1233 1.2 christos char **resp;
1234 1.6 christos u_int i, num;
1235 1.45 christos int r, ret;
1236 1.2 christos
1237 1.2 christos debug3("%s", __func__);
1238 1.45 christos if (sshpam_ctxt == NULL)
1239 1.45 christos fatal("%s: no context", __func__);
1240 1.2 christos sshpam_authok = NULL;
1241 1.27 christos if ((r = sshbuf_get_u32(m, &num)) != 0)
1242 1.27 christos fatal("%s: buffer error: %s", __func__, ssh_err(r));
1243 1.45 christos if (num > PAM_MAX_NUM_MSG) {
1244 1.45 christos fatal_f("Too many PAM messages, got %u, expected <= %u",
1245 1.45 christos num, (unsigned)PAM_MAX_NUM_MSG);
1246 1.45 christos }
1247 1.2 christos if (num > 0) {
1248 1.2 christos resp = xmalloc(num * sizeof(char *));
1249 1.2 christos for (i = 0; i < num; ++i)
1250 1.27 christos if ((r = sshbuf_get_cstring(m, &resp[i], NULL)) != 0)
1251 1.27 christos fatal("%s: buffer error: %s", __func__,
1252 1.27 christos ssh_err(r));
1253 1.2 christos ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1254 1.2 christos for (i = 0; i < num; ++i)
1255 1.11 christos free(resp[i]);
1256 1.11 christos free(resp);
1257 1.2 christos } else {
1258 1.2 christos ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1259 1.2 christos }
1260 1.27 christos sshbuf_reset(m);
1261 1.45 christos if ((r = sshbuf_put_u32(m, ret)) != 0)
1262 1.45 christos fatal("%s: buffer error: %s", __func__, ssh_err(r));
1263 1.2 christos mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1264 1.2 christos auth_method = "keyboard-interactive/pam";
1265 1.45 christos auth_submethod = "pam";
1266 1.2 christos if (ret == 0)
1267 1.2 christos sshpam_authok = sshpam_ctxt;
1268 1.2 christos return (0);
1269 1.2 christos }
1270 1.2 christos
1271 1.2 christos int
1272 1.29 christos mm_answer_pam_free_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1273 1.2 christos {
1274 1.16 christos int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1275 1.2 christos
1276 1.2 christos debug3("%s", __func__);
1277 1.45 christos if (sshpam_ctxt == NULL)
1278 1.45 christos fatal("%s: no context", __func__);
1279 1.2 christos (sshpam_device.free_ctx)(sshpam_ctxt);
1280 1.16 christos sshpam_ctxt = sshpam_authok = NULL;
1281 1.27 christos sshbuf_reset(m);
1282 1.2 christos mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1283 1.45 christos /* Allow another attempt */
1284 1.45 christos monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
1285 1.2 christos auth_method = "keyboard-interactive/pam";
1286 1.45 christos auth_submethod = "pam";
1287 1.16 christos return r;
1288 1.2 christos }
1289 1.2 christos #endif
1290 1.1 christos
1291 1.29 christos /*
1292 1.29 christos * Check that the key type appears in the supplied pattern list, ignoring
1293 1.29 christos * mismatches in the signature algorithm. (Signature algorithm checks are
1294 1.29 christos * performed in the unprivileged authentication code).
1295 1.29 christos * Returns 1 on success, 0 otherwise.
1296 1.29 christos */
1297 1.29 christos static int
1298 1.29 christos key_base_type_match(const char *method, const struct sshkey *key,
1299 1.29 christos const char *list)
1300 1.29 christos {
1301 1.29 christos char *s, *l, *ol = xstrdup(list);
1302 1.29 christos int found = 0;
1303 1.29 christos
1304 1.29 christos l = ol;
1305 1.29 christos for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
1306 1.29 christos if (sshkey_type_from_name(s) == key->type) {
1307 1.29 christos found = 1;
1308 1.29 christos break;
1309 1.29 christos }
1310 1.29 christos }
1311 1.29 christos if (!found) {
1312 1.29 christos error("%s key type %s is not in permitted list %s", method,
1313 1.29 christos sshkey_ssh_name(key), list);
1314 1.29 christos }
1315 1.29 christos
1316 1.29 christos free(ol);
1317 1.29 christos return found;
1318 1.29 christos }
1319 1.29 christos
1320 1.1 christos int
1321 1.29 christos mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m)
1322 1.1 christos {
1323 1.27 christos struct sshkey *key = NULL;
1324 1.1 christos char *cuser, *chost;
1325 1.27 christos u_int pubkey_auth_attempt;
1326 1.37 christos u_int type = 0;
1327 1.25 christos int r, allowed = 0;
1328 1.25 christos struct sshauthopt *opts = NULL;
1329 1.1 christos
1330 1.36 christos debug3_f("entering");
1331 1.27 christos if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1332 1.27 christos (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1333 1.27 christos (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1334 1.27 christos (r = sshkey_froms(m, &key)) != 0 ||
1335 1.27 christos (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1336 1.36 christos fatal_fr(r, "parse");
1337 1.1 christos
1338 1.1 christos if (key != NULL && authctxt->valid) {
1339 1.1 christos switch (type) {
1340 1.1 christos case MM_USERKEY:
1341 1.1 christos auth_method = "publickey";
1342 1.25 christos if (!options.pubkey_authentication)
1343 1.25 christos break;
1344 1.25 christos if (auth2_key_already_used(authctxt, key))
1345 1.25 christos break;
1346 1.29 christos if (!key_base_type_match(auth_method, key,
1347 1.36 christos options.pubkey_accepted_algos))
1348 1.25 christos break;
1349 1.25 christos allowed = user_key_allowed(ssh, authctxt->pw, key,
1350 1.25 christos pubkey_auth_attempt, &opts);
1351 1.1 christos break;
1352 1.1 christos case MM_HOSTKEY:
1353 1.25 christos auth_method = "hostbased";
1354 1.25 christos if (!options.hostbased_authentication)
1355 1.25 christos break;
1356 1.25 christos if (auth2_key_already_used(authctxt, key))
1357 1.25 christos break;
1358 1.29 christos if (!key_base_type_match(auth_method, key,
1359 1.36 christos options.hostbased_accepted_algos))
1360 1.25 christos break;
1361 1.29 christos allowed = hostbased_key_allowed(ssh, authctxt->pw,
1362 1.1 christos cuser, chost, key);
1363 1.23 christos auth2_record_info(authctxt,
1364 1.11 christos "client user \"%.100s\", client host \"%.100s\"",
1365 1.11 christos cuser, chost);
1366 1.1 christos break;
1367 1.1 christos default:
1368 1.37 christos fatal_f("unknown key type %u", type);
1369 1.1 christos break;
1370 1.1 christos }
1371 1.1 christos }
1372 1.19 christos
1373 1.36 christos debug3_f("%s authentication%s: %s key is %s", auth_method,
1374 1.36 christos pubkey_auth_attempt ? "" : " test",
1375 1.25 christos (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1376 1.25 christos allowed ? "allowed" : "not allowed");
1377 1.12 spz
1378 1.23 christos auth2_record_key(authctxt, 0, key);
1379 1.1 christos
1380 1.1 christos /* clear temporarily storage (used by verify) */
1381 1.1 christos monitor_reset_key_state();
1382 1.1 christos
1383 1.1 christos if (allowed) {
1384 1.1 christos /* Save temporarily for comparison in verify */
1385 1.27 christos if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1386 1.36 christos fatal_fr(r, "sshkey_to_blob");
1387 1.1 christos key_blobtype = type;
1388 1.25 christos key_opts = opts;
1389 1.1 christos hostbased_cuser = cuser;
1390 1.1 christos hostbased_chost = chost;
1391 1.1 christos } else {
1392 1.1 christos /* Log failed attempt */
1393 1.29 christos auth_log(ssh, 0, 0, auth_method, NULL);
1394 1.11 christos free(cuser);
1395 1.11 christos free(chost);
1396 1.1 christos }
1397 1.27 christos sshkey_free(key);
1398 1.1 christos
1399 1.27 christos sshbuf_reset(m);
1400 1.27 christos if ((r = sshbuf_put_u32(m, allowed)) != 0)
1401 1.36 christos fatal_fr(r, "assemble");
1402 1.25 christos if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1403 1.36 christos fatal_fr(r, "sshauthopt_serialise");
1404 1.25 christos mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1405 1.1 christos
1406 1.25 christos if (!allowed)
1407 1.25 christos sshauthopt_free(opts);
1408 1.1 christos
1409 1.1 christos return (0);
1410 1.1 christos }
1411 1.1 christos
1412 1.1 christos static int
1413 1.33 christos monitor_valid_userblob(struct ssh *ssh, const u_char *data, u_int datalen)
1414 1.1 christos {
1415 1.27 christos struct sshbuf *b;
1416 1.39 christos struct sshkey *hostkey = NULL;
1417 1.27 christos const u_char *p;
1418 1.19 christos char *userstyle, *cp;
1419 1.27 christos size_t len;
1420 1.27 christos u_char type;
1421 1.39 christos int hostbound = 0, r, fail = 0;
1422 1.1 christos
1423 1.33 christos if ((b = sshbuf_from(data, datalen)) == NULL)
1424 1.36 christos fatal_f("sshbuf_from");
1425 1.1 christos
1426 1.36 christos if (ssh->compat & SSH_OLD_SESSIONID) {
1427 1.27 christos p = sshbuf_ptr(b);
1428 1.27 christos len = sshbuf_len(b);
1429 1.1 christos if ((session_id2 == NULL) ||
1430 1.1 christos (len < session_id2_len) ||
1431 1.4 adam (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1432 1.1 christos fail++;
1433 1.27 christos if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1434 1.36 christos fatal_fr(r, "consume");
1435 1.1 christos } else {
1436 1.27 christos if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1437 1.36 christos fatal_fr(r, "parse sessionid");
1438 1.1 christos if ((session_id2 == NULL) ||
1439 1.1 christos (len != session_id2_len) ||
1440 1.4 adam (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1441 1.1 christos fail++;
1442 1.1 christos }
1443 1.27 christos if ((r = sshbuf_get_u8(b, &type)) != 0)
1444 1.36 christos fatal_fr(r, "parse type");
1445 1.27 christos if (type != SSH2_MSG_USERAUTH_REQUEST)
1446 1.1 christos fail++;
1447 1.27 christos if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1448 1.36 christos fatal_fr(r, "parse userstyle");
1449 1.11 christos xasprintf(&userstyle, "%s%s%s", authctxt->user,
1450 1.11 christos authctxt->style ? ":" : "",
1451 1.11 christos authctxt->style ? authctxt->style : "");
1452 1.19 christos if (strcmp(userstyle, cp) != 0) {
1453 1.19 christos logit("wrong user name passed to monitor: "
1454 1.19 christos "expected %s != %.100s", userstyle, cp);
1455 1.1 christos fail++;
1456 1.1 christos }
1457 1.11 christos free(userstyle);
1458 1.19 christos free(cp);
1459 1.27 christos if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1460 1.27 christos (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1461 1.36 christos fatal_fr(r, "parse method");
1462 1.39 christos if (strcmp("publickey", cp) != 0) {
1463 1.39 christos if (strcmp("publickey-hostbound-v00 (at) openssh.com", cp) == 0)
1464 1.39 christos hostbound = 1;
1465 1.39 christos else
1466 1.39 christos fail++;
1467 1.39 christos }
1468 1.25 christos free(cp);
1469 1.27 christos if ((r = sshbuf_get_u8(b, &type)) != 0)
1470 1.36 christos fatal_fr(r, "parse pktype");
1471 1.27 christos if (type == 0)
1472 1.25 christos fail++;
1473 1.27 christos if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1474 1.39 christos (r = sshbuf_skip_string(b)) != 0 || /* pkblob */
1475 1.39 christos (hostbound && (r = sshkey_froms(b, &hostkey)) != 0))
1476 1.36 christos fatal_fr(r, "parse pk");
1477 1.27 christos if (sshbuf_len(b) != 0)
1478 1.1 christos fail++;
1479 1.27 christos sshbuf_free(b);
1480 1.39 christos if (hostkey != NULL) {
1481 1.39 christos /*
1482 1.39 christos * Ensure this is actually one of our hostkeys; unfortunately
1483 1.39 christos * can't check ssh->kex->initial_hostkey directly at this point
1484 1.39 christos * as packet state has not yet been exported to monitor.
1485 1.39 christos */
1486 1.39 christos if (get_hostkey_index(hostkey, 1, ssh) == -1)
1487 1.39 christos fatal_f("hostbound hostkey does not match");
1488 1.39 christos sshkey_free(hostkey);
1489 1.39 christos }
1490 1.1 christos return (fail == 0);
1491 1.1 christos }
1492 1.1 christos
1493 1.1 christos static int
1494 1.33 christos monitor_valid_hostbasedblob(struct ssh *ssh, const u_char *data, u_int datalen,
1495 1.33 christos const char *cuser, const char *chost)
1496 1.1 christos {
1497 1.27 christos struct sshbuf *b;
1498 1.27 christos const u_char *p;
1499 1.27 christos char *cp, *userstyle;
1500 1.27 christos size_t len;
1501 1.27 christos int r, fail = 0;
1502 1.27 christos u_char type;
1503 1.1 christos
1504 1.33 christos if ((b = sshbuf_from(data, datalen)) == NULL)
1505 1.36 christos fatal_f("sshbuf_new");
1506 1.33 christos if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1507 1.36 christos fatal_fr(r, "parse sessionid");
1508 1.1 christos
1509 1.1 christos if ((session_id2 == NULL) ||
1510 1.1 christos (len != session_id2_len) ||
1511 1.4 adam (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1512 1.1 christos fail++;
1513 1.1 christos
1514 1.27 christos if ((r = sshbuf_get_u8(b, &type)) != 0)
1515 1.36 christos fatal_fr(r, "parse type");
1516 1.27 christos if (type != SSH2_MSG_USERAUTH_REQUEST)
1517 1.1 christos fail++;
1518 1.27 christos if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1519 1.36 christos fatal_fr(r, "parse userstyle");
1520 1.11 christos xasprintf(&userstyle, "%s%s%s", authctxt->user,
1521 1.11 christos authctxt->style ? ":" : "",
1522 1.11 christos authctxt->style ? authctxt->style : "");
1523 1.27 christos if (strcmp(userstyle, cp) != 0) {
1524 1.27 christos logit("wrong user name passed to monitor: "
1525 1.27 christos "expected %s != %.100s", userstyle, cp);
1526 1.1 christos fail++;
1527 1.1 christos }
1528 1.11 christos free(userstyle);
1529 1.27 christos free(cp);
1530 1.27 christos if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1531 1.27 christos (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1532 1.36 christos fatal_fr(r, "parse method");
1533 1.27 christos if (strcmp(cp, "hostbased") != 0)
1534 1.1 christos fail++;
1535 1.27 christos free(cp);
1536 1.27 christos if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1537 1.27 christos (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1538 1.36 christos fatal_fr(r, "parse pk");
1539 1.1 christos
1540 1.1 christos /* verify client host, strip trailing dot if necessary */
1541 1.27 christos if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1542 1.36 christos fatal_fr(r, "parse host");
1543 1.27 christos if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1544 1.27 christos cp[len - 1] = '\0';
1545 1.27 christos if (strcmp(cp, chost) != 0)
1546 1.1 christos fail++;
1547 1.27 christos free(cp);
1548 1.1 christos
1549 1.1 christos /* verify client user */
1550 1.27 christos if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1551 1.36 christos fatal_fr(r, "parse ruser");
1552 1.27 christos if (strcmp(cp, cuser) != 0)
1553 1.1 christos fail++;
1554 1.27 christos free(cp);
1555 1.1 christos
1556 1.27 christos if (sshbuf_len(b) != 0)
1557 1.1 christos fail++;
1558 1.27 christos sshbuf_free(b);
1559 1.1 christos return (fail == 0);
1560 1.1 christos }
1561 1.1 christos
1562 1.1 christos int
1563 1.29 christos mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m)
1564 1.1 christos {
1565 1.23 christos struct sshkey *key;
1566 1.33 christos const u_char *signature, *data, *blob;
1567 1.33 christos char *sigalg = NULL, *fp = NULL;
1568 1.23 christos size_t signaturelen, datalen, bloblen;
1569 1.35 christos int r, ret, req_presence = 0, req_verify = 0, valid_data = 0;
1570 1.35 christos int encoded_ret;
1571 1.33 christos struct sshkey_sig_details *sig_details = NULL;
1572 1.1 christos
1573 1.33 christos if ((r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
1574 1.33 christos (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 ||
1575 1.33 christos (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 ||
1576 1.25 christos (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
1577 1.36 christos fatal_fr(r, "parse");
1578 1.1 christos
1579 1.1 christos if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1580 1.1 christos !monitor_allowed_key(blob, bloblen))
1581 1.36 christos fatal_f("bad key, not previously allowed");
1582 1.1 christos
1583 1.25 christos /* Empty signature algorithm means NULL. */
1584 1.25 christos if (*sigalg == '\0') {
1585 1.25 christos free(sigalg);
1586 1.25 christos sigalg = NULL;
1587 1.25 christos }
1588 1.25 christos
1589 1.23 christos /* XXX use sshkey_froms here; need to change key_blob, etc. */
1590 1.23 christos if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1591 1.36 christos fatal_fr(r, "parse key");
1592 1.1 christos
1593 1.1 christos switch (key_blobtype) {
1594 1.1 christos case MM_USERKEY:
1595 1.29 christos valid_data = monitor_valid_userblob(ssh, data, datalen);
1596 1.23 christos auth_method = "publickey";
1597 1.1 christos break;
1598 1.1 christos case MM_HOSTKEY:
1599 1.29 christos valid_data = monitor_valid_hostbasedblob(ssh, data, datalen,
1600 1.1 christos hostbased_cuser, hostbased_chost);
1601 1.23 christos auth_method = "hostbased";
1602 1.1 christos break;
1603 1.1 christos default:
1604 1.1 christos valid_data = 0;
1605 1.1 christos break;
1606 1.1 christos }
1607 1.1 christos if (!valid_data)
1608 1.36 christos fatal_f("bad %s signature data blob",
1609 1.36 christos key_blobtype == MM_USERKEY ? "userkey" :
1610 1.36 christos (key_blobtype == MM_HOSTKEY ? "hostkey" : "unknown"));
1611 1.1 christos
1612 1.33 christos if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
1613 1.33 christos SSH_FP_DEFAULT)) == NULL)
1614 1.36 christos fatal_f("sshkey_fingerprint failed");
1615 1.33 christos
1616 1.23 christos ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1617 1.33 christos sigalg, ssh->compat, &sig_details);
1618 1.39 christos debug3_f("%s %s signature using %s %s%s%s", auth_method,
1619 1.39 christos sshkey_type(key), sigalg == NULL ? "default" : sigalg,
1620 1.33 christos (ret == 0) ? "verified" : "unverified",
1621 1.33 christos (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
1622 1.33 christos
1623 1.33 christos if (ret == 0 && key_blobtype == MM_USERKEY && sig_details != NULL) {
1624 1.33 christos req_presence = (options.pubkey_auth_options &
1625 1.33 christos PUBKEYAUTH_TOUCH_REQUIRED) ||
1626 1.33 christos !key_opts->no_require_user_presence;
1627 1.33 christos if (req_presence &&
1628 1.33 christos (sig_details->sk_flags & SSH_SK_USER_PRESENCE_REQD) == 0) {
1629 1.33 christos error("public key %s %s signature for %s%s from %.128s "
1630 1.33 christos "port %d rejected: user presence "
1631 1.33 christos "(authenticator touch) requirement not met ",
1632 1.33 christos sshkey_type(key), fp,
1633 1.33 christos authctxt->valid ? "" : "invalid user ",
1634 1.33 christos authctxt->user, ssh_remote_ipaddr(ssh),
1635 1.33 christos ssh_remote_port(ssh));
1636 1.33 christos ret = SSH_ERR_SIGNATURE_INVALID;
1637 1.33 christos }
1638 1.35 christos req_verify = (options.pubkey_auth_options &
1639 1.35 christos PUBKEYAUTH_VERIFY_REQUIRED) || key_opts->require_verify;
1640 1.35 christos if (req_verify &&
1641 1.35 christos (sig_details->sk_flags & SSH_SK_USER_VERIFICATION_REQD) == 0) {
1642 1.35 christos error("public key %s %s signature for %s%s from %.128s "
1643 1.35 christos "port %d rejected: user verification requirement "
1644 1.35 christos "not met ", sshkey_type(key), fp,
1645 1.35 christos authctxt->valid ? "" : "invalid user ",
1646 1.35 christos authctxt->user, ssh_remote_ipaddr(ssh),
1647 1.35 christos ssh_remote_port(ssh));
1648 1.35 christos ret = SSH_ERR_SIGNATURE_INVALID;
1649 1.35 christos }
1650 1.33 christos }
1651 1.23 christos auth2_record_key(authctxt, ret == 0, key);
1652 1.14 christos
1653 1.46 christos if (key_blobtype == MM_USERKEY && ret == 0)
1654 1.25 christos auth_activate_options(ssh, key_opts);
1655 1.23 christos monitor_reset_key_state();
1656 1.1 christos
1657 1.23 christos sshbuf_reset(m);
1658 1.1 christos
1659 1.23 christos /* encode ret != 0 as positive integer, since we're sending u32 */
1660 1.23 christos encoded_ret = (ret != 0);
1661 1.33 christos if ((r = sshbuf_put_u32(m, encoded_ret)) != 0 ||
1662 1.33 christos (r = sshbuf_put_u8(m, sig_details != NULL)) != 0)
1663 1.36 christos fatal_fr(r, "assemble");
1664 1.33 christos if (sig_details != NULL) {
1665 1.33 christos if ((r = sshbuf_put_u32(m, sig_details->sk_counter)) != 0 ||
1666 1.33 christos (r = sshbuf_put_u8(m, sig_details->sk_flags)) != 0)
1667 1.36 christos fatal_fr(r, "assemble sk");
1668 1.33 christos }
1669 1.33 christos sshkey_sig_details_free(sig_details);
1670 1.1 christos mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1671 1.1 christos
1672 1.33 christos free(sigalg);
1673 1.33 christos free(fp);
1674 1.33 christos sshkey_free(key);
1675 1.33 christos
1676 1.23 christos return ret == 0;
1677 1.1 christos }
1678 1.1 christos
1679 1.1 christos static void
1680 1.29 christos mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw)
1681 1.1 christos {
1682 1.1 christos socklen_t fromlen;
1683 1.1 christos struct sockaddr_storage from;
1684 1.1 christos
1685 1.1 christos /*
1686 1.1 christos * Get IP address of client. If the connection is not a socket, let
1687 1.1 christos * the address be 0.0.0.0.
1688 1.1 christos */
1689 1.1 christos memset(&from, 0, sizeof(from));
1690 1.1 christos fromlen = sizeof(from);
1691 1.29 christos if (ssh_packet_connection_is_on_socket(ssh)) {
1692 1.29 christos if (getpeername(ssh_packet_get_connection_in(ssh),
1693 1.30 christos (struct sockaddr *)&from, &fromlen) == -1) {
1694 1.1 christos debug("getpeername: %.100s", strerror(errno));
1695 1.47 christos cleanup_exit(255);
1696 1.1 christos }
1697 1.1 christos }
1698 1.1 christos /* Record that there was a login on that tty from the remote host. */
1699 1.1 christos record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1700 1.19 christos session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1701 1.1 christos (struct sockaddr *)&from, fromlen);
1702 1.1 christos }
1703 1.1 christos
1704 1.1 christos static void
1705 1.1 christos mm_session_close(Session *s)
1706 1.1 christos {
1707 1.36 christos debug3_f("session %d pid %ld", s->self, (long)s->pid);
1708 1.1 christos if (s->ttyfd != -1) {
1709 1.36 christos debug3_f("tty %s ptyfd %d", s->tty, s->ptyfd);
1710 1.1 christos session_pty_cleanup2(s);
1711 1.1 christos }
1712 1.1 christos session_unused(s->self);
1713 1.1 christos }
1714 1.1 christos
1715 1.1 christos int
1716 1.29 christos mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
1717 1.1 christos {
1718 1.1 christos extern struct monitor *pmonitor;
1719 1.1 christos Session *s;
1720 1.27 christos int r, res, fd0;
1721 1.1 christos
1722 1.36 christos debug3_f("entering");
1723 1.1 christos
1724 1.27 christos sshbuf_reset(m);
1725 1.1 christos s = session_new();
1726 1.1 christos if (s == NULL)
1727 1.1 christos goto error;
1728 1.1 christos s->authctxt = authctxt;
1729 1.1 christos s->pw = authctxt->pw;
1730 1.1 christos s->pid = pmonitor->m_pid;
1731 1.1 christos res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1732 1.1 christos if (res == 0)
1733 1.1 christos goto error;
1734 1.1 christos pty_setowner(authctxt->pw, s->tty);
1735 1.1 christos
1736 1.27 christos if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1737 1.27 christos (r = sshbuf_put_cstring(m, s->tty)) != 0)
1738 1.36 christos fatal_fr(r, "assemble");
1739 1.1 christos
1740 1.1 christos /* We need to trick ttyslot */
1741 1.1 christos if (dup2(s->ttyfd, 0) == -1)
1742 1.36 christos fatal_f("dup2");
1743 1.1 christos
1744 1.29 christos mm_record_login(ssh, s, authctxt->pw);
1745 1.1 christos
1746 1.1 christos /* Now we can close the file descriptor again */
1747 1.1 christos close(0);
1748 1.1 christos
1749 1.1 christos /* send messages generated by record_login */
1750 1.27 christos if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1751 1.36 christos fatal_fr(r, "assemble loginmsg");
1752 1.27 christos sshbuf_reset(loginmsg);
1753 1.1 christos
1754 1.1 christos mm_request_send(sock, MONITOR_ANS_PTY, m);
1755 1.1 christos
1756 1.1 christos if (mm_send_fd(sock, s->ptyfd) == -1 ||
1757 1.1 christos mm_send_fd(sock, s->ttyfd) == -1)
1758 1.36 christos fatal_f("send fds failed");
1759 1.1 christos
1760 1.1 christos /* make sure nothing uses fd 0 */
1761 1.30 christos if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1762 1.36 christos fatal_f("open(/dev/null): %s", strerror(errno));
1763 1.1 christos if (fd0 != 0)
1764 1.36 christos error_f("fd0 %d != 0", fd0);
1765 1.1 christos
1766 1.35 christos /* slave side of pty is not needed */
1767 1.1 christos close(s->ttyfd);
1768 1.1 christos s->ttyfd = s->ptyfd;
1769 1.1 christos /* no need to dup() because nobody closes ptyfd */
1770 1.1 christos s->ptymaster = s->ptyfd;
1771 1.1 christos
1772 1.36 christos debug3_f("tty %s ptyfd %d", s->tty, s->ttyfd);
1773 1.1 christos
1774 1.1 christos return (0);
1775 1.1 christos
1776 1.1 christos error:
1777 1.1 christos if (s != NULL)
1778 1.1 christos mm_session_close(s);
1779 1.27 christos if ((r = sshbuf_put_u32(m, 0)) != 0)
1780 1.36 christos fatal_fr(r, "assemble 0");
1781 1.1 christos mm_request_send(sock, MONITOR_ANS_PTY, m);
1782 1.1 christos return (0);
1783 1.1 christos }
1784 1.1 christos
1785 1.1 christos int
1786 1.29 christos mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m)
1787 1.1 christos {
1788 1.1 christos Session *s;
1789 1.1 christos char *tty;
1790 1.27 christos int r;
1791 1.1 christos
1792 1.36 christos debug3_f("entering");
1793 1.1 christos
1794 1.27 christos if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1795 1.36 christos fatal_fr(r, "parse tty");
1796 1.1 christos if ((s = session_by_tty(tty)) != NULL)
1797 1.1 christos mm_session_close(s);
1798 1.27 christos sshbuf_reset(m);
1799 1.11 christos free(tty);
1800 1.1 christos return (0);
1801 1.1 christos }
1802 1.1 christos
1803 1.2 christos #ifdef KRB5
1804 1.2 christos int
1805 1.29 christos mm_answer_krb5(struct ssh *ssh, int xsocket, struct sshbuf *m)
1806 1.2 christos {
1807 1.2 christos krb5_data tkt, reply;
1808 1.2 christos char *client_user;
1809 1.27 christos unsigned char *data;
1810 1.27 christos size_t len;
1811 1.27 christos int r;
1812 1.2 christos int success;
1813 1.2 christos
1814 1.2 christos /* use temporary var to avoid size issues on 64bit arch */
1815 1.27 christos if ((r = sshbuf_get_string(m, &data, &len)) != 0)
1816 1.27 christos fatal("%s: buffer error: %s", __func__, ssh_err(r));
1817 1.27 christos tkt.data = data;
1818 1.2 christos tkt.length = len;
1819 1.2 christos
1820 1.2 christos success = options.kerberos_authentication &&
1821 1.2 christos authctxt->valid &&
1822 1.29 christos auth_krb5(ssh, &tkt, &client_user, &reply);
1823 1.2 christos
1824 1.2 christos if (tkt.length)
1825 1.11 christos free(tkt.data);
1826 1.2 christos
1827 1.27 christos sshbuf_reset(m);
1828 1.27 christos sshbuf_put_u32(m, success);
1829 1.2 christos
1830 1.2 christos if (success) {
1831 1.27 christos sshbuf_put_cstring(m, client_user);
1832 1.27 christos sshbuf_put_string(m, reply.data, reply.length);
1833 1.2 christos if (client_user)
1834 1.11 christos free(client_user);
1835 1.2 christos if (reply.length)
1836 1.11 christos free(reply.data);
1837 1.2 christos }
1838 1.6 christos mm_request_send(xsocket, MONITOR_ANS_KRB5, m);
1839 1.2 christos
1840 1.2 christos auth_method = "kerberos";
1841 1.2 christos
1842 1.2 christos return success;
1843 1.2 christos }
1844 1.2 christos #endif
1845 1.2 christos
1846 1.1 christos int
1847 1.29 christos mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
1848 1.1 christos {
1849 1.1 christos extern struct monitor *pmonitor;
1850 1.1 christos int res, status;
1851 1.1 christos
1852 1.36 christos debug3_f("tearing down sessions");
1853 1.1 christos
1854 1.1 christos /* The child is terminating */
1855 1.23 christos session_destroy_all(ssh, &mm_session_close);
1856 1.1 christos
1857 1.45 christos #ifdef USE_PAM
1858 1.45 christos if (options.use_pam)
1859 1.45 christos sshpam_cleanup();
1860 1.45 christos #endif
1861 1.45 christos
1862 1.1 christos while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1863 1.1 christos if (errno != EINTR)
1864 1.1 christos exit(1);
1865 1.1 christos
1866 1.1 christos res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1867 1.1 christos
1868 1.1 christos /* Terminate process */
1869 1.1 christos exit(res);
1870 1.1 christos }
1871 1.1 christos
1872 1.1 christos void
1873 1.29 christos monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor)
1874 1.23 christos {
1875 1.23 christos ssh_clear_newkeys(ssh, MODE_IN);
1876 1.23 christos ssh_clear_newkeys(ssh, MODE_OUT);
1877 1.23 christos sshbuf_free(child_state);
1878 1.23 christos child_state = NULL;
1879 1.23 christos }
1880 1.23 christos
1881 1.23 christos void
1882 1.29 christos monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
1883 1.1 christos {
1884 1.14 christos struct kex *kex;
1885 1.14 christos int r;
1886 1.14 christos
1887 1.36 christos debug3_f("packet_set_state");
1888 1.14 christos if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1889 1.36 christos fatal_fr(r, "packet_set_state");
1890 1.14 christos sshbuf_free(child_state);
1891 1.14 christos child_state = NULL;
1892 1.36 christos if ((kex = ssh->kex) == NULL)
1893 1.36 christos fatal_f("internal error: ssh->kex == NULL");
1894 1.36 christos if (session_id2_len != sshbuf_len(ssh->kex->session_id)) {
1895 1.36 christos fatal_f("incorrect session id length %zu (expected %u)",
1896 1.36 christos sshbuf_len(ssh->kex->session_id), session_id2_len);
1897 1.36 christos }
1898 1.36 christos if (memcmp(sshbuf_ptr(ssh->kex->session_id), session_id2,
1899 1.36 christos session_id2_len) != 0)
1900 1.36 christos fatal_f("session ID mismatch");
1901 1.36 christos /* XXX set callbacks */
1902 1.15 christos #ifdef WITH_OPENSSL
1903 1.36 christos kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
1904 1.36 christos kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
1905 1.36 christos kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
1906 1.36 christos kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
1907 1.36 christos kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
1908 1.36 christos kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1909 1.36 christos kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1910 1.36 christos kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
1911 1.36 christos #endif
1912 1.36 christos kex->kex[KEX_C25519_SHA256] = kex_gen_server;
1913 1.36 christos kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
1914 1.46 christos kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_server;
1915 1.52 christos kex->kex[KEX_KEM_MLKEM768ECDH_SHA256] = kex_gen_server;
1916 1.36 christos kex->load_host_public_key=&get_hostkey_public_by_type;
1917 1.36 christos kex->load_host_private_key=&get_hostkey_private_by_type;
1918 1.36 christos kex->host_key_index=&get_hostkey_index;
1919 1.36 christos kex->sign = sshd_hostkey_sign;
1920 1.1 christos }
1921 1.1 christos
1922 1.34 christos /* This function requires careful sanity checking */
1923 1.1 christos
1924 1.1 christos void
1925 1.29 christos mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
1926 1.1 christos {
1927 1.36 christos debug3_f("Waiting for new keys");
1928 1.1 christos
1929 1.14 christos if ((child_state = sshbuf_new()) == NULL)
1930 1.36 christos fatal_f("sshbuf_new failed");
1931 1.14 christos mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1932 1.14 christos child_state);
1933 1.36 christos debug3_f("GOT new keys");
1934 1.1 christos }
1935 1.1 christos
1936 1.1 christos
1937 1.1 christos /* XXX */
1938 1.1 christos
1939 1.1 christos static void
1940 1.7 christos monitor_openfds(struct monitor *mon, int do_logfds)
1941 1.1 christos {
1942 1.7 christos int pair[2];
1943 1.23 christos #ifdef SO_ZEROIZE
1944 1.23 christos int on = 1;
1945 1.23 christos #endif
1946 1.7 christos
1947 1.1 christos if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1948 1.36 christos fatal_f("socketpair: %s", strerror(errno));
1949 1.23 christos #ifdef SO_ZEROIZE
1950 1.30 christos if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1951 1.23 christos error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1952 1.30 christos if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1953 1.23 christos error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1954 1.23 christos #endif
1955 1.1 christos FD_CLOSEONEXEC(pair[0]);
1956 1.1 christos FD_CLOSEONEXEC(pair[1]);
1957 1.7 christos mon->m_recvfd = pair[0];
1958 1.7 christos mon->m_sendfd = pair[1];
1959 1.7 christos
1960 1.7 christos if (do_logfds) {
1961 1.7 christos if (pipe(pair) == -1)
1962 1.36 christos fatal_f("pipe: %s", strerror(errno));
1963 1.7 christos FD_CLOSEONEXEC(pair[0]);
1964 1.7 christos FD_CLOSEONEXEC(pair[1]);
1965 1.7 christos mon->m_log_recvfd = pair[0];
1966 1.7 christos mon->m_log_sendfd = pair[1];
1967 1.7 christos } else
1968 1.7 christos mon->m_log_recvfd = mon->m_log_sendfd = -1;
1969 1.1 christos }
1970 1.1 christos
1971 1.1 christos struct monitor *
1972 1.1 christos monitor_init(void)
1973 1.1 christos {
1974 1.1 christos struct monitor *mon;
1975 1.1 christos
1976 1.1 christos mon = xcalloc(1, sizeof(*mon));
1977 1.7 christos monitor_openfds(mon, 1);
1978 1.1 christos
1979 1.1 christos return mon;
1980 1.1 christos }
1981 1.1 christos
1982 1.1 christos void
1983 1.1 christos monitor_reinit(struct monitor *mon)
1984 1.1 christos {
1985 1.7 christos monitor_openfds(mon, 0);
1986 1.1 christos }
1987 1.1 christos
1988 1.51 christos int
1989 1.51 christos monitor_auth_attempted(void)
1990 1.51 christos {
1991 1.51 christos return auth_attempted;
1992 1.51 christos }
1993 1.51 christos
1994 1.51 christos int
1995 1.51 christos monitor_invalid_user(void)
1996 1.51 christos {
1997 1.51 christos return invalid_user;
1998 1.51 christos }
1999 1.51 christos
2000 1.1 christos #ifdef GSSAPI
2001 1.1 christos int
2002 1.29 christos mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
2003 1.1 christos {
2004 1.1 christos gss_OID_desc goid;
2005 1.1 christos OM_uint32 major;
2006 1.27 christos size_t len;
2007 1.27 christos u_char *p;
2008 1.27 christos int r;
2009 1.1 christos
2010 1.20 christos if (!options.gss_authentication)
2011 1.36 christos fatal_f("GSSAPI authentication not enabled");
2012 1.20 christos
2013 1.27 christos if ((r = sshbuf_get_string(m, &p, &len)) != 0)
2014 1.36 christos fatal_fr(r, "parse");
2015 1.27 christos goid.elements = p;
2016 1.1 christos goid.length = len;
2017 1.1 christos
2018 1.1 christos major = ssh_gssapi_server_ctx(&gsscontext, &goid);
2019 1.1 christos
2020 1.11 christos free(goid.elements);
2021 1.1 christos
2022 1.27 christos sshbuf_reset(m);
2023 1.27 christos if ((r = sshbuf_put_u32(m, major)) != 0)
2024 1.36 christos fatal_fr(r, "assemble");
2025 1.1 christos
2026 1.1 christos mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
2027 1.1 christos
2028 1.1 christos /* Now we have a context, enable the step */
2029 1.1 christos monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2030 1.1 christos
2031 1.1 christos return (0);
2032 1.1 christos }
2033 1.1 christos
2034 1.1 christos int
2035 1.29 christos mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
2036 1.1 christos {
2037 1.1 christos gss_buffer_desc in;
2038 1.1 christos gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
2039 1.1 christos OM_uint32 major, minor;
2040 1.1 christos OM_uint32 flags = 0; /* GSI needs this */
2041 1.27 christos int r;
2042 1.1 christos
2043 1.20 christos if (!options.gss_authentication)
2044 1.36 christos fatal_f("GSSAPI authentication not enabled");
2045 1.20 christos
2046 1.27 christos if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
2047 1.36 christos fatal_fr(r, "ssh_gssapi_get_buffer_desc");
2048 1.1 christos major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
2049 1.11 christos free(in.value);
2050 1.1 christos
2051 1.27 christos sshbuf_reset(m);
2052 1.27 christos if ((r = sshbuf_put_u32(m, major)) != 0 ||
2053 1.27 christos (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
2054 1.27 christos (r = sshbuf_put_u32(m, flags)) != 0)
2055 1.36 christos fatal_fr(r, "assemble");
2056 1.1 christos mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
2057 1.1 christos
2058 1.1 christos gss_release_buffer(&minor, &out);
2059 1.1 christos
2060 1.1 christos if (major == GSS_S_COMPLETE) {
2061 1.1 christos monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2062 1.1 christos monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2063 1.1 christos monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2064 1.1 christos }
2065 1.1 christos return (0);
2066 1.1 christos }
2067 1.1 christos
2068 1.1 christos int
2069 1.29 christos mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
2070 1.1 christos {
2071 1.1 christos gss_buffer_desc gssbuf, mic;
2072 1.1 christos OM_uint32 ret;
2073 1.27 christos int r;
2074 1.1 christos
2075 1.20 christos if (!options.gss_authentication)
2076 1.36 christos fatal_f("GSSAPI authentication not enabled");
2077 1.20 christos
2078 1.27 christos if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
2079 1.27 christos (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
2080 1.36 christos fatal_fr(r, "ssh_gssapi_get_buffer_desc");
2081 1.1 christos
2082 1.1 christos ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2083 1.1 christos
2084 1.11 christos free(gssbuf.value);
2085 1.11 christos free(mic.value);
2086 1.1 christos
2087 1.27 christos sshbuf_reset(m);
2088 1.27 christos if ((r = sshbuf_put_u32(m, ret)) != 0)
2089 1.36 christos fatal_fr(r, "assemble");
2090 1.1 christos
2091 1.1 christos mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2092 1.1 christos
2093 1.1 christos if (!GSS_ERROR(ret))
2094 1.1 christos monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2095 1.1 christos
2096 1.1 christos return (0);
2097 1.1 christos }
2098 1.1 christos
2099 1.1 christos int
2100 1.29 christos mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
2101 1.1 christos {
2102 1.27 christos int r, authenticated;
2103 1.23 christos const char *displayname;
2104 1.1 christos
2105 1.20 christos if (!options.gss_authentication)
2106 1.36 christos fatal_f("GSSAPI authentication not enabled");
2107 1.20 christos
2108 1.1 christos authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2109 1.1 christos
2110 1.27 christos sshbuf_reset(m);
2111 1.27 christos if ((r = sshbuf_put_u32(m, authenticated)) != 0)
2112 1.36 christos fatal_fr(r, "assemble");
2113 1.1 christos
2114 1.36 christos debug3_f("sending result %d", authenticated);
2115 1.1 christos mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2116 1.1 christos
2117 1.1 christos auth_method = "gssapi-with-mic";
2118 1.1 christos
2119 1.23 christos if ((displayname = ssh_gssapi_displayname()) != NULL)
2120 1.23 christos auth2_record_info(authctxt, "%s", displayname);
2121 1.23 christos
2122 1.1 christos /* Monitor loop will terminate if authenticated */
2123 1.1 christos return (authenticated);
2124 1.1 christos }
2125 1.1 christos #endif /* GSSAPI */
2126 1.1 christos
2127