blob: c36c890d890292520911d74a14a081c5168d356e [file] [log] [blame]
Greg Hartman9768ca42017-06-22 20:49:52 -07001/* $OpenBSD: monitor.c,v 1.167 2017/02/03 23:05:57 djm Exp $ */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
29
30#include <sys/types.h>
Greg Hartmanbd77cf72015-02-25 13:21:06 -080031#include <sys/socket.h>
32#include "openbsd-compat/sys-tree.h"
33#include <sys/wait.h>
34
35#include <errno.h>
36#include <fcntl.h>
Greg Hartman9768ca42017-06-22 20:49:52 -070037#include <limits.h>
Greg Hartmanbd77cf72015-02-25 13:21:06 -080038#ifdef HAVE_PATHS_H
39#include <paths.h>
40#endif
41#include <pwd.h>
42#include <signal.h>
Adam Langleyd0592972015-03-30 14:49:51 -070043#ifdef HAVE_STDINT_H
44#include <stdint.h>
45#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -080046#include <stdlib.h>
47#include <string.h>
Adam Langleyd0592972015-03-30 14:49:51 -070048#include <stdarg.h>
49#include <stdio.h>
Greg Hartmanbd77cf72015-02-25 13:21:06 -080050#include <unistd.h>
51#ifdef HAVE_POLL_H
52#include <poll.h>
53#else
54# ifdef HAVE_SYS_POLL_H
55# include <sys/poll.h>
56# endif
57#endif
58
59#ifdef SKEY
60#include <skey.h>
61#endif
62
Adam Langleyd0592972015-03-30 14:49:51 -070063#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -080064#include <openssl/dh.h>
Adam Langleyd0592972015-03-30 14:49:51 -070065#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -080066
67#include "openbsd-compat/sys-queue.h"
68#include "atomicio.h"
69#include "xmalloc.h"
70#include "ssh.h"
71#include "key.h"
72#include "buffer.h"
73#include "hostfile.h"
74#include "auth.h"
75#include "cipher.h"
76#include "kex.h"
77#include "dh.h"
Greg Hartman9768ca42017-06-22 20:49:52 -070078#include "auth-pam.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080079#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
80#undef TARGET_OS_MAC
81#include "zlib.h"
82#define TARGET_OS_MAC 1
83#else
84#include "zlib.h"
85#endif
86#include "packet.h"
87#include "auth-options.h"
88#include "sshpty.h"
89#include "channels.h"
90#include "session.h"
91#include "sshlogin.h"
92#include "canohost.h"
93#include "log.h"
Adam Langleyd0592972015-03-30 14:49:51 -070094#include "misc.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080095#include "servconf.h"
96#include "monitor.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080097#ifdef GSSAPI
98#include "ssh-gss.h"
99#endif
100#include "monitor_wrap.h"
101#include "monitor_fdpass.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800102#include "compat.h"
103#include "ssh2.h"
Adam Langleyd0592972015-03-30 14:49:51 -0700104#include "authfd.h"
105#include "match.h"
106#include "ssherr.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800107
108#ifdef GSSAPI
109static Gssctxt *gsscontext = NULL;
110#endif
111
112/* Imports */
113extern ServerOptions options;
114extern u_int utmp_len;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800115extern u_char session_id[];
116extern Buffer auth_debug;
117extern int auth_debug_init;
118extern Buffer loginmsg;
119
120/* State exported from the child */
Adam Langleyd0592972015-03-30 14:49:51 -0700121static struct sshbuf *child_state;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800122
123/* Functions on the monitor that answer unprivileged requests */
124
125int mm_answer_moduli(int, Buffer *);
126int mm_answer_sign(int, Buffer *);
127int mm_answer_pwnamallow(int, Buffer *);
128int mm_answer_auth2_read_banner(int, Buffer *);
129int mm_answer_authserv(int, Buffer *);
130int mm_answer_authpassword(int, Buffer *);
131int mm_answer_bsdauthquery(int, Buffer *);
132int mm_answer_bsdauthrespond(int, Buffer *);
133int mm_answer_skeyquery(int, Buffer *);
134int mm_answer_skeyrespond(int, Buffer *);
135int mm_answer_keyallowed(int, Buffer *);
136int mm_answer_keyverify(int, Buffer *);
137int mm_answer_pty(int, Buffer *);
138int mm_answer_pty_cleanup(int, Buffer *);
139int mm_answer_term(int, Buffer *);
140int mm_answer_rsa_keyallowed(int, Buffer *);
141int mm_answer_rsa_challenge(int, Buffer *);
142int mm_answer_rsa_response(int, Buffer *);
143int mm_answer_sesskey(int, Buffer *);
144int mm_answer_sessid(int, Buffer *);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800145
146#ifdef USE_PAM
147int mm_answer_pam_start(int, Buffer *);
148int mm_answer_pam_account(int, Buffer *);
149int mm_answer_pam_init_ctx(int, Buffer *);
150int mm_answer_pam_query(int, Buffer *);
151int mm_answer_pam_respond(int, Buffer *);
152int mm_answer_pam_free_ctx(int, Buffer *);
153#endif
154
155#ifdef GSSAPI
156int mm_answer_gss_setup_ctx(int, Buffer *);
157int mm_answer_gss_accept_ctx(int, Buffer *);
158int mm_answer_gss_userok(int, Buffer *);
159int mm_answer_gss_checkmic(int, Buffer *);
160#endif
161
162#ifdef SSH_AUDIT_EVENTS
163int mm_answer_audit_event(int, Buffer *);
164int mm_answer_audit_command(int, Buffer *);
165#endif
166
167static int monitor_read_log(struct monitor *);
168
169static Authctxt *authctxt;
Adam Langleyd0592972015-03-30 14:49:51 -0700170
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800171/* local state for key verify */
172static u_char *key_blob = NULL;
173static u_int key_bloblen = 0;
174static int key_blobtype = MM_NOKEY;
175static char *hostbased_cuser = NULL;
176static char *hostbased_chost = NULL;
177static char *auth_method = "unknown";
Adam Langleyd0592972015-03-30 14:49:51 -0700178static char *auth_submethod = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800179static u_int session_id2_len = 0;
180static u_char *session_id2 = NULL;
181static pid_t monitor_child_pid;
182
183struct mon_table {
184 enum monitor_reqtype type;
185 int flags;
186 int (*f)(int, Buffer *);
187};
188
189#define MON_ISAUTH 0x0004 /* Required for Authentication */
190#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
191#define MON_ONCE 0x0010 /* Disable after calling */
192#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
193
194#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
195
196#define MON_PERMIT 0x1000 /* Request is permitted */
197
198struct mon_table mon_dispatch_proto20[] = {
Adam Langleyd0592972015-03-30 14:49:51 -0700199#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800200 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Adam Langleyd0592972015-03-30 14:49:51 -0700201#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800202 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
203 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
204 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
205 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
206 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
207#ifdef USE_PAM
208 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
209 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Greg Hartman9768ca42017-06-22 20:49:52 -0700210 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
211 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
212 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800213 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
214#endif
215#ifdef SSH_AUDIT_EVENTS
216 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
217#endif
218#ifdef BSD_AUTH
219 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
220 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
221#endif
222#ifdef SKEY
223 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
224 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
225#endif
226 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
227 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
228#ifdef GSSAPI
229 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
Greg Hartman9768ca42017-06-22 20:49:52 -0700230 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
231 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
232 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800233#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800234 {0, 0, NULL}
235};
236
237struct mon_table mon_dispatch_postauth20[] = {
Adam Langleyd0592972015-03-30 14:49:51 -0700238#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800239 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Adam Langleyd0592972015-03-30 14:49:51 -0700240#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800241 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
242 {MONITOR_REQ_PTY, 0, mm_answer_pty},
243 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
244 {MONITOR_REQ_TERM, 0, mm_answer_term},
245#ifdef SSH_AUDIT_EVENTS
246 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
247 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
248#endif
249 {0, 0, NULL}
250};
251
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800252struct mon_table *mon_dispatch;
253
254/* Specifies if a certain message is allowed at the moment */
255
256static void
257monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
258{
259 while (ent->f != NULL) {
260 if (ent->type == type) {
261 ent->flags &= ~MON_PERMIT;
262 ent->flags |= permit ? MON_PERMIT : 0;
263 return;
264 }
265 ent++;
266 }
267}
268
269static void
270monitor_permit_authentications(int permit)
271{
272 struct mon_table *ent = mon_dispatch;
273
274 while (ent->f != NULL) {
275 if (ent->flags & MON_AUTH) {
276 ent->flags &= ~MON_PERMIT;
277 ent->flags |= permit ? MON_PERMIT : 0;
278 }
279 ent++;
280 }
281}
282
283void
284monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
285{
Greg Hartman9768ca42017-06-22 20:49:52 -0700286 struct ssh *ssh = active_state; /* XXX */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800287 struct mon_table *ent;
Adam Langleyd0592972015-03-30 14:49:51 -0700288 int authenticated = 0, partial = 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800289
290 debug3("preauth child monitor started");
291
292 close(pmonitor->m_recvfd);
293 close(pmonitor->m_log_sendfd);
294 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
295
296 authctxt = _authctxt;
297 memset(authctxt, 0, sizeof(*authctxt));
298
299 authctxt->loginmsg = &loginmsg;
300
Greg Hartman9768ca42017-06-22 20:49:52 -0700301 mon_dispatch = mon_dispatch_proto20;
302 /* Permit requests for moduli and signatures */
303 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
304 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800305
306 /* The first few requests do not require asynchronous access */
307 while (!authenticated) {
Adam Langleyd0592972015-03-30 14:49:51 -0700308 partial = 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800309 auth_method = "unknown";
Adam Langleyd0592972015-03-30 14:49:51 -0700310 auth_submethod = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800311 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Adam Langleyd0592972015-03-30 14:49:51 -0700312
313 /* Special handling for multiple required authentications */
314 if (options.num_auth_methods != 0) {
Adam Langleyd0592972015-03-30 14:49:51 -0700315 if (authenticated &&
316 !auth2_update_methods_lists(authctxt,
317 auth_method, auth_submethod)) {
318 debug3("%s: method %s: partial", __func__,
319 auth_method);
320 authenticated = 0;
321 partial = 1;
322 }
323 }
324
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800325 if (authenticated) {
326 if (!(ent->flags & MON_AUTHDECIDE))
327 fatal("%s: unexpected authentication from %d",
328 __func__, ent->type);
329 if (authctxt->pw->pw_uid == 0 &&
330 !auth_root_allowed(auth_method))
331 authenticated = 0;
332#ifdef USE_PAM
333 /* PAM needs to perform account checks after auth */
334 if (options.use_pam && authenticated) {
335 Buffer m;
336
337 buffer_init(&m);
338 mm_request_receive_expect(pmonitor->m_sendfd,
339 MONITOR_REQ_PAM_ACCOUNT, &m);
340 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
341 buffer_free(&m);
342 }
343#endif
344 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800345 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Adam Langleyd0592972015-03-30 14:49:51 -0700346 auth_log(authctxt, authenticated, partial,
347 auth_method, auth_submethod);
Greg Hartmanccacbc92016-02-03 09:59:44 -0800348 if (!partial && !authenticated)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800349 authctxt->failures++;
350 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800351 }
352
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800353 if (!authctxt->valid)
354 fatal("%s: authenticated invalid user", __func__);
355 if (strcmp(auth_method, "unknown") == 0)
356 fatal("%s: authentication method name unknown", __func__);
357
358 debug("%s: %s has been authenticated by privileged process",
359 __func__, authctxt->user);
Greg Hartman9768ca42017-06-22 20:49:52 -0700360 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800361
362 mm_get_keystate(pmonitor);
363
Adam Langleyd0592972015-03-30 14:49:51 -0700364 /* Drain any buffered messages from the child */
365 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
366 ;
367
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800368 close(pmonitor->m_sendfd);
369 close(pmonitor->m_log_recvfd);
370 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
371}
372
373static void
374monitor_set_child_handler(pid_t pid)
375{
376 monitor_child_pid = pid;
377}
378
379static void
380monitor_child_handler(int sig)
381{
382 kill(monitor_child_pid, sig);
383}
384
385void
386monitor_child_postauth(struct monitor *pmonitor)
387{
388 close(pmonitor->m_recvfd);
389 pmonitor->m_recvfd = -1;
390
391 monitor_set_child_handler(pmonitor->m_pid);
392 signal(SIGHUP, &monitor_child_handler);
393 signal(SIGTERM, &monitor_child_handler);
394 signal(SIGINT, &monitor_child_handler);
Adam Langleyd0592972015-03-30 14:49:51 -0700395#ifdef SIGXFSZ
396 signal(SIGXFSZ, SIG_IGN);
397#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800398
Greg Hartman9768ca42017-06-22 20:49:52 -0700399 mon_dispatch = mon_dispatch_postauth20;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800400
Greg Hartman9768ca42017-06-22 20:49:52 -0700401 /* Permit requests for moduli and signatures */
402 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
403 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
404 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
405
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800406 if (!no_pty_flag) {
407 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
408 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
409 }
410
411 for (;;)
412 monitor_read(pmonitor, mon_dispatch, NULL);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800413}
414
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800415static int
416monitor_read_log(struct monitor *pmonitor)
417{
418 Buffer logmsg;
419 u_int len, level;
420 char *msg;
421
422 buffer_init(&logmsg);
423
424 /* Read length */
425 buffer_append_space(&logmsg, 4);
426 if (atomicio(read, pmonitor->m_log_recvfd,
427 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
428 if (errno == EPIPE) {
Adam Langleyd0592972015-03-30 14:49:51 -0700429 buffer_free(&logmsg);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800430 debug("%s: child log fd closed", __func__);
431 close(pmonitor->m_log_recvfd);
432 pmonitor->m_log_recvfd = -1;
433 return -1;
434 }
435 fatal("%s: log fd read: %s", __func__, strerror(errno));
436 }
437 len = buffer_get_int(&logmsg);
438 if (len <= 4 || len > 8192)
439 fatal("%s: invalid log message length %u", __func__, len);
440
441 /* Read severity, message */
442 buffer_clear(&logmsg);
443 buffer_append_space(&logmsg, len);
444 if (atomicio(read, pmonitor->m_log_recvfd,
445 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
446 fatal("%s: log fd read: %s", __func__, strerror(errno));
447
448 /* Log it */
449 level = buffer_get_int(&logmsg);
450 msg = buffer_get_string(&logmsg, NULL);
451 if (log_level_name(level) == NULL)
452 fatal("%s: invalid log level %u (corrupted message?)",
453 __func__, level);
454 do_log2(level, "%s [preauth]", msg);
455
456 buffer_free(&logmsg);
Adam Langleyd0592972015-03-30 14:49:51 -0700457 free(msg);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800458
459 return 0;
460}
461
462int
463monitor_read(struct monitor *pmonitor, struct mon_table *ent,
464 struct mon_table **pent)
465{
466 Buffer m;
467 int ret;
468 u_char type;
469 struct pollfd pfd[2];
470
471 for (;;) {
Adam Langleyd0592972015-03-30 14:49:51 -0700472 memset(&pfd, 0, sizeof(pfd));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800473 pfd[0].fd = pmonitor->m_sendfd;
474 pfd[0].events = POLLIN;
475 pfd[1].fd = pmonitor->m_log_recvfd;
476 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
477 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
478 if (errno == EINTR || errno == EAGAIN)
479 continue;
480 fatal("%s: poll: %s", __func__, strerror(errno));
481 }
482 if (pfd[1].revents) {
483 /*
484 * Drain all log messages before processing next
485 * monitor request.
486 */
487 monitor_read_log(pmonitor);
488 continue;
489 }
490 if (pfd[0].revents)
491 break; /* Continues below */
492 }
493
494 buffer_init(&m);
495
496 mm_request_receive(pmonitor->m_sendfd, &m);
497 type = buffer_get_char(&m);
498
499 debug3("%s: checking request %d", __func__, type);
500
501 while (ent->f != NULL) {
502 if (ent->type == type)
503 break;
504 ent++;
505 }
506
507 if (ent->f != NULL) {
508 if (!(ent->flags & MON_PERMIT))
509 fatal("%s: unpermitted request %d", __func__,
510 type);
511 ret = (*ent->f)(pmonitor->m_sendfd, &m);
512 buffer_free(&m);
513
514 /* The child may use this request only once, disable it */
515 if (ent->flags & MON_ONCE) {
516 debug2("%s: %d used once, disabling now", __func__,
517 type);
518 ent->flags &= ~MON_PERMIT;
519 }
520
521 if (pent != NULL)
522 *pent = ent;
523
524 return ret;
525 }
526
527 fatal("%s: unsupported request: %d", __func__, type);
528
529 /* NOTREACHED */
530 return (-1);
531}
532
533/* allowed key state */
534static int
535monitor_allowed_key(u_char *blob, u_int bloblen)
536{
537 /* make sure key is allowed */
538 if (key_blob == NULL || key_bloblen != bloblen ||
539 timingsafe_bcmp(key_blob, blob, key_bloblen))
540 return (0);
541 return (1);
542}
543
544static void
545monitor_reset_key_state(void)
546{
547 /* reset state */
Adam Langleyd0592972015-03-30 14:49:51 -0700548 free(key_blob);
549 free(hostbased_cuser);
550 free(hostbased_chost);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800551 key_blob = NULL;
552 key_bloblen = 0;
553 key_blobtype = MM_NOKEY;
554 hostbased_cuser = NULL;
555 hostbased_chost = NULL;
556}
557
Adam Langleyd0592972015-03-30 14:49:51 -0700558#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800559int
560mm_answer_moduli(int sock, Buffer *m)
561{
562 DH *dh;
563 int min, want, max;
564
565 min = buffer_get_int(m);
566 want = buffer_get_int(m);
567 max = buffer_get_int(m);
568
569 debug3("%s: got parameters: %d %d %d",
570 __func__, min, want, max);
571 /* We need to check here, too, in case the child got corrupted */
572 if (max < min || want < min || max < want)
573 fatal("%s: bad parameters: %d %d %d",
574 __func__, min, want, max);
575
576 buffer_clear(m);
577
578 dh = choose_dh(min, want, max);
579 if (dh == NULL) {
580 buffer_put_char(m, 0);
581 return (0);
582 } else {
583 /* Send first bignum */
584 buffer_put_char(m, 1);
585 buffer_put_bignum2(m, dh->p);
586 buffer_put_bignum2(m, dh->g);
587
588 DH_free(dh);
589 }
590 mm_request_send(sock, MONITOR_ANS_MODULI, m);
591 return (0);
592}
Adam Langleyd0592972015-03-30 14:49:51 -0700593#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800594
595int
596mm_answer_sign(int sock, Buffer *m)
597{
Adam Langleyd0592972015-03-30 14:49:51 -0700598 struct ssh *ssh = active_state; /* XXX */
599 extern int auth_sock; /* XXX move to state struct? */
600 struct sshkey *key;
Greg Hartman9768ca42017-06-22 20:49:52 -0700601 struct sshbuf *sigbuf = NULL;
602 u_char *p = NULL, *signature = NULL;
603 char *alg = NULL;
604 size_t datlen, siglen, alglen;
605 int r, is_proof = 0;
606 u_int keyid;
Adam Langleyd0592972015-03-30 14:49:51 -0700607 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800608
609 debug3("%s", __func__);
610
Adam Langleyd0592972015-03-30 14:49:51 -0700611 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
Greg Hartman9768ca42017-06-22 20:49:52 -0700612 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
613 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
Adam Langleyd0592972015-03-30 14:49:51 -0700614 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartman9768ca42017-06-22 20:49:52 -0700615 if (keyid > INT_MAX)
616 fatal("%s: invalid key ID", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800617
618 /*
619 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
620 * SHA384 (48 bytes) and SHA512 (64 bytes).
Adam Langleyd0592972015-03-30 14:49:51 -0700621 *
622 * Otherwise, verify the signature request is for a hostkey
623 * proof.
624 *
625 * XXX perform similar check for KEX signature requests too?
626 * it's not trivial, since what is signed is the hash, rather
627 * than the full kex structure...
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800628 */
Adam Langleyd0592972015-03-30 14:49:51 -0700629 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
630 /*
631 * Construct expected hostkey proof and compare it to what
632 * the client sent us.
633 */
634 if (session_id2_len == 0) /* hostkeys is never first */
635 fatal("%s: bad data length: %zu", __func__, datlen);
636 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
637 fatal("%s: no hostkey for index %d", __func__, keyid);
638 if ((sigbuf = sshbuf_new()) == NULL)
639 fatal("%s: sshbuf_new", __func__);
640 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
641 (r = sshbuf_put_string(sigbuf, session_id2,
Greg Hartman9768ca42017-06-22 20:49:52 -0700642 session_id2_len)) != 0 ||
Adam Langleyd0592972015-03-30 14:49:51 -0700643 (r = sshkey_puts(key, sigbuf)) != 0)
644 fatal("%s: couldn't prepare private key "
645 "proof buffer: %s", __func__, ssh_err(r));
646 if (datlen != sshbuf_len(sigbuf) ||
647 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
648 fatal("%s: bad data length: %zu, hostkey proof len %zu",
649 __func__, datlen, sshbuf_len(sigbuf));
650 sshbuf_free(sigbuf);
651 is_proof = 1;
652 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800653
654 /* save session id, it will be passed on the first call */
655 if (session_id2_len == 0) {
656 session_id2_len = datlen;
657 session_id2 = xmalloc(session_id2_len);
658 memcpy(session_id2, p, session_id2_len);
659 }
660
Adam Langleyd0592972015-03-30 14:49:51 -0700661 if ((key = get_hostkey_by_index(keyid)) != NULL) {
Greg Hartman9768ca42017-06-22 20:49:52 -0700662 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
Adam Langleyd0592972015-03-30 14:49:51 -0700663 datafellows)) != 0)
664 fatal("%s: sshkey_sign failed: %s",
665 __func__, ssh_err(r));
666 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
667 auth_sock > 0) {
668 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
Greg Hartman9768ca42017-06-22 20:49:52 -0700669 p, datlen, alg, datafellows)) != 0) {
Adam Langleyd0592972015-03-30 14:49:51 -0700670 fatal("%s: ssh_agent_sign failed: %s",
671 __func__, ssh_err(r));
672 }
673 } else
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800674 fatal("%s: no hostkey from index %d", __func__, keyid);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800675
Adam Langleyd0592972015-03-30 14:49:51 -0700676 debug3("%s: %s signature %p(%zu)", __func__,
677 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800678
Adam Langleyd0592972015-03-30 14:49:51 -0700679 sshbuf_reset(m);
680 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
681 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800682
Greg Hartman9768ca42017-06-22 20:49:52 -0700683 free(alg);
Adam Langleyd0592972015-03-30 14:49:51 -0700684 free(p);
685 free(signature);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800686
687 mm_request_send(sock, MONITOR_ANS_SIGN, m);
688
689 /* Turn on permissions for getpwnam */
690 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
691
692 return (0);
693}
694
695/* Retrieves the password entry and also checks if the user is permitted */
696
697int
698mm_answer_pwnamallow(int sock, Buffer *m)
699{
Greg Hartman9768ca42017-06-22 20:49:52 -0700700 struct ssh *ssh = active_state; /* XXX */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800701 char *username;
702 struct passwd *pwent;
703 int allowed = 0;
704 u_int i;
705
706 debug3("%s", __func__);
707
708 if (authctxt->attempt++ != 0)
709 fatal("%s: multiple attempts for getpwnam", __func__);
710
711 username = buffer_get_string(m, NULL);
712
713 pwent = getpwnamallow(username);
714
715 authctxt->user = xstrdup(username);
716 setproctitle("%s [priv]", pwent ? username : "unknown");
Adam Langleyd0592972015-03-30 14:49:51 -0700717 free(username);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800718
719 buffer_clear(m);
720
721 if (pwent == NULL) {
722 buffer_put_char(m, 0);
723 authctxt->pw = fakepw();
724 goto out;
725 }
726
727 allowed = 1;
728 authctxt->pw = pwent;
729 authctxt->valid = 1;
730
731 buffer_put_char(m, 1);
732 buffer_put_string(m, pwent, sizeof(struct passwd));
733 buffer_put_cstring(m, pwent->pw_name);
734 buffer_put_cstring(m, "*");
Adam Langleyd0592972015-03-30 14:49:51 -0700735#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800736 buffer_put_cstring(m, pwent->pw_gecos);
737#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700738#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800739 buffer_put_cstring(m, pwent->pw_class);
740#endif
741 buffer_put_cstring(m, pwent->pw_dir);
742 buffer_put_cstring(m, pwent->pw_shell);
743
744 out:
Greg Hartman9768ca42017-06-22 20:49:52 -0700745 ssh_packet_set_log_preamble(ssh, "%suser %s",
746 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800747 buffer_put_string(m, &options, sizeof(options));
748
749#define M_CP_STROPT(x) do { \
750 if (options.x != NULL) \
751 buffer_put_cstring(m, options.x); \
752 } while (0)
753#define M_CP_STRARRAYOPT(x, nx) do { \
754 for (i = 0; i < options.nx; i++) \
755 buffer_put_cstring(m, options.x[i]); \
756 } while (0)
757 /* See comment in servconf.h */
758 COPY_MATCH_STRING_OPTS();
759#undef M_CP_STROPT
760#undef M_CP_STRARRAYOPT
Adam Langleyd0592972015-03-30 14:49:51 -0700761
762 /* Create valid auth method lists */
Greg Hartman9768ca42017-06-22 20:49:52 -0700763 if (auth2_setup_methods_lists(authctxt) != 0) {
Adam Langleyd0592972015-03-30 14:49:51 -0700764 /*
765 * The monitor will continue long enough to let the child
766 * run to it's packet_disconnect(), but it must not allow any
767 * authentication to succeed.
768 */
769 debug("%s: no valid authentication method lists", __func__);
770 }
771
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800772 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
773 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
774
Greg Hartman9768ca42017-06-22 20:49:52 -0700775 /* Allow service/style information on the auth context */
776 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
777 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
778
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800779#ifdef USE_PAM
780 if (options.use_pam)
781 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
782#endif
783
784 return (0);
785}
786
787int mm_answer_auth2_read_banner(int sock, Buffer *m)
788{
789 char *banner;
790
791 buffer_clear(m);
792 banner = auth2_read_banner();
793 buffer_put_cstring(m, banner != NULL ? banner : "");
794 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Adam Langleyd0592972015-03-30 14:49:51 -0700795 free(banner);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800796
797 return (0);
798}
799
800int
801mm_answer_authserv(int sock, Buffer *m)
802{
803 monitor_permit_authentications(1);
804
805 authctxt->service = buffer_get_string(m, NULL);
806 authctxt->style = buffer_get_string(m, NULL);
807 debug3("%s: service=%s, style=%s",
808 __func__, authctxt->service, authctxt->style);
809
810 if (strlen(authctxt->style) == 0) {
Adam Langleyd0592972015-03-30 14:49:51 -0700811 free(authctxt->style);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800812 authctxt->style = NULL;
813 }
814
815 return (0);
816}
817
818int
819mm_answer_authpassword(int sock, Buffer *m)
820{
821 static int call_count;
822 char *passwd;
823 int authenticated;
824 u_int plen;
825
Greg Hartman9768ca42017-06-22 20:49:52 -0700826 if (!options.password_authentication)
827 fatal("%s: password authentication not enabled", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800828 passwd = buffer_get_string(m, &plen);
Adam Langleyd0592972015-03-30 14:49:51 -0700829#if !defined(ANDROID)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800830 /* Only authenticate if the context is valid */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800831 authenticated = options.password_authentication &&
832 auth_password(authctxt, passwd);
833#else
Adam Langleyd0592972015-03-30 14:49:51 -0700834 /* no password authentication in Android. */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800835 authenticated = 0;
836#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700837 explicit_bzero(passwd, strlen(passwd));
838 free(passwd);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800839
840 buffer_clear(m);
841 buffer_put_int(m, authenticated);
Greg Hartman9768ca42017-06-22 20:49:52 -0700842#ifdef USE_PAM
843 buffer_put_int(m, sshpam_get_maxtries_reached());
844#endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800845
846 debug3("%s: sending result %d", __func__, authenticated);
847 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
848
849 call_count++;
850 if (plen == 0 && call_count == 1)
851 auth_method = "none";
852 else
853 auth_method = "password";
854
855 /* Causes monitor loop to terminate if authenticated */
856 return (authenticated);
857}
858
859#ifdef BSD_AUTH
860int
861mm_answer_bsdauthquery(int sock, Buffer *m)
862{
863 char *name, *infotxt;
864 u_int numprompts;
865 u_int *echo_on;
866 char **prompts;
867 u_int success;
868
Greg Hartman9768ca42017-06-22 20:49:52 -0700869 if (!options.kbd_interactive_authentication)
870 fatal("%s: kbd-int authentication not enabled", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800871 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
872 &prompts, &echo_on) < 0 ? 0 : 1;
873
874 buffer_clear(m);
875 buffer_put_int(m, success);
876 if (success)
877 buffer_put_cstring(m, prompts[0]);
878
879 debug3("%s: sending challenge success: %u", __func__, success);
880 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
881
882 if (success) {
Adam Langleyd0592972015-03-30 14:49:51 -0700883 free(name);
884 free(infotxt);
885 free(prompts);
886 free(echo_on);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800887 }
888
889 return (0);
890}
891
892int
893mm_answer_bsdauthrespond(int sock, Buffer *m)
894{
895 char *response;
896 int authok;
897
Greg Hartman9768ca42017-06-22 20:49:52 -0700898 if (!options.kbd_interactive_authentication)
899 fatal("%s: kbd-int authentication not enabled", __func__);
900 if (authctxt->as == NULL)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800901 fatal("%s: no bsd auth session", __func__);
902
903 response = buffer_get_string(m, NULL);
904 authok = options.challenge_response_authentication &&
905 auth_userresponse(authctxt->as, response, 0);
906 authctxt->as = NULL;
907 debug3("%s: <%s> = <%d>", __func__, response, authok);
Adam Langleyd0592972015-03-30 14:49:51 -0700908 free(response);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800909
910 buffer_clear(m);
911 buffer_put_int(m, authok);
912
913 debug3("%s: sending authenticated: %d", __func__, authok);
914 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
915
Greg Hartman9768ca42017-06-22 20:49:52 -0700916 auth_method = "keyboard-interactive";
917 auth_submethod = "bsdauth";
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800918
919 return (authok != 0);
920}
921#endif
922
923#ifdef SKEY
924int
925mm_answer_skeyquery(int sock, Buffer *m)
926{
927 struct skey skey;
928 char challenge[1024];
929 u_int success;
930
931 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
932 sizeof(challenge)) < 0 ? 0 : 1;
933
934 buffer_clear(m);
935 buffer_put_int(m, success);
936 if (success)
937 buffer_put_cstring(m, challenge);
938
939 debug3("%s: sending challenge success: %u", __func__, success);
940 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
941
942 return (0);
943}
944
945int
946mm_answer_skeyrespond(int sock, Buffer *m)
947{
948 char *response;
949 int authok;
950
951 response = buffer_get_string(m, NULL);
952
953 authok = (options.challenge_response_authentication &&
954 authctxt->valid &&
955 skey_haskey(authctxt->pw->pw_name) == 0 &&
956 skey_passcheck(authctxt->pw->pw_name, response) != -1);
957
Adam Langleyd0592972015-03-30 14:49:51 -0700958 free(response);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800959
960 buffer_clear(m);
961 buffer_put_int(m, authok);
962
963 debug3("%s: sending authenticated: %d", __func__, authok);
964 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
965
Greg Hartman9768ca42017-06-22 20:49:52 -0700966 auth_method = "keyboard-interactive";
967 auth_submethod = "skey";
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800968
969 return (authok != 0);
970}
971#endif
972
973#ifdef USE_PAM
974int
975mm_answer_pam_start(int sock, Buffer *m)
976{
977 if (!options.use_pam)
978 fatal("UsePAM not set, but ended up in %s anyway", __func__);
979
980 start_pam(authctxt);
981
982 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Greg Hartman9768ca42017-06-22 20:49:52 -0700983 if (options.kbd_interactive_authentication)
984 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800985
986 return (0);
987}
988
989int
990mm_answer_pam_account(int sock, Buffer *m)
991{
992 u_int ret;
993
994 if (!options.use_pam)
Greg Hartman9768ca42017-06-22 20:49:52 -0700995 fatal("%s: PAM not enabled", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800996
997 ret = do_pam_account();
998
999 buffer_put_int(m, ret);
1000 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1001
1002 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1003
1004 return (ret);
1005}
1006
1007static void *sshpam_ctxt, *sshpam_authok;
1008extern KbdintDevice sshpam_device;
1009
1010int
1011mm_answer_pam_init_ctx(int sock, Buffer *m)
1012{
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001013 debug3("%s", __func__);
Greg Hartman9768ca42017-06-22 20:49:52 -07001014 if (!options.kbd_interactive_authentication)
1015 fatal("%s: kbd-int authentication not enabled", __func__);
1016 if (sshpam_ctxt != NULL)
1017 fatal("%s: already called", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001018 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1019 sshpam_authok = NULL;
1020 buffer_clear(m);
1021 if (sshpam_ctxt != NULL) {
1022 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Greg Hartman9768ca42017-06-22 20:49:52 -07001023 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001024 buffer_put_int(m, 1);
1025 } else {
1026 buffer_put_int(m, 0);
1027 }
1028 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1029 return (0);
1030}
1031
1032int
1033mm_answer_pam_query(int sock, Buffer *m)
1034{
1035 char *name = NULL, *info = NULL, **prompts = NULL;
1036 u_int i, num = 0, *echo_on = 0;
1037 int ret;
1038
1039 debug3("%s", __func__);
1040 sshpam_authok = NULL;
Greg Hartman9768ca42017-06-22 20:49:52 -07001041 if (sshpam_ctxt == NULL)
1042 fatal("%s: no context", __func__);
1043 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1044 &num, &prompts, &echo_on);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001045 if (ret == 0 && num == 0)
1046 sshpam_authok = sshpam_ctxt;
1047 if (num > 1 || name == NULL || info == NULL)
Greg Hartman9768ca42017-06-22 20:49:52 -07001048 fatal("sshpam_device.query failed");
1049 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001050 buffer_clear(m);
1051 buffer_put_int(m, ret);
1052 buffer_put_cstring(m, name);
Adam Langleyd0592972015-03-30 14:49:51 -07001053 free(name);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001054 buffer_put_cstring(m, info);
Adam Langleyd0592972015-03-30 14:49:51 -07001055 free(info);
Greg Hartman9768ca42017-06-22 20:49:52 -07001056 buffer_put_int(m, sshpam_get_maxtries_reached());
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001057 buffer_put_int(m, num);
1058 for (i = 0; i < num; ++i) {
1059 buffer_put_cstring(m, prompts[i]);
Adam Langleyd0592972015-03-30 14:49:51 -07001060 free(prompts[i]);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001061 buffer_put_int(m, echo_on[i]);
1062 }
Adam Langleyd0592972015-03-30 14:49:51 -07001063 free(prompts);
1064 free(echo_on);
1065 auth_method = "keyboard-interactive";
1066 auth_submethod = "pam";
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001067 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1068 return (0);
1069}
1070
1071int
1072mm_answer_pam_respond(int sock, Buffer *m)
1073{
1074 char **resp;
1075 u_int i, num;
1076 int ret;
1077
1078 debug3("%s", __func__);
Greg Hartman9768ca42017-06-22 20:49:52 -07001079 if (sshpam_ctxt == NULL)
1080 fatal("%s: no context", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001081 sshpam_authok = NULL;
1082 num = buffer_get_int(m);
1083 if (num > 0) {
1084 resp = xcalloc(num, sizeof(char *));
1085 for (i = 0; i < num; ++i)
1086 resp[i] = buffer_get_string(m, NULL);
1087 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1088 for (i = 0; i < num; ++i)
Adam Langleyd0592972015-03-30 14:49:51 -07001089 free(resp[i]);
1090 free(resp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001091 } else {
1092 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1093 }
1094 buffer_clear(m);
1095 buffer_put_int(m, ret);
1096 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Adam Langleyd0592972015-03-30 14:49:51 -07001097 auth_method = "keyboard-interactive";
1098 auth_submethod = "pam";
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001099 if (ret == 0)
1100 sshpam_authok = sshpam_ctxt;
1101 return (0);
1102}
1103
1104int
1105mm_answer_pam_free_ctx(int sock, Buffer *m)
1106{
Greg Hartmanccacbc92016-02-03 09:59:44 -08001107 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001108
1109 debug3("%s", __func__);
Greg Hartman9768ca42017-06-22 20:49:52 -07001110 if (sshpam_ctxt == NULL)
1111 fatal("%s: no context", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001112 (sshpam_device.free_ctx)(sshpam_ctxt);
Greg Hartmanccacbc92016-02-03 09:59:44 -08001113 sshpam_ctxt = sshpam_authok = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001114 buffer_clear(m);
1115 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Greg Hartman9768ca42017-06-22 20:49:52 -07001116 /* Allow another attempt */
1117 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Adam Langleyd0592972015-03-30 14:49:51 -07001118 auth_method = "keyboard-interactive";
1119 auth_submethod = "pam";
Greg Hartmanccacbc92016-02-03 09:59:44 -08001120 return r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001121}
1122#endif
1123
1124int
1125mm_answer_keyallowed(int sock, Buffer *m)
1126{
1127 Key *key;
1128 char *cuser, *chost;
1129 u_char *blob;
Greg Hartmanccacbc92016-02-03 09:59:44 -08001130 u_int bloblen, pubkey_auth_attempt;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001131 enum mm_keytype type = 0;
1132 int allowed = 0;
1133
1134 debug3("%s entering", __func__);
1135
1136 type = buffer_get_int(m);
1137 cuser = buffer_get_string(m, NULL);
1138 chost = buffer_get_string(m, NULL);
1139 blob = buffer_get_string(m, &bloblen);
Greg Hartmanccacbc92016-02-03 09:59:44 -08001140 pubkey_auth_attempt = buffer_get_int(m);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001141
1142 key = key_from_blob(blob, bloblen);
1143
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001144 debug3("%s: key_from_blob: %p", __func__, key);
1145
1146 if (key != NULL && authctxt->valid) {
Adam Langleyd0592972015-03-30 14:49:51 -07001147 /* These should not make it past the privsep child */
1148 if (key_type_plain(key->type) == KEY_RSA &&
1149 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1150 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1151
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001152 switch (type) {
1153 case MM_USERKEY:
1154 allowed = options.pubkey_authentication &&
Adam Langleyd0592972015-03-30 14:49:51 -07001155 !auth2_userkey_already_used(authctxt, key) &&
1156 match_pattern_list(sshkey_ssh_name(key),
Greg Hartmanccacbc92016-02-03 09:59:44 -08001157 options.pubkey_key_types, 0) == 1 &&
1158 user_key_allowed(authctxt->pw, key,
1159 pubkey_auth_attempt);
Adam Langleyd0592972015-03-30 14:49:51 -07001160 pubkey_auth_info(authctxt, key, NULL);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001161 auth_method = "publickey";
Greg Hartmanccacbc92016-02-03 09:59:44 -08001162 if (options.pubkey_authentication &&
1163 (!pubkey_auth_attempt || allowed != 1))
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001164 auth_clear_options();
1165 break;
1166 case MM_HOSTKEY:
1167 allowed = options.hostbased_authentication &&
Adam Langleyd0592972015-03-30 14:49:51 -07001168 match_pattern_list(sshkey_ssh_name(key),
Greg Hartmanccacbc92016-02-03 09:59:44 -08001169 options.hostbased_key_types, 0) == 1 &&
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001170 hostbased_key_allowed(authctxt->pw,
1171 cuser, chost, key);
Adam Langleyd0592972015-03-30 14:49:51 -07001172 pubkey_auth_info(authctxt, key,
1173 "client user \"%.100s\", client host \"%.100s\"",
1174 cuser, chost);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001175 auth_method = "hostbased";
1176 break;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001177 default:
1178 fatal("%s: unknown key type %d", __func__, type);
1179 break;
1180 }
1181 }
Greg Hartman9768ca42017-06-22 20:49:52 -07001182
1183 debug3("%s: key %p is %s",
1184 __func__, key, allowed ? "allowed" : "not allowed");
1185
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001186 if (key != NULL)
1187 key_free(key);
1188
1189 /* clear temporarily storage (used by verify) */
1190 monitor_reset_key_state();
1191
1192 if (allowed) {
1193 /* Save temporarily for comparison in verify */
1194 key_blob = blob;
1195 key_bloblen = bloblen;
1196 key_blobtype = type;
1197 hostbased_cuser = cuser;
1198 hostbased_chost = chost;
1199 } else {
1200 /* Log failed attempt */
Adam Langleyd0592972015-03-30 14:49:51 -07001201 auth_log(authctxt, 0, 0, auth_method, NULL);
1202 free(blob);
1203 free(cuser);
1204 free(chost);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001205 }
1206
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001207 buffer_clear(m);
1208 buffer_put_int(m, allowed);
1209 buffer_put_int(m, forced_command != NULL);
1210
1211 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1212
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001213 return (0);
1214}
1215
1216static int
1217monitor_valid_userblob(u_char *data, u_int datalen)
1218{
1219 Buffer b;
Greg Hartman9768ca42017-06-22 20:49:52 -07001220 u_char *p;
1221 char *userstyle, *cp;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001222 u_int len;
1223 int fail = 0;
1224
1225 buffer_init(&b);
1226 buffer_append(&b, data, datalen);
1227
1228 if (datafellows & SSH_OLD_SESSIONID) {
1229 p = buffer_ptr(&b);
1230 len = buffer_len(&b);
1231 if ((session_id2 == NULL) ||
1232 (len < session_id2_len) ||
1233 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1234 fail++;
1235 buffer_consume(&b, session_id2_len);
1236 } else {
1237 p = buffer_get_string(&b, &len);
1238 if ((session_id2 == NULL) ||
1239 (len != session_id2_len) ||
1240 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1241 fail++;
Adam Langleyd0592972015-03-30 14:49:51 -07001242 free(p);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001243 }
1244 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1245 fail++;
Greg Hartman9768ca42017-06-22 20:49:52 -07001246 cp = buffer_get_cstring(&b, NULL);
Adam Langleyd0592972015-03-30 14:49:51 -07001247 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1248 authctxt->style ? ":" : "",
1249 authctxt->style ? authctxt->style : "");
Greg Hartman9768ca42017-06-22 20:49:52 -07001250 if (strcmp(userstyle, cp) != 0) {
1251 logit("wrong user name passed to monitor: "
1252 "expected %s != %.100s", userstyle, cp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001253 fail++;
1254 }
Adam Langleyd0592972015-03-30 14:49:51 -07001255 free(userstyle);
Greg Hartman9768ca42017-06-22 20:49:52 -07001256 free(cp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001257 buffer_skip_string(&b);
1258 if (datafellows & SSH_BUG_PKAUTH) {
1259 if (!buffer_get_char(&b))
1260 fail++;
1261 } else {
Greg Hartman9768ca42017-06-22 20:49:52 -07001262 cp = buffer_get_cstring(&b, NULL);
1263 if (strcmp("publickey", cp) != 0)
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001264 fail++;
Greg Hartman9768ca42017-06-22 20:49:52 -07001265 free(cp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001266 if (!buffer_get_char(&b))
1267 fail++;
1268 buffer_skip_string(&b);
1269 }
1270 buffer_skip_string(&b);
1271 if (buffer_len(&b) != 0)
1272 fail++;
1273 buffer_free(&b);
1274 return (fail == 0);
1275}
1276
1277static int
1278monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1279 char *chost)
1280{
1281 Buffer b;
Adam Langleyd0592972015-03-30 14:49:51 -07001282 char *p, *userstyle;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001283 u_int len;
1284 int fail = 0;
1285
1286 buffer_init(&b);
1287 buffer_append(&b, data, datalen);
1288
1289 p = buffer_get_string(&b, &len);
1290 if ((session_id2 == NULL) ||
1291 (len != session_id2_len) ||
1292 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1293 fail++;
Adam Langleyd0592972015-03-30 14:49:51 -07001294 free(p);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001295
1296 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1297 fail++;
Adam Langleyd0592972015-03-30 14:49:51 -07001298 p = buffer_get_cstring(&b, NULL);
1299 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1300 authctxt->style ? ":" : "",
1301 authctxt->style ? authctxt->style : "");
1302 if (strcmp(userstyle, p) != 0) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001303 logit("wrong user name passed to monitor: expected %s != %.100s",
Adam Langleyd0592972015-03-30 14:49:51 -07001304 userstyle, p);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001305 fail++;
1306 }
Adam Langleyd0592972015-03-30 14:49:51 -07001307 free(userstyle);
1308 free(p);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001309 buffer_skip_string(&b); /* service */
Adam Langleyd0592972015-03-30 14:49:51 -07001310 p = buffer_get_cstring(&b, NULL);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001311 if (strcmp(p, "hostbased") != 0)
1312 fail++;
Adam Langleyd0592972015-03-30 14:49:51 -07001313 free(p);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001314 buffer_skip_string(&b); /* pkalg */
1315 buffer_skip_string(&b); /* pkblob */
1316
1317 /* verify client host, strip trailing dot if necessary */
1318 p = buffer_get_string(&b, NULL);
1319 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1320 p[len - 1] = '\0';
1321 if (strcmp(p, chost) != 0)
1322 fail++;
Adam Langleyd0592972015-03-30 14:49:51 -07001323 free(p);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001324
1325 /* verify client user */
1326 p = buffer_get_string(&b, NULL);
1327 if (strcmp(p, cuser) != 0)
1328 fail++;
Adam Langleyd0592972015-03-30 14:49:51 -07001329 free(p);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001330
1331 if (buffer_len(&b) != 0)
1332 fail++;
1333 buffer_free(&b);
1334 return (fail == 0);
1335}
1336
1337int
1338mm_answer_keyverify(int sock, Buffer *m)
1339{
1340 Key *key;
1341 u_char *signature, *data, *blob;
1342 u_int signaturelen, datalen, bloblen;
1343 int verified = 0;
1344 int valid_data = 0;
1345
1346 blob = buffer_get_string(m, &bloblen);
1347 signature = buffer_get_string(m, &signaturelen);
1348 data = buffer_get_string(m, &datalen);
1349
1350 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1351 !monitor_allowed_key(blob, bloblen))
1352 fatal("%s: bad key, not previously allowed", __func__);
1353
1354 key = key_from_blob(blob, bloblen);
1355 if (key == NULL)
1356 fatal("%s: bad public key blob", __func__);
1357
1358 switch (key_blobtype) {
1359 case MM_USERKEY:
1360 valid_data = monitor_valid_userblob(data, datalen);
1361 break;
1362 case MM_HOSTKEY:
1363 valid_data = monitor_valid_hostbasedblob(data, datalen,
1364 hostbased_cuser, hostbased_chost);
1365 break;
1366 default:
1367 valid_data = 0;
1368 break;
1369 }
1370 if (!valid_data)
1371 fatal("%s: bad signature data blob", __func__);
1372
1373 verified = key_verify(key, signature, signaturelen, data, datalen);
1374 debug3("%s: key %p signature %s",
1375 __func__, key, (verified == 1) ? "verified" : "unverified");
1376
Adam Langleyd0592972015-03-30 14:49:51 -07001377 /* If auth was successful then record key to ensure it isn't reused */
Greg Hartman9768ca42017-06-22 20:49:52 -07001378 if (verified == 1 && key_blobtype == MM_USERKEY)
Adam Langleyd0592972015-03-30 14:49:51 -07001379 auth2_record_userkey(authctxt, key);
1380 else
1381 key_free(key);
1382
1383 free(blob);
1384 free(signature);
1385 free(data);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001386
1387 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1388
1389 monitor_reset_key_state();
1390
1391 buffer_clear(m);
1392 buffer_put_int(m, verified);
1393 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1394
1395 return (verified == 1);
1396}
1397
1398static void
1399mm_record_login(Session *s, struct passwd *pw)
1400{
Greg Hartman9768ca42017-06-22 20:49:52 -07001401 struct ssh *ssh = active_state; /* XXX */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001402 socklen_t fromlen;
1403 struct sockaddr_storage from;
1404
1405 /*
1406 * Get IP address of client. If the connection is not a socket, let
1407 * the address be 0.0.0.0.
1408 */
1409 memset(&from, 0, sizeof(from));
1410 fromlen = sizeof(from);
1411 if (packet_connection_is_on_socket()) {
1412 if (getpeername(packet_get_connection_in(),
1413 (struct sockaddr *)&from, &fromlen) < 0) {
1414 debug("getpeername: %.100s", strerror(errno));
1415 cleanup_exit(255);
1416 }
1417 }
1418 /* Record that there was a login on that tty from the remote host. */
1419 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Greg Hartman9768ca42017-06-22 20:49:52 -07001420 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001421 (struct sockaddr *)&from, fromlen);
1422}
1423
1424static void
1425mm_session_close(Session *s)
1426{
1427 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1428 if (s->ttyfd != -1) {
1429 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1430 session_pty_cleanup2(s);
1431 }
1432 session_unused(s->self);
1433}
1434
1435int
1436mm_answer_pty(int sock, Buffer *m)
1437{
1438 extern struct monitor *pmonitor;
1439 Session *s;
1440 int res, fd0;
1441
1442 debug3("%s entering", __func__);
1443
1444 buffer_clear(m);
1445 s = session_new();
1446 if (s == NULL)
1447 goto error;
1448 s->authctxt = authctxt;
1449 s->pw = authctxt->pw;
1450 s->pid = pmonitor->m_pid;
1451 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1452 if (res == 0)
1453 goto error;
1454 pty_setowner(authctxt->pw, s->tty);
1455
1456 buffer_put_int(m, 1);
1457 buffer_put_cstring(m, s->tty);
1458
1459 /* We need to trick ttyslot */
1460 if (dup2(s->ttyfd, 0) == -1)
1461 fatal("%s: dup2", __func__);
1462
1463 mm_record_login(s, authctxt->pw);
1464
1465 /* Now we can close the file descriptor again */
1466 close(0);
1467
1468 /* send messages generated by record_login */
1469 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1470 buffer_clear(&loginmsg);
1471
1472 mm_request_send(sock, MONITOR_ANS_PTY, m);
1473
1474 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1475 mm_send_fd(sock, s->ttyfd) == -1)
1476 fatal("%s: send fds failed", __func__);
1477
1478 /* make sure nothing uses fd 0 */
1479 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1480 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1481 if (fd0 != 0)
1482 error("%s: fd0 %d != 0", __func__, fd0);
1483
1484 /* slave is not needed */
1485 close(s->ttyfd);
1486 s->ttyfd = s->ptyfd;
1487 /* no need to dup() because nobody closes ptyfd */
1488 s->ptymaster = s->ptyfd;
1489
1490 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1491
1492 return (0);
1493
1494 error:
1495 if (s != NULL)
1496 mm_session_close(s);
1497 buffer_put_int(m, 0);
1498 mm_request_send(sock, MONITOR_ANS_PTY, m);
1499 return (0);
1500}
1501
1502int
1503mm_answer_pty_cleanup(int sock, Buffer *m)
1504{
1505 Session *s;
1506 char *tty;
1507
1508 debug3("%s entering", __func__);
1509
1510 tty = buffer_get_string(m, NULL);
1511 if ((s = session_by_tty(tty)) != NULL)
1512 mm_session_close(s);
1513 buffer_clear(m);
Adam Langleyd0592972015-03-30 14:49:51 -07001514 free(tty);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001515 return (0);
1516}
1517
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001518int
1519mm_answer_term(int sock, Buffer *req)
1520{
1521 extern struct monitor *pmonitor;
1522 int res, status;
1523
1524 debug3("%s: tearing down sessions", __func__);
1525
1526 /* The child is terminating */
1527 session_destroy_all(&mm_session_close);
1528
1529#ifdef USE_PAM
1530 if (options.use_pam)
1531 sshpam_cleanup();
1532#endif
1533
1534 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1535 if (errno != EINTR)
1536 exit(1);
1537
1538 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1539
1540 /* Terminate process */
1541 exit(res);
1542}
1543
1544#ifdef SSH_AUDIT_EVENTS
1545/* Report that an audit event occurred */
1546int
1547mm_answer_audit_event(int socket, Buffer *m)
1548{
1549 ssh_audit_event_t event;
1550
1551 debug3("%s entering", __func__);
1552
1553 event = buffer_get_int(m);
1554 switch(event) {
1555 case SSH_AUTH_FAIL_PUBKEY:
1556 case SSH_AUTH_FAIL_HOSTBASED:
1557 case SSH_AUTH_FAIL_GSSAPI:
1558 case SSH_LOGIN_EXCEED_MAXTRIES:
1559 case SSH_LOGIN_ROOT_DENIED:
1560 case SSH_CONNECTION_CLOSE:
1561 case SSH_INVALID_USER:
1562 audit_event(event);
1563 break;
1564 default:
1565 fatal("Audit event type %d not permitted", event);
1566 }
1567
1568 return (0);
1569}
1570
1571int
1572mm_answer_audit_command(int socket, Buffer *m)
1573{
1574 u_int len;
1575 char *cmd;
1576
1577 debug3("%s entering", __func__);
1578 cmd = buffer_get_string(m, &len);
1579 /* sanity check command, if so how? */
1580 audit_run_command(cmd);
Adam Langleyd0592972015-03-30 14:49:51 -07001581 free(cmd);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001582 return (0);
1583}
1584#endif /* SSH_AUDIT_EVENTS */
1585
1586void
1587monitor_apply_keystate(struct monitor *pmonitor)
1588{
Adam Langleyd0592972015-03-30 14:49:51 -07001589 struct ssh *ssh = active_state; /* XXX */
1590 struct kex *kex;
1591 int r;
1592
1593 debug3("%s: packet_set_state", __func__);
1594 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1595 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1596 sshbuf_free(child_state);
1597 child_state = NULL;
1598
Greg Hartman9768ca42017-06-22 20:49:52 -07001599 if ((kex = ssh->kex) != NULL) {
Adam Langleyd0592972015-03-30 14:49:51 -07001600 /* XXX set callbacks */
1601#ifdef WITH_OPENSSL
1602 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1603 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Greg Hartman9768ca42017-06-22 20:49:52 -07001604 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1605 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1606 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
Adam Langleyd0592972015-03-30 14:49:51 -07001607 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1608 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1609# ifdef OPENSSL_HAS_ECC
1610 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1611# endif
1612#endif /* WITH_OPENSSL */
1613 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1614 kex->load_host_public_key=&get_hostkey_public_by_type;
1615 kex->load_host_private_key=&get_hostkey_private_by_type;
1616 kex->host_key_index=&get_hostkey_index;
1617 kex->sign = sshd_hostkey_sign;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001618 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001619}
1620
1621/* This function requries careful sanity checking */
1622
1623void
1624mm_get_keystate(struct monitor *pmonitor)
1625{
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001626 debug3("%s: Waiting for new keys", __func__);
1627
Adam Langleyd0592972015-03-30 14:49:51 -07001628 if ((child_state = sshbuf_new()) == NULL)
1629 fatal("%s: sshbuf_new failed", __func__);
1630 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1631 child_state);
1632 debug3("%s: GOT new keys", __func__);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001633}
1634
1635
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001636/* XXX */
1637
1638#define FD_CLOSEONEXEC(x) do { \
1639 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1640 fatal("fcntl(%d, F_SETFD)", x); \
1641} while (0)
1642
1643static void
1644monitor_openfds(struct monitor *mon, int do_logfds)
1645{
1646 int pair[2];
1647
1648 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1649 fatal("%s: socketpair: %s", __func__, strerror(errno));
1650 FD_CLOSEONEXEC(pair[0]);
1651 FD_CLOSEONEXEC(pair[1]);
1652 mon->m_recvfd = pair[0];
1653 mon->m_sendfd = pair[1];
1654
1655 if (do_logfds) {
1656 if (pipe(pair) == -1)
1657 fatal("%s: pipe: %s", __func__, strerror(errno));
1658 FD_CLOSEONEXEC(pair[0]);
1659 FD_CLOSEONEXEC(pair[1]);
1660 mon->m_log_recvfd = pair[0];
1661 mon->m_log_sendfd = pair[1];
1662 } else
1663 mon->m_log_recvfd = mon->m_log_sendfd = -1;
1664}
1665
1666#define MM_MEMSIZE 65536
1667
1668struct monitor *
1669monitor_init(void)
1670{
1671 struct monitor *mon;
1672
1673 mon = xcalloc(1, sizeof(*mon));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001674 monitor_openfds(mon, 1);
1675
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001676 return mon;
1677}
1678
1679void
1680monitor_reinit(struct monitor *mon)
1681{
1682 monitor_openfds(mon, 0);
1683}
1684
1685#ifdef GSSAPI
1686int
1687mm_answer_gss_setup_ctx(int sock, Buffer *m)
1688{
1689 gss_OID_desc goid;
1690 OM_uint32 major;
1691 u_int len;
1692
Greg Hartman9768ca42017-06-22 20:49:52 -07001693 if (!options.gss_authentication)
1694 fatal("%s: GSSAPI authentication not enabled", __func__);
1695
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001696 goid.elements = buffer_get_string(m, &len);
1697 goid.length = len;
1698
1699 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1700
Adam Langleyd0592972015-03-30 14:49:51 -07001701 free(goid.elements);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001702
1703 buffer_clear(m);
1704 buffer_put_int(m, major);
1705
1706 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1707
1708 /* Now we have a context, enable the step */
1709 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1710
1711 return (0);
1712}
1713
1714int
1715mm_answer_gss_accept_ctx(int sock, Buffer *m)
1716{
1717 gss_buffer_desc in;
1718 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1719 OM_uint32 major, minor;
1720 OM_uint32 flags = 0; /* GSI needs this */
1721 u_int len;
1722
Greg Hartman9768ca42017-06-22 20:49:52 -07001723 if (!options.gss_authentication)
1724 fatal("%s: GSSAPI authentication not enabled", __func__);
1725
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001726 in.value = buffer_get_string(m, &len);
1727 in.length = len;
1728 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Adam Langleyd0592972015-03-30 14:49:51 -07001729 free(in.value);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001730
1731 buffer_clear(m);
1732 buffer_put_int(m, major);
1733 buffer_put_string(m, out.value, out.length);
1734 buffer_put_int(m, flags);
1735 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1736
1737 gss_release_buffer(&minor, &out);
1738
1739 if (major == GSS_S_COMPLETE) {
1740 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1741 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1742 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1743 }
1744 return (0);
1745}
1746
1747int
1748mm_answer_gss_checkmic(int sock, Buffer *m)
1749{
1750 gss_buffer_desc gssbuf, mic;
1751 OM_uint32 ret;
1752 u_int len;
1753
Greg Hartman9768ca42017-06-22 20:49:52 -07001754 if (!options.gss_authentication)
1755 fatal("%s: GSSAPI authentication not enabled", __func__);
1756
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001757 gssbuf.value = buffer_get_string(m, &len);
1758 gssbuf.length = len;
1759 mic.value = buffer_get_string(m, &len);
1760 mic.length = len;
1761
1762 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1763
Adam Langleyd0592972015-03-30 14:49:51 -07001764 free(gssbuf.value);
1765 free(mic.value);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001766
1767 buffer_clear(m);
1768 buffer_put_int(m, ret);
1769
1770 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1771
1772 if (!GSS_ERROR(ret))
1773 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1774
1775 return (0);
1776}
1777
1778int
1779mm_answer_gss_userok(int sock, Buffer *m)
1780{
1781 int authenticated;
1782
Greg Hartman9768ca42017-06-22 20:49:52 -07001783 if (!options.gss_authentication)
1784 fatal("%s: GSSAPI authentication not enabled", __func__);
1785
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001786 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1787
1788 buffer_clear(m);
1789 buffer_put_int(m, authenticated);
1790
1791 debug3("%s: sending result %d", __func__, authenticated);
1792 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1793
1794 auth_method = "gssapi-with-mic";
1795
1796 /* Monitor loop will terminate if authenticated */
1797 return (authenticated);
1798}
1799#endif /* GSSAPI */