djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 1 | /* $OpenBSD: monitor.c,v 1.145 2015/02/20 22:17:21 djm Exp $ */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2002 Niels Provos <provos@citi.umich.edu> |
| 4 | * Copyright 2002 Markus Friedl <markus@openbsd.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "includes.h" |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 29 | |
| 30 | #include <sys/types.h> |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 31 | #include <sys/socket.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 32 | #include "openbsd-compat/sys-tree.h" |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 33 | #include <sys/wait.h> |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 34 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 35 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 36 | #include <fcntl.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 37 | #ifdef HAVE_PATHS_H |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 38 | #include <paths.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 39 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 40 | #include <pwd.h> |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 41 | #include <signal.h> |
Damien Miller | 9af2197 | 2015-02-24 09:04:32 +1100 | [diff] [blame] | 42 | #ifdef HAVE_STDINT_H |
millert@openbsd.org | fd36834 | 2015-02-06 23:21:59 +0000 | [diff] [blame] | 43 | #include <stdint.h> |
Damien Miller | 9af2197 | 2015-02-24 09:04:32 +1100 | [diff] [blame] | 44 | #endif |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 45 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 46 | #include <string.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 47 | #include <stdarg.h> |
| 48 | #include <stdio.h> |
Damien Miller | 607aede | 2006-09-01 15:48:19 +1000 | [diff] [blame] | 49 | #include <unistd.h> |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 50 | #ifdef HAVE_POLL_H |
| 51 | #include <poll.h> |
| 52 | #else |
| 53 | # ifdef HAVE_SYS_POLL_H |
| 54 | # include <sys/poll.h> |
| 55 | # endif |
| 56 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 57 | |
| 58 | #ifdef SKEY |
| 59 | #include <skey.h> |
| 60 | #endif |
| 61 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 62 | #ifdef WITH_OPENSSL |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 63 | #include <openssl/dh.h> |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 64 | #endif |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 65 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 66 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 67 | #include "atomicio.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 68 | #include "xmalloc.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 69 | #include "ssh.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 70 | #include "key.h" |
| 71 | #include "buffer.h" |
| 72 | #include "hostfile.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 73 | #include "auth.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 74 | #include "cipher.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 75 | #include "kex.h" |
| 76 | #include "dh.h" |
Ben Lindstrom | 036768e | 2004-04-08 16:12:30 +0000 | [diff] [blame] | 77 | #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */ |
| 78 | #undef TARGET_OS_MAC |
Ben Lindstrom | 1b9f2a6 | 2004-04-08 05:11:03 +0000 | [diff] [blame] | 79 | #include "zlib.h" |
Ben Lindstrom | 036768e | 2004-04-08 16:12:30 +0000 | [diff] [blame] | 80 | #define TARGET_OS_MAC 1 |
| 81 | #else |
| 82 | #include "zlib.h" |
| 83 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 84 | #include "packet.h" |
| 85 | #include "auth-options.h" |
| 86 | #include "sshpty.h" |
| 87 | #include "channels.h" |
| 88 | #include "session.h" |
| 89 | #include "sshlogin.h" |
| 90 | #include "canohost.h" |
| 91 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 92 | #include "misc.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 93 | #include "servconf.h" |
| 94 | #include "monitor.h" |
| 95 | #include "monitor_mm.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 96 | #ifdef GSSAPI |
| 97 | #include "ssh-gss.h" |
| 98 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 99 | #include "monitor_wrap.h" |
| 100 | #include "monitor_fdpass.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 101 | #include "compat.h" |
| 102 | #include "ssh2.h" |
Darren Tucker | b422afa | 2009-06-21 18:58:46 +1000 | [diff] [blame] | 103 | #include "roaming.h" |
Damien Miller | 85b45e0 | 2013-07-20 13:21:52 +1000 | [diff] [blame] | 104 | #include "authfd.h" |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 105 | #include "match.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 106 | #include "ssherr.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 107 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 108 | #ifdef GSSAPI |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 109 | static Gssctxt *gsscontext = NULL; |
| 110 | #endif |
| 111 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 112 | /* Imports */ |
| 113 | extern ServerOptions options; |
| 114 | extern u_int utmp_len; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 115 | extern u_char session_id[]; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 116 | extern Buffer auth_debug; |
| 117 | extern int auth_debug_init; |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 118 | extern Buffer loginmsg; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 119 | |
| 120 | /* State exported from the child */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 121 | static struct sshbuf *child_state; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 122 | |
Damien Miller | 469954d | 2003-06-18 20:25:33 +1000 | [diff] [blame] | 123 | /* Functions on the monitor that answer unprivileged requests */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 124 | |
| 125 | int mm_answer_moduli(int, Buffer *); |
| 126 | int mm_answer_sign(int, Buffer *); |
| 127 | int mm_answer_pwnamallow(int, Buffer *); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 128 | int mm_answer_auth2_read_banner(int, Buffer *); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 129 | int mm_answer_authserv(int, Buffer *); |
| 130 | int mm_answer_authpassword(int, Buffer *); |
| 131 | int mm_answer_bsdauthquery(int, Buffer *); |
| 132 | int mm_answer_bsdauthrespond(int, Buffer *); |
| 133 | int mm_answer_skeyquery(int, Buffer *); |
| 134 | int mm_answer_skeyrespond(int, Buffer *); |
| 135 | int mm_answer_keyallowed(int, Buffer *); |
| 136 | int mm_answer_keyverify(int, Buffer *); |
| 137 | int mm_answer_pty(int, Buffer *); |
| 138 | int mm_answer_pty_cleanup(int, Buffer *); |
| 139 | int mm_answer_term(int, Buffer *); |
| 140 | int mm_answer_rsa_keyallowed(int, Buffer *); |
| 141 | int mm_answer_rsa_challenge(int, Buffer *); |
| 142 | int mm_answer_rsa_response(int, Buffer *); |
| 143 | int mm_answer_sesskey(int, Buffer *); |
| 144 | int mm_answer_sessid(int, Buffer *); |
| 145 | |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 146 | #ifdef USE_PAM |
| 147 | int mm_answer_pam_start(int, Buffer *); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 148 | int mm_answer_pam_account(int, Buffer *); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 149 | int mm_answer_pam_init_ctx(int, Buffer *); |
| 150 | int mm_answer_pam_query(int, Buffer *); |
| 151 | int mm_answer_pam_respond(int, Buffer *); |
| 152 | int mm_answer_pam_free_ctx(int, Buffer *); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 153 | #endif |
| 154 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 155 | #ifdef GSSAPI |
| 156 | int mm_answer_gss_setup_ctx(int, Buffer *); |
| 157 | int mm_answer_gss_accept_ctx(int, Buffer *); |
| 158 | int mm_answer_gss_userok(int, Buffer *); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 159 | int mm_answer_gss_checkmic(int, Buffer *); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 160 | #endif |
Damien Miller | 25162f2 | 2002-09-12 09:47:29 +1000 | [diff] [blame] | 161 | |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 162 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 163 | int mm_answer_audit_event(int, Buffer *); |
| 164 | int mm_answer_audit_command(int, Buffer *); |
| 165 | #endif |
| 166 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 167 | static int monitor_read_log(struct monitor *); |
| 168 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 169 | static Authctxt *authctxt; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 170 | |
| 171 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 172 | static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */ |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 173 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 174 | |
| 175 | /* local state for key verify */ |
| 176 | static u_char *key_blob = NULL; |
| 177 | static u_int key_bloblen = 0; |
| 178 | static int key_blobtype = MM_NOKEY; |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 179 | static char *hostbased_cuser = NULL; |
| 180 | static char *hostbased_chost = NULL; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 181 | static char *auth_method = "unknown"; |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 182 | static char *auth_submethod = NULL; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 183 | static u_int session_id2_len = 0; |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 184 | static u_char *session_id2 = NULL; |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 185 | static pid_t monitor_child_pid; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 186 | |
| 187 | struct mon_table { |
| 188 | enum monitor_reqtype type; |
| 189 | int flags; |
| 190 | int (*f)(int, Buffer *); |
| 191 | }; |
| 192 | |
| 193 | #define MON_ISAUTH 0x0004 /* Required for Authentication */ |
| 194 | #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */ |
| 195 | #define MON_ONCE 0x0010 /* Disable after calling */ |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 196 | #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 197 | |
| 198 | #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE) |
| 199 | |
| 200 | #define MON_PERMIT 0x1000 /* Request is permitted */ |
| 201 | |
| 202 | struct mon_table mon_dispatch_proto20[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 203 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 204 | {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli}, |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 205 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 206 | {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign}, |
| 207 | {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, |
| 208 | {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv}, |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 209 | {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner}, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 210 | {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 211 | #ifdef USE_PAM |
| 212 | {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 213 | {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 214 | {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, |
| 215 | {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, |
| 216 | {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, |
| 217 | {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, |
Kevin Steves | bd1901b | 2002-04-01 18:04:35 +0000 | [diff] [blame] | 218 | #endif |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 219 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 3745e2b | 2005-03-06 22:31:35 +1100 | [diff] [blame] | 220 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 221 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 222 | #ifdef BSD_AUTH |
| 223 | {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, |
Damien Miller | 0b70b54 | 2006-03-15 11:20:03 +1100 | [diff] [blame] | 224 | {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 225 | #endif |
| 226 | #ifdef SKEY |
| 227 | {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, |
| 228 | {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, |
| 229 | #endif |
| 230 | {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, |
| 231 | {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify}, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 232 | #ifdef GSSAPI |
| 233 | {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx}, |
| 234 | {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx}, |
| 235 | {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok}, |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 236 | {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic}, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 237 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 238 | {0, 0, NULL} |
| 239 | }; |
| 240 | |
| 241 | struct mon_table mon_dispatch_postauth20[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 242 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 243 | {MONITOR_REQ_MODULI, 0, mm_answer_moduli}, |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 244 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 245 | {MONITOR_REQ_SIGN, 0, mm_answer_sign}, |
| 246 | {MONITOR_REQ_PTY, 0, mm_answer_pty}, |
| 247 | {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup}, |
| 248 | {MONITOR_REQ_TERM, 0, mm_answer_term}, |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 249 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 250 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, |
| 251 | {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command}, |
| 252 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 253 | {0, 0, NULL} |
| 254 | }; |
| 255 | |
| 256 | struct mon_table mon_dispatch_proto15[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 257 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 258 | {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, |
| 259 | {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey}, |
| 260 | {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid}, |
| 261 | {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 262 | {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed}, |
| 263 | {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed}, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 264 | {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge}, |
| 265 | {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response}, |
| 266 | #ifdef BSD_AUTH |
| 267 | {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, |
Damien Miller | 0b70b54 | 2006-03-15 11:20:03 +1100 | [diff] [blame] | 268 | {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 269 | #endif |
| 270 | #ifdef SKEY |
| 271 | {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, |
| 272 | {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, |
| 273 | #endif |
Damien Miller | a33501b | 2002-05-08 12:24:42 +1000 | [diff] [blame] | 274 | #ifdef USE_PAM |
| 275 | {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 276 | {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 277 | {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, |
| 278 | {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, |
| 279 | {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, |
| 280 | {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, |
Damien Miller | a33501b | 2002-05-08 12:24:42 +1000 | [diff] [blame] | 281 | #endif |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 282 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 3745e2b | 2005-03-06 22:31:35 +1100 | [diff] [blame] | 283 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 284 | #endif |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 285 | #endif /* WITH_SSH1 */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 286 | {0, 0, NULL} |
| 287 | }; |
| 288 | |
| 289 | struct mon_table mon_dispatch_postauth15[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 290 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 291 | {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty}, |
| 292 | {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup}, |
| 293 | {MONITOR_REQ_TERM, 0, mm_answer_term}, |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 294 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 295 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, |
Darren Tucker | 5965ae1 | 2006-09-17 12:00:13 +1000 | [diff] [blame] | 296 | {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command}, |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 297 | #endif |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 298 | #endif /* WITH_SSH1 */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 299 | {0, 0, NULL} |
| 300 | }; |
| 301 | |
| 302 | struct mon_table *mon_dispatch; |
| 303 | |
| 304 | /* Specifies if a certain message is allowed at the moment */ |
| 305 | |
| 306 | static void |
| 307 | monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit) |
| 308 | { |
| 309 | while (ent->f != NULL) { |
| 310 | if (ent->type == type) { |
| 311 | ent->flags &= ~MON_PERMIT; |
| 312 | ent->flags |= permit ? MON_PERMIT : 0; |
| 313 | return; |
| 314 | } |
| 315 | ent++; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static void |
| 320 | monitor_permit_authentications(int permit) |
| 321 | { |
| 322 | struct mon_table *ent = mon_dispatch; |
| 323 | |
| 324 | while (ent->f != NULL) { |
| 325 | if (ent->flags & MON_AUTH) { |
| 326 | ent->flags &= ~MON_PERMIT; |
| 327 | ent->flags |= permit ? MON_PERMIT : 0; |
| 328 | } |
| 329 | ent++; |
| 330 | } |
| 331 | } |
| 332 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 333 | void |
| 334 | monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 335 | { |
| 336 | struct mon_table *ent; |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 337 | int authenticated = 0, partial = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 338 | |
| 339 | debug3("preauth child monitor started"); |
| 340 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 341 | close(pmonitor->m_recvfd); |
| 342 | close(pmonitor->m_log_sendfd); |
| 343 | pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1; |
| 344 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 345 | authctxt = _authctxt; |
| 346 | memset(authctxt, 0, sizeof(*authctxt)); |
| 347 | |
Darren Tucker | de0de39 | 2005-03-31 23:52:04 +1000 | [diff] [blame] | 348 | authctxt->loginmsg = &loginmsg; |
| 349 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 350 | if (compat20) { |
| 351 | mon_dispatch = mon_dispatch_proto20; |
| 352 | |
| 353 | /* Permit requests for moduli and signatures */ |
| 354 | monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); |
| 355 | monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); |
| 356 | } else { |
| 357 | mon_dispatch = mon_dispatch_proto15; |
| 358 | |
| 359 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1); |
| 360 | } |
| 361 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 362 | /* The first few requests do not require asynchronous access */ |
| 363 | while (!authenticated) { |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 364 | partial = 0; |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 365 | auth_method = "unknown"; |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 366 | auth_submethod = NULL; |
Darren Tucker | fbba735 | 2006-11-07 23:16:08 +1100 | [diff] [blame] | 367 | authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1); |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 368 | |
| 369 | /* Special handling for multiple required authentications */ |
| 370 | if (options.num_auth_methods != 0) { |
| 371 | if (!compat20) |
| 372 | fatal("AuthenticationMethods is not supported" |
| 373 | "with SSH protocol 1"); |
| 374 | if (authenticated && |
| 375 | !auth2_update_methods_lists(authctxt, |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 376 | auth_method, auth_submethod)) { |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 377 | debug3("%s: method %s: partial", __func__, |
| 378 | auth_method); |
| 379 | authenticated = 0; |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 380 | partial = 1; |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 384 | if (authenticated) { |
| 385 | if (!(ent->flags & MON_AUTHDECIDE)) |
| 386 | fatal("%s: unexpected authentication from %d", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 387 | __func__, ent->type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 388 | if (authctxt->pw->pw_uid == 0 && |
| 389 | !auth_root_allowed(auth_method)) |
| 390 | authenticated = 0; |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 391 | #ifdef USE_PAM |
| 392 | /* PAM needs to perform account checks after auth */ |
Damien Miller | 6aef38f | 2003-11-18 10:45:20 +1100 | [diff] [blame] | 393 | if (options.use_pam && authenticated) { |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 394 | Buffer m; |
| 395 | |
| 396 | buffer_init(&m); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 397 | mm_request_receive_expect(pmonitor->m_sendfd, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 398 | MONITOR_REQ_PAM_ACCOUNT, &m); |
| 399 | authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m); |
| 400 | buffer_free(&m); |
| 401 | } |
| 402 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 403 | } |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 404 | if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) { |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 405 | auth_log(authctxt, authenticated, partial, |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 406 | auth_method, auth_submethod); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 407 | if (!authenticated) |
| 408 | authctxt->failures++; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (!authctxt->valid) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 413 | fatal("%s: authenticated invalid user", __func__); |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 414 | if (strcmp(auth_method, "unknown") == 0) |
| 415 | fatal("%s: authentication method name unknown", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 416 | |
| 417 | debug("%s: %s has been authenticated by privileged process", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 418 | __func__, authctxt->user); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 419 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 420 | mm_get_keystate(pmonitor); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 421 | |
Damien Miller | 6a1937e | 2012-12-12 10:44:38 +1100 | [diff] [blame] | 422 | /* Drain any buffered messages from the child */ |
| 423 | while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0) |
| 424 | ; |
| 425 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 426 | close(pmonitor->m_sendfd); |
| 427 | close(pmonitor->m_log_recvfd); |
| 428 | pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 431 | static void |
| 432 | monitor_set_child_handler(pid_t pid) |
| 433 | { |
| 434 | monitor_child_pid = pid; |
| 435 | } |
| 436 | |
| 437 | static void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 438 | monitor_child_handler(int sig) |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 439 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 440 | kill(monitor_child_pid, sig); |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 441 | } |
| 442 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 443 | void |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 444 | monitor_child_postauth(struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 445 | { |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 446 | close(pmonitor->m_recvfd); |
| 447 | pmonitor->m_recvfd = -1; |
| 448 | |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 449 | monitor_set_child_handler(pmonitor->m_pid); |
| 450 | signal(SIGHUP, &monitor_child_handler); |
| 451 | signal(SIGTERM, &monitor_child_handler); |
Darren Tucker | 7fa339b | 2007-05-20 15:10:16 +1000 | [diff] [blame] | 452 | signal(SIGINT, &monitor_child_handler); |
Damien Miller | 146218a | 2014-08-27 04:11:55 +1000 | [diff] [blame] | 453 | #ifdef SIGXFSZ |
| 454 | signal(SIGXFSZ, SIG_IGN); |
| 455 | #endif |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 456 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 457 | if (compat20) { |
| 458 | mon_dispatch = mon_dispatch_postauth20; |
| 459 | |
| 460 | /* Permit requests for moduli and signatures */ |
| 461 | monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); |
| 462 | monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); |
| 463 | monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 464 | } else { |
| 465 | mon_dispatch = mon_dispatch_postauth15; |
| 466 | monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); |
| 467 | } |
| 468 | if (!no_pty_flag) { |
| 469 | monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1); |
| 470 | monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1); |
| 471 | } |
| 472 | |
| 473 | for (;;) |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 474 | monitor_read(pmonitor, mon_dispatch, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 478 | monitor_sync(struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 479 | { |
Damien Miller | 2d6b833 | 2002-06-21 15:59:49 +1000 | [diff] [blame] | 480 | if (options.compression) { |
| 481 | /* The member allocation is not visible, so sync it */ |
| 482 | mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback); |
| 483 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 484 | } |
| 485 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 486 | /* Allocation functions for zlib */ |
| 487 | static void * |
| 488 | mm_zalloc(struct mm_master *mm, u_int ncount, u_int size) |
| 489 | { |
| 490 | size_t len = (size_t) size * ncount; |
| 491 | void *address; |
| 492 | |
millert@openbsd.org | fd36834 | 2015-02-06 23:21:59 +0000 | [diff] [blame] | 493 | if (len == 0 || ncount > SIZE_MAX / size) |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 494 | fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size); |
| 495 | |
| 496 | address = mm_malloc(mm, len); |
| 497 | |
| 498 | return (address); |
| 499 | } |
| 500 | |
| 501 | static void |
| 502 | mm_zfree(struct mm_master *mm, void *address) |
| 503 | { |
| 504 | mm_free(mm, address); |
| 505 | } |
| 506 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 507 | static int |
| 508 | monitor_read_log(struct monitor *pmonitor) |
| 509 | { |
| 510 | Buffer logmsg; |
| 511 | u_int len, level; |
| 512 | char *msg; |
| 513 | |
| 514 | buffer_init(&logmsg); |
| 515 | |
| 516 | /* Read length */ |
| 517 | buffer_append_space(&logmsg, 4); |
| 518 | if (atomicio(read, pmonitor->m_log_recvfd, |
| 519 | buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) { |
| 520 | if (errno == EPIPE) { |
Damien Miller | a2876db | 2012-02-11 08:16:06 +1100 | [diff] [blame] | 521 | buffer_free(&logmsg); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 522 | debug("%s: child log fd closed", __func__); |
| 523 | close(pmonitor->m_log_recvfd); |
| 524 | pmonitor->m_log_recvfd = -1; |
| 525 | return -1; |
| 526 | } |
| 527 | fatal("%s: log fd read: %s", __func__, strerror(errno)); |
| 528 | } |
| 529 | len = buffer_get_int(&logmsg); |
| 530 | if (len <= 4 || len > 8192) |
| 531 | fatal("%s: invalid log message length %u", __func__, len); |
| 532 | |
| 533 | /* Read severity, message */ |
| 534 | buffer_clear(&logmsg); |
| 535 | buffer_append_space(&logmsg, len); |
| 536 | if (atomicio(read, pmonitor->m_log_recvfd, |
| 537 | buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) |
| 538 | fatal("%s: log fd read: %s", __func__, strerror(errno)); |
| 539 | |
| 540 | /* Log it */ |
| 541 | level = buffer_get_int(&logmsg); |
| 542 | msg = buffer_get_string(&logmsg, NULL); |
| 543 | if (log_level_name(level) == NULL) |
| 544 | fatal("%s: invalid log level %u (corrupted message?)", |
| 545 | __func__, level); |
| 546 | do_log2(level, "%s [preauth]", msg); |
| 547 | |
| 548 | buffer_free(&logmsg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 549 | free(msg); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 554 | int |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 555 | monitor_read(struct monitor *pmonitor, struct mon_table *ent, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 556 | struct mon_table **pent) |
| 557 | { |
| 558 | Buffer m; |
| 559 | int ret; |
| 560 | u_char type; |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 561 | struct pollfd pfd[2]; |
| 562 | |
| 563 | for (;;) { |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 564 | memset(&pfd, 0, sizeof(pfd)); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 565 | pfd[0].fd = pmonitor->m_sendfd; |
| 566 | pfd[0].events = POLLIN; |
| 567 | pfd[1].fd = pmonitor->m_log_recvfd; |
| 568 | pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN; |
Damien Miller | 7741ce8 | 2011-08-06 06:15:15 +1000 | [diff] [blame] | 569 | if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) { |
| 570 | if (errno == EINTR || errno == EAGAIN) |
| 571 | continue; |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 572 | fatal("%s: poll: %s", __func__, strerror(errno)); |
Damien Miller | 7741ce8 | 2011-08-06 06:15:15 +1000 | [diff] [blame] | 573 | } |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 574 | if (pfd[1].revents) { |
| 575 | /* |
| 576 | * Drain all log messages before processing next |
| 577 | * monitor request. |
| 578 | */ |
| 579 | monitor_read_log(pmonitor); |
| 580 | continue; |
| 581 | } |
| 582 | if (pfd[0].revents) |
| 583 | break; /* Continues below */ |
| 584 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 585 | |
| 586 | buffer_init(&m); |
| 587 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 588 | mm_request_receive(pmonitor->m_sendfd, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 589 | type = buffer_get_char(&m); |
| 590 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 591 | debug3("%s: checking request %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 592 | |
| 593 | while (ent->f != NULL) { |
| 594 | if (ent->type == type) |
| 595 | break; |
| 596 | ent++; |
| 597 | } |
| 598 | |
| 599 | if (ent->f != NULL) { |
| 600 | if (!(ent->flags & MON_PERMIT)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 601 | fatal("%s: unpermitted request %d", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 602 | type); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 603 | ret = (*ent->f)(pmonitor->m_sendfd, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 604 | buffer_free(&m); |
| 605 | |
| 606 | /* The child may use this request only once, disable it */ |
| 607 | if (ent->flags & MON_ONCE) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 608 | debug2("%s: %d used once, disabling now", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 609 | type); |
| 610 | ent->flags &= ~MON_PERMIT; |
| 611 | } |
| 612 | |
| 613 | if (pent != NULL) |
| 614 | *pent = ent; |
| 615 | |
| 616 | return ret; |
| 617 | } |
| 618 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 619 | fatal("%s: unsupported request: %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 620 | |
| 621 | /* NOTREACHED */ |
| 622 | return (-1); |
| 623 | } |
| 624 | |
| 625 | /* allowed key state */ |
| 626 | static int |
| 627 | monitor_allowed_key(u_char *blob, u_int bloblen) |
| 628 | { |
| 629 | /* make sure key is allowed */ |
| 630 | if (key_blob == NULL || key_bloblen != bloblen || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 631 | timingsafe_bcmp(key_blob, blob, key_bloblen)) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 632 | return (0); |
| 633 | return (1); |
| 634 | } |
| 635 | |
| 636 | static void |
| 637 | monitor_reset_key_state(void) |
| 638 | { |
| 639 | /* reset state */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 640 | free(key_blob); |
| 641 | free(hostbased_cuser); |
| 642 | free(hostbased_chost); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 643 | key_blob = NULL; |
| 644 | key_bloblen = 0; |
| 645 | key_blobtype = MM_NOKEY; |
| 646 | hostbased_cuser = NULL; |
| 647 | hostbased_chost = NULL; |
| 648 | } |
| 649 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 650 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 651 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 652 | mm_answer_moduli(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 653 | { |
| 654 | DH *dh; |
| 655 | int min, want, max; |
| 656 | |
| 657 | min = buffer_get_int(m); |
| 658 | want = buffer_get_int(m); |
| 659 | max = buffer_get_int(m); |
| 660 | |
| 661 | debug3("%s: got parameters: %d %d %d", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 662 | __func__, min, want, max); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 663 | /* We need to check here, too, in case the child got corrupted */ |
| 664 | if (max < min || want < min || max < want) |
| 665 | fatal("%s: bad parameters: %d %d %d", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 666 | __func__, min, want, max); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 667 | |
| 668 | buffer_clear(m); |
| 669 | |
| 670 | dh = choose_dh(min, want, max); |
| 671 | if (dh == NULL) { |
| 672 | buffer_put_char(m, 0); |
| 673 | return (0); |
| 674 | } else { |
| 675 | /* Send first bignum */ |
| 676 | buffer_put_char(m, 1); |
| 677 | buffer_put_bignum2(m, dh->p); |
| 678 | buffer_put_bignum2(m, dh->g); |
| 679 | |
| 680 | DH_free(dh); |
| 681 | } |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 682 | mm_request_send(sock, MONITOR_ANS_MODULI, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 683 | return (0); |
| 684 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 685 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 686 | |
| 687 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 688 | mm_answer_sign(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 689 | { |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 690 | struct ssh *ssh = active_state; /* XXX */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 691 | extern int auth_sock; /* XXX move to state struct? */ |
| 692 | struct sshkey *key; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 693 | struct sshbuf *sigbuf; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 694 | u_char *p; |
| 695 | u_char *signature; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 696 | size_t datlen, siglen; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 697 | int r, keyid, is_proof = 0; |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 698 | const char proof_req[] = "hostkeys-prove-00@openssh.com"; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 699 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 700 | debug3("%s", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 701 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 702 | if ((r = sshbuf_get_u32(m, &keyid)) != 0 || |
| 703 | (r = sshbuf_get_string(m, &p, &datlen)) != 0) |
| 704 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 705 | |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 706 | /* |
Damien Miller | 041ab7c | 2010-09-10 11:23:34 +1000 | [diff] [blame] | 707 | * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes), |
| 708 | * SHA384 (48 bytes) and SHA512 (64 bytes). |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 709 | * |
| 710 | * Otherwise, verify the signature request is for a hostkey |
| 711 | * proof. |
| 712 | * |
| 713 | * XXX perform similar check for KEX signature requests too? |
| 714 | * it's not trivial, since what is signed is the hash, rather |
| 715 | * than the full kex structure... |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 716 | */ |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 717 | if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) { |
| 718 | /* |
| 719 | * Construct expected hostkey proof and compare it to what |
| 720 | * the client sent us. |
| 721 | */ |
| 722 | if (session_id2_len == 0) /* hostkeys is never first */ |
| 723 | fatal("%s: bad data length: %zu", __func__, datlen); |
| 724 | if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL) |
| 725 | fatal("%s: no hostkey for index %d", __func__, keyid); |
| 726 | if ((sigbuf = sshbuf_new()) == NULL) |
| 727 | fatal("%s: sshbuf_new", __func__); |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 728 | if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 || |
| 729 | (r = sshbuf_put_string(sigbuf, session_id2, |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 730 | session_id2_len) != 0) || |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 731 | (r = sshkey_puts(key, sigbuf)) != 0) |
| 732 | fatal("%s: couldn't prepare private key " |
| 733 | "proof buffer: %s", __func__, ssh_err(r)); |
| 734 | if (datlen != sshbuf_len(sigbuf) || |
| 735 | memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0) |
| 736 | fatal("%s: bad data length: %zu, hostkey proof len %zu", |
| 737 | __func__, datlen, sshbuf_len(sigbuf)); |
| 738 | sshbuf_free(sigbuf); |
| 739 | is_proof = 1; |
| 740 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 741 | |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 742 | /* save session id, it will be passed on the first call */ |
| 743 | if (session_id2_len == 0) { |
| 744 | session_id2_len = datlen; |
| 745 | session_id2 = xmalloc(session_id2_len); |
| 746 | memcpy(session_id2, p, session_id2_len); |
| 747 | } |
| 748 | |
Damien Miller | 85b45e0 | 2013-07-20 13:21:52 +1000 | [diff] [blame] | 749 | if ((key = get_hostkey_by_index(keyid)) != NULL) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 750 | if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, |
| 751 | datafellows)) != 0) |
| 752 | fatal("%s: sshkey_sign failed: %s", |
| 753 | __func__, ssh_err(r)); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 754 | } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL && |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 755 | auth_sock > 0) { |
| 756 | if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen, |
| 757 | p, datlen, datafellows)) != 0) { |
| 758 | fatal("%s: ssh_agent_sign failed: %s", |
| 759 | __func__, ssh_err(r)); |
| 760 | } |
Damien Miller | 85b45e0 | 2013-07-20 13:21:52 +1000 | [diff] [blame] | 761 | } else |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 762 | fatal("%s: no hostkey from index %d", __func__, keyid); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 763 | |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 764 | debug3("%s: %s signature %p(%zu)", __func__, |
| 765 | is_proof ? "KEX" : "hostkey proof", signature, siglen); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 766 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 767 | sshbuf_reset(m); |
| 768 | if ((r = sshbuf_put_string(m, signature, siglen)) != 0) |
| 769 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 770 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 771 | free(p); |
| 772 | free(signature); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 773 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 774 | mm_request_send(sock, MONITOR_ANS_SIGN, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 775 | |
| 776 | /* Turn on permissions for getpwnam */ |
| 777 | monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); |
| 778 | |
| 779 | return (0); |
| 780 | } |
| 781 | |
| 782 | /* Retrieves the password entry and also checks if the user is permitted */ |
| 783 | |
| 784 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 785 | mm_answer_pwnamallow(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 786 | { |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 787 | char *username; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 788 | struct passwd *pwent; |
| 789 | int allowed = 0; |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 790 | u_int i; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 791 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 792 | debug3("%s", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 793 | |
| 794 | if (authctxt->attempt++ != 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 795 | fatal("%s: multiple attempts for getpwnam", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 796 | |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 797 | username = buffer_get_string(m, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 798 | |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 799 | pwent = getpwnamallow(username); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 800 | |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 801 | authctxt->user = xstrdup(username); |
| 802 | setproctitle("%s [priv]", pwent ? username : "unknown"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 803 | free(username); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 804 | |
| 805 | buffer_clear(m); |
| 806 | |
| 807 | if (pwent == NULL) { |
| 808 | buffer_put_char(m, 0); |
Damien Miller | f96d183 | 2003-11-18 22:01:48 +1100 | [diff] [blame] | 809 | authctxt->pw = fakepw(); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 810 | goto out; |
| 811 | } |
| 812 | |
| 813 | allowed = 1; |
| 814 | authctxt->pw = pwent; |
| 815 | authctxt->valid = 1; |
| 816 | |
| 817 | buffer_put_char(m, 1); |
| 818 | buffer_put_string(m, pwent, sizeof(struct passwd)); |
| 819 | buffer_put_cstring(m, pwent->pw_name); |
| 820 | buffer_put_cstring(m, "*"); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 821 | #ifdef HAVE_STRUCT_PASSWD_PW_GECOS |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 822 | buffer_put_cstring(m, pwent->pw_gecos); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 823 | #endif |
| 824 | #ifdef HAVE_STRUCT_PASSWD_PW_CLASS |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 825 | buffer_put_cstring(m, pwent->pw_class); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 826 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 827 | buffer_put_cstring(m, pwent->pw_dir); |
| 828 | buffer_put_cstring(m, pwent->pw_shell); |
Darren Tucker | 2f8b3d9 | 2007-12-02 23:02:15 +1100 | [diff] [blame] | 829 | |
| 830 | out: |
Darren Tucker | 1629c07 | 2007-02-19 22:25:37 +1100 | [diff] [blame] | 831 | buffer_put_string(m, &options, sizeof(options)); |
Damien Miller | f2e407e | 2011-05-20 19:04:14 +1000 | [diff] [blame] | 832 | |
| 833 | #define M_CP_STROPT(x) do { \ |
| 834 | if (options.x != NULL) \ |
| 835 | buffer_put_cstring(m, options.x); \ |
| 836 | } while (0) |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 837 | #define M_CP_STRARRAYOPT(x, nx) do { \ |
| 838 | for (i = 0; i < options.nx; i++) \ |
| 839 | buffer_put_cstring(m, options.x[i]); \ |
| 840 | } while (0) |
Damien Miller | f2e407e | 2011-05-20 19:04:14 +1000 | [diff] [blame] | 841 | /* See comment in servconf.h */ |
| 842 | COPY_MATCH_STRING_OPTS(); |
| 843 | #undef M_CP_STROPT |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 844 | #undef M_CP_STRARRAYOPT |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 845 | |
| 846 | /* Create valid auth method lists */ |
| 847 | if (compat20 && auth2_setup_methods_lists(authctxt) != 0) { |
| 848 | /* |
| 849 | * The monitor will continue long enough to let the child |
| 850 | * run to it's packet_disconnect(), but it must not allow any |
| 851 | * authentication to succeed. |
| 852 | */ |
| 853 | debug("%s: no valid authentication method lists", __func__); |
| 854 | } |
| 855 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 856 | debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 857 | mm_request_send(sock, MONITOR_ANS_PWNAM, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 858 | |
| 859 | /* For SSHv1 allow authentication now */ |
| 860 | if (!compat20) |
| 861 | monitor_permit_authentications(1); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 862 | else { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 863 | /* Allow service/style information on the auth context */ |
| 864 | monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 865 | monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1); |
| 866 | } |
Damien Miller | a33501b | 2002-05-08 12:24:42 +1000 | [diff] [blame] | 867 | #ifdef USE_PAM |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 868 | if (options.use_pam) |
| 869 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1); |
Damien Miller | a33501b | 2002-05-08 12:24:42 +1000 | [diff] [blame] | 870 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 871 | |
| 872 | return (0); |
| 873 | } |
| 874 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 875 | int mm_answer_auth2_read_banner(int sock, Buffer *m) |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 876 | { |
| 877 | char *banner; |
| 878 | |
| 879 | buffer_clear(m); |
| 880 | banner = auth2_read_banner(); |
| 881 | buffer_put_cstring(m, banner != NULL ? banner : ""); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 882 | mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 883 | free(banner); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 884 | |
| 885 | return (0); |
| 886 | } |
| 887 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 888 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 889 | mm_answer_authserv(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 890 | { |
| 891 | monitor_permit_authentications(1); |
| 892 | |
| 893 | authctxt->service = buffer_get_string(m, NULL); |
| 894 | authctxt->style = buffer_get_string(m, NULL); |
| 895 | debug3("%s: service=%s, style=%s", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 896 | __func__, authctxt->service, authctxt->style); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 897 | |
| 898 | if (strlen(authctxt->style) == 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 899 | free(authctxt->style); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 900 | authctxt->style = NULL; |
| 901 | } |
| 902 | |
| 903 | return (0); |
| 904 | } |
| 905 | |
| 906 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 907 | mm_answer_authpassword(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 908 | { |
| 909 | static int call_count; |
| 910 | char *passwd; |
Ben Lindstrom | 7cea16b | 2002-07-23 21:13:40 +0000 | [diff] [blame] | 911 | int authenticated; |
| 912 | u_int plen; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 913 | |
| 914 | passwd = buffer_get_string(m, &plen); |
| 915 | /* Only authenticate if the context is valid */ |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 916 | authenticated = options.password_authentication && |
Damien Miller | 856f0be | 2003-09-03 07:32:45 +1000 | [diff] [blame] | 917 | auth_password(authctxt, passwd); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 918 | explicit_bzero(passwd, strlen(passwd)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 919 | free(passwd); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 920 | |
| 921 | buffer_clear(m); |
| 922 | buffer_put_int(m, authenticated); |
| 923 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 924 | debug3("%s: sending result %d", __func__, authenticated); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 925 | mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 926 | |
| 927 | call_count++; |
| 928 | if (plen == 0 && call_count == 1) |
| 929 | auth_method = "none"; |
| 930 | else |
| 931 | auth_method = "password"; |
| 932 | |
| 933 | /* Causes monitor loop to terminate if authenticated */ |
| 934 | return (authenticated); |
| 935 | } |
| 936 | |
| 937 | #ifdef BSD_AUTH |
| 938 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 939 | mm_answer_bsdauthquery(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 940 | { |
| 941 | char *name, *infotxt; |
| 942 | u_int numprompts; |
| 943 | u_int *echo_on; |
| 944 | char **prompts; |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 945 | u_int success; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 946 | |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 947 | success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, |
| 948 | &prompts, &echo_on) < 0 ? 0 : 1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 949 | |
| 950 | buffer_clear(m); |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 951 | buffer_put_int(m, success); |
| 952 | if (success) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 953 | buffer_put_cstring(m, prompts[0]); |
| 954 | |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 955 | debug3("%s: sending challenge success: %u", __func__, success); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 956 | mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 957 | |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 958 | if (success) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 959 | free(name); |
| 960 | free(infotxt); |
| 961 | free(prompts); |
| 962 | free(echo_on); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | return (0); |
| 966 | } |
| 967 | |
| 968 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 969 | mm_answer_bsdauthrespond(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 970 | { |
| 971 | char *response; |
| 972 | int authok; |
| 973 | |
| 974 | if (authctxt->as == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 975 | fatal("%s: no bsd auth session", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 976 | |
| 977 | response = buffer_get_string(m, NULL); |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 978 | authok = options.challenge_response_authentication && |
| 979 | auth_userresponse(authctxt->as, response, 0); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 980 | authctxt->as = NULL; |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 981 | debug3("%s: <%s> = <%d>", __func__, response, authok); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 982 | free(response); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 983 | |
| 984 | buffer_clear(m); |
| 985 | buffer_put_int(m, authok); |
| 986 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 987 | debug3("%s: sending authenticated: %d", __func__, authok); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 988 | mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 989 | |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 990 | if (compat20) { |
| 991 | auth_method = "keyboard-interactive"; |
| 992 | auth_submethod = "bsdauth"; |
| 993 | } else |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 994 | auth_method = "bsdauth"; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 995 | |
| 996 | return (authok != 0); |
| 997 | } |
| 998 | #endif |
| 999 | |
| 1000 | #ifdef SKEY |
| 1001 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1002 | mm_answer_skeyquery(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1003 | { |
| 1004 | struct skey skey; |
| 1005 | char challenge[1024]; |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 1006 | u_int success; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1007 | |
Darren Tucker | 06a8cfe | 2004-04-14 17:24:30 +1000 | [diff] [blame] | 1008 | success = _compat_skeychallenge(&skey, authctxt->user, challenge, |
| 1009 | sizeof(challenge)) < 0 ? 0 : 1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1010 | |
| 1011 | buffer_clear(m); |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 1012 | buffer_put_int(m, success); |
| 1013 | if (success) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1014 | buffer_put_cstring(m, challenge); |
| 1015 | |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 1016 | debug3("%s: sending challenge success: %u", __func__, success); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1017 | mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1018 | |
| 1019 | return (0); |
| 1020 | } |
| 1021 | |
| 1022 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1023 | mm_answer_skeyrespond(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1024 | { |
| 1025 | char *response; |
| 1026 | int authok; |
| 1027 | |
| 1028 | response = buffer_get_string(m, NULL); |
| 1029 | |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1030 | authok = (options.challenge_response_authentication && |
| 1031 | authctxt->valid && |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1032 | skey_haskey(authctxt->pw->pw_name) == 0 && |
| 1033 | skey_passcheck(authctxt->pw->pw_name, response) != -1); |
| 1034 | |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1035 | free(response); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1036 | |
| 1037 | buffer_clear(m); |
| 1038 | buffer_put_int(m, authok); |
| 1039 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1040 | debug3("%s: sending authenticated: %d", __func__, authok); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1041 | mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1042 | |
| 1043 | auth_method = "skey"; |
| 1044 | |
| 1045 | return (authok != 0); |
| 1046 | } |
| 1047 | #endif |
| 1048 | |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1049 | #ifdef USE_PAM |
| 1050 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1051 | mm_answer_pam_start(int sock, Buffer *m) |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1052 | { |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 1053 | if (!options.use_pam) |
| 1054 | fatal("UsePAM not set, but ended up in %s anyway", __func__); |
| 1055 | |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 1056 | start_pam(authctxt); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1057 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1058 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1); |
| 1059 | |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1060 | return (0); |
| 1061 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1062 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1063 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1064 | mm_answer_pam_account(int sock, Buffer *m) |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1065 | { |
| 1066 | u_int ret; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1067 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1068 | if (!options.use_pam) |
| 1069 | fatal("UsePAM not set, but ended up in %s anyway", __func__); |
| 1070 | |
| 1071 | ret = do_pam_account(); |
| 1072 | |
| 1073 | buffer_put_int(m, ret); |
Darren Tucker | d4f04ae | 2005-09-30 10:23:21 +1000 | [diff] [blame] | 1074 | buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1075 | |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1076 | mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1077 | |
| 1078 | return (ret); |
| 1079 | } |
| 1080 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1081 | static void *sshpam_ctxt, *sshpam_authok; |
| 1082 | extern KbdintDevice sshpam_device; |
| 1083 | |
| 1084 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1085 | mm_answer_pam_init_ctx(int sock, Buffer *m) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1086 | { |
| 1087 | |
| 1088 | debug3("%s", __func__); |
| 1089 | authctxt->user = buffer_get_string(m, NULL); |
| 1090 | sshpam_ctxt = (sshpam_device.init_ctx)(authctxt); |
| 1091 | sshpam_authok = NULL; |
| 1092 | buffer_clear(m); |
| 1093 | if (sshpam_ctxt != NULL) { |
| 1094 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1); |
| 1095 | buffer_put_int(m, 1); |
| 1096 | } else { |
| 1097 | buffer_put_int(m, 0); |
| 1098 | } |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1099 | mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1100 | return (0); |
| 1101 | } |
| 1102 | |
| 1103 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1104 | mm_answer_pam_query(int sock, Buffer *m) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1105 | { |
Darren Tucker | 8b7a055 | 2010-08-03 15:50:16 +1000 | [diff] [blame] | 1106 | char *name = NULL, *info = NULL, **prompts = NULL; |
| 1107 | u_int i, num = 0, *echo_on = 0; |
Damien Miller | 04b6533 | 2005-07-17 17:53:31 +1000 | [diff] [blame] | 1108 | int ret; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1109 | |
| 1110 | debug3("%s", __func__); |
| 1111 | sshpam_authok = NULL; |
| 1112 | ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on); |
| 1113 | if (ret == 0 && num == 0) |
| 1114 | sshpam_authok = sshpam_ctxt; |
| 1115 | if (num > 1 || name == NULL || info == NULL) |
| 1116 | ret = -1; |
| 1117 | buffer_clear(m); |
| 1118 | buffer_put_int(m, ret); |
| 1119 | buffer_put_cstring(m, name); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1120 | free(name); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1121 | buffer_put_cstring(m, info); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1122 | free(info); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1123 | buffer_put_int(m, num); |
| 1124 | for (i = 0; i < num; ++i) { |
| 1125 | buffer_put_cstring(m, prompts[i]); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1126 | free(prompts[i]); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1127 | buffer_put_int(m, echo_on[i]); |
| 1128 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1129 | free(prompts); |
| 1130 | free(echo_on); |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 1131 | auth_method = "keyboard-interactive"; |
| 1132 | auth_submethod = "pam"; |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1133 | mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1134 | return (0); |
| 1135 | } |
| 1136 | |
| 1137 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1138 | mm_answer_pam_respond(int sock, Buffer *m) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1139 | { |
| 1140 | char **resp; |
Damien Miller | 04b6533 | 2005-07-17 17:53:31 +1000 | [diff] [blame] | 1141 | u_int i, num; |
| 1142 | int ret; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1143 | |
| 1144 | debug3("%s", __func__); |
| 1145 | sshpam_authok = NULL; |
| 1146 | num = buffer_get_int(m); |
| 1147 | if (num > 0) { |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 1148 | resp = xcalloc(num, sizeof(char *)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1149 | for (i = 0; i < num; ++i) |
| 1150 | resp[i] = buffer_get_string(m, NULL); |
| 1151 | ret = (sshpam_device.respond)(sshpam_ctxt, num, resp); |
| 1152 | for (i = 0; i < num; ++i) |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1153 | free(resp[i]); |
| 1154 | free(resp); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1155 | } else { |
| 1156 | ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL); |
| 1157 | } |
| 1158 | buffer_clear(m); |
| 1159 | buffer_put_int(m, ret); |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1160 | mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m); |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 1161 | auth_method = "keyboard-interactive"; |
| 1162 | auth_submethod = "pam"; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1163 | if (ret == 0) |
| 1164 | sshpam_authok = sshpam_ctxt; |
| 1165 | return (0); |
| 1166 | } |
| 1167 | |
| 1168 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1169 | mm_answer_pam_free_ctx(int sock, Buffer *m) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1170 | { |
| 1171 | |
| 1172 | debug3("%s", __func__); |
| 1173 | (sshpam_device.free_ctx)(sshpam_ctxt); |
| 1174 | buffer_clear(m); |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1175 | mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m); |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 1176 | auth_method = "keyboard-interactive"; |
| 1177 | auth_submethod = "pam"; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1178 | return (sshpam_authok == sshpam_ctxt); |
| 1179 | } |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1180 | #endif |
| 1181 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1182 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1183 | mm_answer_keyallowed(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1184 | { |
| 1185 | Key *key; |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 1186 | char *cuser, *chost; |
| 1187 | u_char *blob; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1188 | u_int bloblen; |
| 1189 | enum mm_keytype type = 0; |
| 1190 | int allowed = 0; |
| 1191 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1192 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1193 | |
| 1194 | type = buffer_get_int(m); |
| 1195 | cuser = buffer_get_string(m, NULL); |
| 1196 | chost = buffer_get_string(m, NULL); |
| 1197 | blob = buffer_get_string(m, &bloblen); |
| 1198 | |
| 1199 | key = key_from_blob(blob, bloblen); |
| 1200 | |
| 1201 | if ((compat20 && type == MM_RSAHOSTKEY) || |
| 1202 | (!compat20 && type != MM_RSAHOSTKEY)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1203 | fatal("%s: key type and protocol mismatch", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1204 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1205 | debug3("%s: key_from_blob: %p", __func__, key); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1206 | |
Damien Miller | 3e3b514 | 2003-11-17 21:13:40 +1100 | [diff] [blame] | 1207 | if (key != NULL && authctxt->valid) { |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 1208 | /* These should not make it past the privsep child */ |
| 1209 | if (key_type_plain(key->type) == KEY_RSA && |
| 1210 | (datafellows & SSH_BUG_RSASIGMD5) != 0) |
| 1211 | fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__); |
| 1212 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1213 | switch (type) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1214 | case MM_USERKEY: |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1215 | allowed = options.pubkey_authentication && |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 1216 | !auth2_userkey_already_used(authctxt, key) && |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 1217 | match_pattern_list(sshkey_ssh_name(key), |
| 1218 | options.pubkey_key_types, |
| 1219 | strlen(options.pubkey_key_types), 0) == 1 && |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1220 | user_key_allowed(authctxt->pw, key); |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 1221 | pubkey_auth_info(authctxt, key, NULL); |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1222 | auth_method = "publickey"; |
Darren Tucker | f2c16d3 | 2008-06-14 08:59:49 +1000 | [diff] [blame] | 1223 | if (options.pubkey_authentication && allowed != 1) |
| 1224 | auth_clear_options(); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1225 | break; |
| 1226 | case MM_HOSTKEY: |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1227 | allowed = options.hostbased_authentication && |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 1228 | match_pattern_list(sshkey_ssh_name(key), |
| 1229 | options.hostbased_key_types, |
| 1230 | strlen(options.hostbased_key_types), 0) == 1 && |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1231 | hostbased_key_allowed(authctxt->pw, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1232 | cuser, chost, key); |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 1233 | pubkey_auth_info(authctxt, key, |
| 1234 | "client user \"%.100s\", client host \"%.100s\"", |
| 1235 | cuser, chost); |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1236 | auth_method = "hostbased"; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1237 | break; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1238 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1239 | case MM_RSAHOSTKEY: |
| 1240 | key->type = KEY_RSA1; /* XXX */ |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1241 | allowed = options.rhosts_rsa_authentication && |
| 1242 | auth_rhosts_rsa_key_allowed(authctxt->pw, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1243 | cuser, chost, key); |
Darren Tucker | f2c16d3 | 2008-06-14 08:59:49 +1000 | [diff] [blame] | 1244 | if (options.rhosts_rsa_authentication && allowed != 1) |
| 1245 | auth_clear_options(); |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1246 | auth_method = "rsa"; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1247 | break; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1248 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1249 | default: |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1250 | fatal("%s: unknown key type %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1253 | } |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1254 | if (key != NULL) |
| 1255 | key_free(key); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1256 | |
| 1257 | /* clear temporarily storage (used by verify) */ |
| 1258 | monitor_reset_key_state(); |
| 1259 | |
| 1260 | if (allowed) { |
| 1261 | /* Save temporarily for comparison in verify */ |
| 1262 | key_blob = blob; |
| 1263 | key_bloblen = bloblen; |
| 1264 | key_blobtype = type; |
| 1265 | hostbased_cuser = cuser; |
| 1266 | hostbased_chost = chost; |
Damien Miller | 96937bd | 2006-03-26 14:01:54 +1100 | [diff] [blame] | 1267 | } else { |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1268 | /* Log failed attempt */ |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 1269 | auth_log(authctxt, 0, 0, auth_method, NULL); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1270 | free(blob); |
| 1271 | free(cuser); |
| 1272 | free(chost); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | debug3("%s: key %p is %s", |
Darren Tucker | 2784f1f | 2008-07-04 13:51:45 +1000 | [diff] [blame] | 1276 | __func__, key, allowed ? "allowed" : "not allowed"); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1277 | |
| 1278 | buffer_clear(m); |
| 1279 | buffer_put_int(m, allowed); |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 1280 | buffer_put_int(m, forced_command != NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1281 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1282 | mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1283 | |
| 1284 | if (type == MM_RSAHOSTKEY) |
| 1285 | monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); |
| 1286 | |
| 1287 | return (0); |
| 1288 | } |
| 1289 | |
| 1290 | static int |
| 1291 | monitor_valid_userblob(u_char *data, u_int datalen) |
| 1292 | { |
| 1293 | Buffer b; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1294 | char *p, *userstyle; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1295 | u_int len; |
| 1296 | int fail = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1297 | |
| 1298 | buffer_init(&b); |
| 1299 | buffer_append(&b, data, datalen); |
| 1300 | |
| 1301 | if (datafellows & SSH_OLD_SESSIONID) { |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1302 | p = buffer_ptr(&b); |
| 1303 | len = buffer_len(&b); |
| 1304 | if ((session_id2 == NULL) || |
| 1305 | (len < session_id2_len) || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1306 | (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1307 | fail++; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1308 | buffer_consume(&b, session_id2_len); |
| 1309 | } else { |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1310 | p = buffer_get_string(&b, &len); |
| 1311 | if ((session_id2 == NULL) || |
| 1312 | (len != session_id2_len) || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1313 | (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1314 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1315 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1316 | } |
| 1317 | if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) |
| 1318 | fail++; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1319 | p = buffer_get_cstring(&b, NULL); |
| 1320 | xasprintf(&userstyle, "%s%s%s", authctxt->user, |
| 1321 | authctxt->style ? ":" : "", |
| 1322 | authctxt->style ? authctxt->style : ""); |
| 1323 | if (strcmp(userstyle, p) != 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1324 | logit("wrong user name passed to monitor: expected %s != %.100s", |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1325 | userstyle, p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1326 | fail++; |
| 1327 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1328 | free(userstyle); |
| 1329 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1330 | buffer_skip_string(&b); |
| 1331 | if (datafellows & SSH_BUG_PKAUTH) { |
| 1332 | if (!buffer_get_char(&b)) |
| 1333 | fail++; |
| 1334 | } else { |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1335 | p = buffer_get_cstring(&b, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1336 | if (strcmp("publickey", p) != 0) |
| 1337 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1338 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1339 | if (!buffer_get_char(&b)) |
| 1340 | fail++; |
| 1341 | buffer_skip_string(&b); |
| 1342 | } |
| 1343 | buffer_skip_string(&b); |
| 1344 | if (buffer_len(&b) != 0) |
| 1345 | fail++; |
| 1346 | buffer_free(&b); |
| 1347 | return (fail == 0); |
| 1348 | } |
| 1349 | |
| 1350 | static int |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 1351 | monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, |
| 1352 | char *chost) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1353 | { |
| 1354 | Buffer b; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1355 | char *p, *userstyle; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1356 | u_int len; |
| 1357 | int fail = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1358 | |
| 1359 | buffer_init(&b); |
| 1360 | buffer_append(&b, data, datalen); |
| 1361 | |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1362 | p = buffer_get_string(&b, &len); |
| 1363 | if ((session_id2 == NULL) || |
| 1364 | (len != session_id2_len) || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1365 | (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1366 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1367 | free(p); |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1368 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1369 | if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) |
| 1370 | fail++; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1371 | p = buffer_get_cstring(&b, NULL); |
| 1372 | xasprintf(&userstyle, "%s%s%s", authctxt->user, |
| 1373 | authctxt->style ? ":" : "", |
| 1374 | authctxt->style ? authctxt->style : ""); |
| 1375 | if (strcmp(userstyle, p) != 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1376 | logit("wrong user name passed to monitor: expected %s != %.100s", |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1377 | userstyle, p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1378 | fail++; |
| 1379 | } |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1380 | free(userstyle); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1381 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1382 | buffer_skip_string(&b); /* service */ |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1383 | p = buffer_get_cstring(&b, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1384 | if (strcmp(p, "hostbased") != 0) |
| 1385 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1386 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1387 | buffer_skip_string(&b); /* pkalg */ |
| 1388 | buffer_skip_string(&b); /* pkblob */ |
| 1389 | |
| 1390 | /* verify client host, strip trailing dot if necessary */ |
| 1391 | p = buffer_get_string(&b, NULL); |
| 1392 | if (((len = strlen(p)) > 0) && p[len - 1] == '.') |
| 1393 | p[len - 1] = '\0'; |
| 1394 | if (strcmp(p, chost) != 0) |
| 1395 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1396 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1397 | |
| 1398 | /* verify client user */ |
| 1399 | p = buffer_get_string(&b, NULL); |
| 1400 | if (strcmp(p, cuser) != 0) |
| 1401 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1402 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1403 | |
| 1404 | if (buffer_len(&b) != 0) |
| 1405 | fail++; |
| 1406 | buffer_free(&b); |
| 1407 | return (fail == 0); |
| 1408 | } |
| 1409 | |
| 1410 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1411 | mm_answer_keyverify(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1412 | { |
| 1413 | Key *key; |
| 1414 | u_char *signature, *data, *blob; |
| 1415 | u_int signaturelen, datalen, bloblen; |
| 1416 | int verified = 0; |
| 1417 | int valid_data = 0; |
| 1418 | |
| 1419 | blob = buffer_get_string(m, &bloblen); |
| 1420 | signature = buffer_get_string(m, &signaturelen); |
| 1421 | data = buffer_get_string(m, &datalen); |
| 1422 | |
| 1423 | if (hostbased_cuser == NULL || hostbased_chost == NULL || |
Ben Lindstrom | b57a4bf | 2002-03-27 18:00:59 +0000 | [diff] [blame] | 1424 | !monitor_allowed_key(blob, bloblen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1425 | fatal("%s: bad key, not previously allowed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1426 | |
| 1427 | key = key_from_blob(blob, bloblen); |
| 1428 | if (key == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1429 | fatal("%s: bad public key blob", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1430 | |
| 1431 | switch (key_blobtype) { |
| 1432 | case MM_USERKEY: |
| 1433 | valid_data = monitor_valid_userblob(data, datalen); |
| 1434 | break; |
| 1435 | case MM_HOSTKEY: |
| 1436 | valid_data = monitor_valid_hostbasedblob(data, datalen, |
| 1437 | hostbased_cuser, hostbased_chost); |
| 1438 | break; |
| 1439 | default: |
| 1440 | valid_data = 0; |
| 1441 | break; |
| 1442 | } |
| 1443 | if (!valid_data) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1444 | fatal("%s: bad signature data blob", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1445 | |
| 1446 | verified = key_verify(key, signature, signaturelen, data, datalen); |
| 1447 | debug3("%s: key %p signature %s", |
Darren Tucker | fbba735 | 2006-11-07 23:16:08 +1100 | [diff] [blame] | 1448 | __func__, key, (verified == 1) ? "verified" : "unverified"); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1449 | |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 1450 | /* If auth was successful then record key to ensure it isn't reused */ |
| 1451 | if (verified == 1) |
| 1452 | auth2_record_userkey(authctxt, key); |
| 1453 | else |
| 1454 | key_free(key); |
| 1455 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1456 | free(blob); |
| 1457 | free(signature); |
| 1458 | free(data); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1459 | |
Ben Lindstrom | e1c0912 | 2002-06-23 00:38:24 +0000 | [diff] [blame] | 1460 | auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased"; |
| 1461 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1462 | monitor_reset_key_state(); |
| 1463 | |
| 1464 | buffer_clear(m); |
| 1465 | buffer_put_int(m, verified); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1466 | mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1467 | |
Darren Tucker | fbba735 | 2006-11-07 23:16:08 +1100 | [diff] [blame] | 1468 | return (verified == 1); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | static void |
| 1472 | mm_record_login(Session *s, struct passwd *pw) |
| 1473 | { |
| 1474 | socklen_t fromlen; |
| 1475 | struct sockaddr_storage from; |
| 1476 | |
| 1477 | /* |
| 1478 | * Get IP address of client. If the connection is not a socket, let |
| 1479 | * the address be 0.0.0.0. |
| 1480 | */ |
| 1481 | memset(&from, 0, sizeof(from)); |
Damien Miller | ebc2306 | 2002-09-04 16:45:09 +1000 | [diff] [blame] | 1482 | fromlen = sizeof(from); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1483 | if (packet_connection_is_on_socket()) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1484 | if (getpeername(packet_get_connection_in(), |
Damien Miller | 9f3bd53 | 2006-03-26 14:07:52 +1100 | [diff] [blame] | 1485 | (struct sockaddr *)&from, &fromlen) < 0) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1486 | debug("getpeername: %.100s", strerror(errno)); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1487 | cleanup_exit(255); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | /* Record that there was a login on that tty from the remote host. */ |
| 1491 | record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, |
Damien Miller | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 1492 | get_remote_name_or_ip(utmp_len, options.use_dns), |
Damien Miller | ebc2306 | 2002-09-04 16:45:09 +1000 | [diff] [blame] | 1493 | (struct sockaddr *)&from, fromlen); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | static void |
| 1497 | mm_session_close(Session *s) |
| 1498 | { |
Damien Miller | 04bd8b0 | 2003-05-25 14:38:33 +1000 | [diff] [blame] | 1499 | debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1500 | if (s->ttyfd != -1) { |
Damien Miller | 9ab00b4 | 2006-08-05 12:40:11 +1000 | [diff] [blame] | 1501 | debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1502 | session_pty_cleanup2(s); |
| 1503 | } |
Damien Miller | 7207f64 | 2008-05-19 15:34:50 +1000 | [diff] [blame] | 1504 | session_unused(s->self); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1508 | mm_answer_pty(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1509 | { |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1510 | extern struct monitor *pmonitor; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1511 | Session *s; |
| 1512 | int res, fd0; |
| 1513 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1514 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1515 | |
| 1516 | buffer_clear(m); |
| 1517 | s = session_new(); |
| 1518 | if (s == NULL) |
| 1519 | goto error; |
| 1520 | s->authctxt = authctxt; |
| 1521 | s->pw = authctxt->pw; |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1522 | s->pid = pmonitor->m_pid; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1523 | res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); |
| 1524 | if (res == 0) |
| 1525 | goto error; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1526 | pty_setowner(authctxt->pw, s->tty); |
| 1527 | |
| 1528 | buffer_put_int(m, 1); |
| 1529 | buffer_put_cstring(m, s->tty); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1530 | |
| 1531 | /* We need to trick ttyslot */ |
| 1532 | if (dup2(s->ttyfd, 0) == -1) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1533 | fatal("%s: dup2", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1534 | |
| 1535 | mm_record_login(s, authctxt->pw); |
| 1536 | |
| 1537 | /* Now we can close the file descriptor again */ |
| 1538 | close(0); |
| 1539 | |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 1540 | /* send messages generated by record_login */ |
| 1541 | buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); |
| 1542 | buffer_clear(&loginmsg); |
| 1543 | |
| 1544 | mm_request_send(sock, MONITOR_ANS_PTY, m); |
| 1545 | |
Damien Miller | 54fd7cf | 2007-09-17 12:04:08 +1000 | [diff] [blame] | 1546 | if (mm_send_fd(sock, s->ptyfd) == -1 || |
| 1547 | mm_send_fd(sock, s->ttyfd) == -1) |
| 1548 | fatal("%s: send fds failed", __func__); |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 1549 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1550 | /* make sure nothing uses fd 0 */ |
| 1551 | if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1552 | fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1553 | if (fd0 != 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1554 | error("%s: fd0 %d != 0", __func__, fd0); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1555 | |
| 1556 | /* slave is not needed */ |
| 1557 | close(s->ttyfd); |
| 1558 | s->ttyfd = s->ptyfd; |
| 1559 | /* no need to dup() because nobody closes ptyfd */ |
| 1560 | s->ptymaster = s->ptyfd; |
| 1561 | |
Damien Miller | 9ab00b4 | 2006-08-05 12:40:11 +1000 | [diff] [blame] | 1562 | debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1563 | |
| 1564 | return (0); |
| 1565 | |
| 1566 | error: |
| 1567 | if (s != NULL) |
| 1568 | mm_session_close(s); |
| 1569 | buffer_put_int(m, 0); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1570 | mm_request_send(sock, MONITOR_ANS_PTY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1571 | return (0); |
| 1572 | } |
| 1573 | |
| 1574 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1575 | mm_answer_pty_cleanup(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1576 | { |
| 1577 | Session *s; |
| 1578 | char *tty; |
| 1579 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1580 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1581 | |
| 1582 | tty = buffer_get_string(m, NULL); |
| 1583 | if ((s = session_by_tty(tty)) != NULL) |
| 1584 | mm_session_close(s); |
| 1585 | buffer_clear(m); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1586 | free(tty); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1587 | return (0); |
| 1588 | } |
| 1589 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1590 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1591 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1592 | mm_answer_sesskey(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1593 | { |
| 1594 | BIGNUM *p; |
| 1595 | int rsafail; |
| 1596 | |
| 1597 | /* Turn off permissions */ |
Darren Tucker | 5b53026 | 2005-02-09 09:52:17 +1100 | [diff] [blame] | 1598 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1599 | |
| 1600 | if ((p = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1601 | fatal("%s: BN_new", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1602 | |
| 1603 | buffer_get_bignum2(m, p); |
| 1604 | |
| 1605 | rsafail = ssh1_session_key(p); |
| 1606 | |
| 1607 | buffer_clear(m); |
| 1608 | buffer_put_int(m, rsafail); |
| 1609 | buffer_put_bignum2(m, p); |
| 1610 | |
| 1611 | BN_clear_free(p); |
| 1612 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1613 | mm_request_send(sock, MONITOR_ANS_SESSKEY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1614 | |
| 1615 | /* Turn on permissions for sessid passing */ |
| 1616 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1); |
| 1617 | |
| 1618 | return (0); |
| 1619 | } |
| 1620 | |
| 1621 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1622 | mm_answer_sessid(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1623 | { |
| 1624 | int i; |
| 1625 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1626 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1627 | |
| 1628 | if (buffer_len(m) != 16) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1629 | fatal("%s: bad ssh1 session id", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1630 | for (i = 0; i < 16; i++) |
| 1631 | session_id[i] = buffer_get_char(m); |
| 1632 | |
| 1633 | /* Turn on permissions for getpwnam */ |
| 1634 | monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); |
| 1635 | |
| 1636 | return (0); |
| 1637 | } |
| 1638 | |
| 1639 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1640 | mm_answer_rsa_keyallowed(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1641 | { |
| 1642 | BIGNUM *client_n; |
| 1643 | Key *key = NULL; |
| 1644 | u_char *blob = NULL; |
| 1645 | u_int blen = 0; |
| 1646 | int allowed = 0; |
| 1647 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1648 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1649 | |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1650 | auth_method = "rsa"; |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1651 | if (options.rsa_authentication && authctxt->valid) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1652 | if ((client_n = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1653 | fatal("%s: BN_new", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1654 | buffer_get_bignum2(m, client_n); |
| 1655 | allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key); |
| 1656 | BN_clear_free(client_n); |
| 1657 | } |
| 1658 | buffer_clear(m); |
| 1659 | buffer_put_int(m, allowed); |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 1660 | buffer_put_int(m, forced_command != NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1661 | |
| 1662 | /* clear temporarily storage (used by generate challenge) */ |
| 1663 | monitor_reset_key_state(); |
| 1664 | |
| 1665 | if (allowed && key != NULL) { |
| 1666 | key->type = KEY_RSA; /* cheat for key_to_blob */ |
| 1667 | if (key_to_blob(key, &blob, &blen) == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1668 | fatal("%s: key_to_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1669 | buffer_put_string(m, blob, blen); |
| 1670 | |
| 1671 | /* Save temporarily for comparison in verify */ |
| 1672 | key_blob = blob; |
| 1673 | key_bloblen = blen; |
| 1674 | key_blobtype = MM_RSAUSERKEY; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1675 | } |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1676 | if (key != NULL) |
| 1677 | key_free(key); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1678 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1679 | mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1680 | |
| 1681 | monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); |
| 1682 | monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0); |
| 1683 | return (0); |
| 1684 | } |
| 1685 | |
| 1686 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1687 | mm_answer_rsa_challenge(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1688 | { |
| 1689 | Key *key = NULL; |
| 1690 | u_char *blob; |
| 1691 | u_int blen; |
| 1692 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1693 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1694 | |
| 1695 | if (!authctxt->valid) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1696 | fatal("%s: authctxt not valid", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1697 | blob = buffer_get_string(m, &blen); |
| 1698 | if (!monitor_allowed_key(blob, blen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1699 | fatal("%s: bad key, not previously allowed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1700 | if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1701 | fatal("%s: key type mismatch", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1702 | if ((key = key_from_blob(blob, blen)) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1703 | fatal("%s: received bad key", __func__); |
Damien Miller | 923e8bb | 2009-02-14 16:33:31 +1100 | [diff] [blame] | 1704 | if (key->type != KEY_RSA) |
| 1705 | fatal("%s: received bad key type %d", __func__, key->type); |
| 1706 | key->type = KEY_RSA1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1707 | if (ssh1_challenge) |
| 1708 | BN_clear_free(ssh1_challenge); |
| 1709 | ssh1_challenge = auth_rsa_generate_challenge(key); |
| 1710 | |
| 1711 | buffer_clear(m); |
| 1712 | buffer_put_bignum2(m, ssh1_challenge); |
| 1713 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1714 | debug3("%s sending reply", __func__); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1715 | mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1716 | |
| 1717 | monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1718 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1719 | free(blob); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1720 | key_free(key); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1721 | return (0); |
| 1722 | } |
| 1723 | |
| 1724 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1725 | mm_answer_rsa_response(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1726 | { |
| 1727 | Key *key = NULL; |
| 1728 | u_char *blob, *response; |
| 1729 | u_int blen, len; |
| 1730 | int success; |
| 1731 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1732 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1733 | |
| 1734 | if (!authctxt->valid) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1735 | fatal("%s: authctxt not valid", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1736 | if (ssh1_challenge == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1737 | fatal("%s: no ssh1_challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1738 | |
| 1739 | blob = buffer_get_string(m, &blen); |
| 1740 | if (!monitor_allowed_key(blob, blen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1741 | fatal("%s: bad key, not previously allowed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1742 | if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1743 | fatal("%s: key type mismatch: %d", __func__, key_blobtype); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1744 | if ((key = key_from_blob(blob, blen)) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1745 | fatal("%s: received bad key", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1746 | response = buffer_get_string(m, &len); |
| 1747 | if (len != 16) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1748 | fatal("%s: received bad response to challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1749 | success = auth_rsa_verify_response(key, ssh1_challenge, response); |
| 1750 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1751 | free(blob); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1752 | key_free(key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1753 | free(response); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1754 | |
| 1755 | auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa"; |
| 1756 | |
| 1757 | /* reset state */ |
| 1758 | BN_clear_free(ssh1_challenge); |
| 1759 | ssh1_challenge = NULL; |
| 1760 | monitor_reset_key_state(); |
| 1761 | |
| 1762 | buffer_clear(m); |
| 1763 | buffer_put_int(m, success); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1764 | mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1765 | |
| 1766 | return (success); |
| 1767 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1768 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1769 | |
| 1770 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1771 | mm_answer_term(int sock, Buffer *req) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1772 | { |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1773 | extern struct monitor *pmonitor; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1774 | int res, status; |
| 1775 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1776 | debug3("%s: tearing down sessions", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1777 | |
| 1778 | /* The child is terminating */ |
| 1779 | session_destroy_all(&mm_session_close); |
| 1780 | |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 1781 | #ifdef USE_PAM |
| 1782 | if (options.use_pam) |
| 1783 | sshpam_cleanup(); |
| 1784 | #endif |
| 1785 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1786 | while (waitpid(pmonitor->m_pid, &status, 0) == -1) |
Ben Lindstrom | 47fd811 | 2002-04-02 20:48:19 +0000 | [diff] [blame] | 1787 | if (errno != EINTR) |
| 1788 | exit(1); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1789 | |
| 1790 | res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; |
| 1791 | |
| 1792 | /* Terminate process */ |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 1793 | exit(res); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1796 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1797 | /* Report that an audit event occurred */ |
| 1798 | int |
| 1799 | mm_answer_audit_event(int socket, Buffer *m) |
| 1800 | { |
| 1801 | ssh_audit_event_t event; |
| 1802 | |
| 1803 | debug3("%s entering", __func__); |
| 1804 | |
| 1805 | event = buffer_get_int(m); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1806 | switch(event) { |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1807 | case SSH_AUTH_FAIL_PUBKEY: |
| 1808 | case SSH_AUTH_FAIL_HOSTBASED: |
| 1809 | case SSH_AUTH_FAIL_GSSAPI: |
| 1810 | case SSH_LOGIN_EXCEED_MAXTRIES: |
| 1811 | case SSH_LOGIN_ROOT_DENIED: |
| 1812 | case SSH_CONNECTION_CLOSE: |
| 1813 | case SSH_INVALID_USER: |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1814 | audit_event(event); |
| 1815 | break; |
| 1816 | default: |
| 1817 | fatal("Audit event type %d not permitted", event); |
| 1818 | } |
| 1819 | |
| 1820 | return (0); |
| 1821 | } |
| 1822 | |
| 1823 | int |
| 1824 | mm_answer_audit_command(int socket, Buffer *m) |
| 1825 | { |
| 1826 | u_int len; |
| 1827 | char *cmd; |
| 1828 | |
| 1829 | debug3("%s entering", __func__); |
| 1830 | cmd = buffer_get_string(m, &len); |
| 1831 | /* sanity check command, if so how? */ |
| 1832 | audit_run_command(cmd); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1833 | free(cmd); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1834 | return (0); |
| 1835 | } |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1836 | #endif /* SSH_AUDIT_EVENTS */ |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1837 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1838 | void |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1839 | monitor_apply_keystate(struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1840 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1841 | struct ssh *ssh = active_state; /* XXX */ |
| 1842 | struct kex *kex; |
| 1843 | int r; |
| 1844 | |
| 1845 | debug3("%s: packet_set_state", __func__); |
| 1846 | if ((r = ssh_packet_set_state(ssh, child_state)) != 0) |
| 1847 | fatal("%s: packet_set_state: %s", __func__, ssh_err(r)); |
| 1848 | sshbuf_free(child_state); |
| 1849 | child_state = NULL; |
| 1850 | |
| 1851 | if ((kex = ssh->kex) != 0) { |
| 1852 | /* XXX set callbacks */ |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 1853 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1854 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; |
| 1855 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; |
| 1856 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; |
| 1857 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 1858 | # ifdef OPENSSL_HAS_ECC |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1859 | kex->kex[KEX_ECDH_SHA2] = kexecdh_server; |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 1860 | # endif |
Damien Miller | 773dda2 | 2015-01-30 23:10:17 +1100 | [diff] [blame] | 1861 | #endif /* WITH_OPENSSL */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1862 | kex->kex[KEX_C25519_SHA256] = kexc25519_server; |
| 1863 | kex->load_host_public_key=&get_hostkey_public_by_type; |
| 1864 | kex->load_host_private_key=&get_hostkey_private_by_type; |
| 1865 | kex->host_key_index=&get_hostkey_index; |
| 1866 | kex->sign = sshd_hostkey_sign; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1869 | /* Update with new address */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1870 | if (options.compression) { |
| 1871 | ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib, |
| 1872 | (ssh_packet_comp_alloc_func *)mm_zalloc, |
| 1873 | (ssh_packet_comp_free_func *)mm_zfree); |
| 1874 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | /* This function requries careful sanity checking */ |
| 1878 | |
| 1879 | void |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1880 | mm_get_keystate(struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1881 | { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1882 | debug3("%s: Waiting for new keys", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1883 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1884 | if ((child_state = sshbuf_new()) == NULL) |
| 1885 | fatal("%s: sshbuf_new failed", __func__); |
| 1886 | mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, |
| 1887 | child_state); |
| 1888 | debug3("%s: GOT new keys", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1892 | /* XXX */ |
| 1893 | |
| 1894 | #define FD_CLOSEONEXEC(x) do { \ |
Damien Miller | 814ace0 | 2011-05-20 19:02:47 +1000 | [diff] [blame] | 1895 | if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1896 | fatal("fcntl(%d, F_SETFD)", x); \ |
| 1897 | } while (0) |
| 1898 | |
| 1899 | static void |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 1900 | monitor_openfds(struct monitor *mon, int do_logfds) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1901 | { |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 1902 | int pair[2]; |
| 1903 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1904 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 1905 | fatal("%s: socketpair: %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1906 | FD_CLOSEONEXEC(pair[0]); |
| 1907 | FD_CLOSEONEXEC(pair[1]); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 1908 | mon->m_recvfd = pair[0]; |
| 1909 | mon->m_sendfd = pair[1]; |
| 1910 | |
| 1911 | if (do_logfds) { |
| 1912 | if (pipe(pair) == -1) |
| 1913 | fatal("%s: pipe: %s", __func__, strerror(errno)); |
| 1914 | FD_CLOSEONEXEC(pair[0]); |
| 1915 | FD_CLOSEONEXEC(pair[1]); |
| 1916 | mon->m_log_recvfd = pair[0]; |
| 1917 | mon->m_log_sendfd = pair[1]; |
| 1918 | } else |
| 1919 | mon->m_log_recvfd = mon->m_log_sendfd = -1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | #define MM_MEMSIZE 65536 |
| 1923 | |
| 1924 | struct monitor * |
| 1925 | monitor_init(void) |
| 1926 | { |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1927 | struct ssh *ssh = active_state; /* XXX */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1928 | struct monitor *mon; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1929 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1930 | mon = xcalloc(1, sizeof(*mon)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1931 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 1932 | monitor_openfds(mon, 1); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1933 | |
| 1934 | /* Used to share zlib space across processes */ |
Damien Miller | 2d6b833 | 2002-06-21 15:59:49 +1000 | [diff] [blame] | 1935 | if (options.compression) { |
| 1936 | mon->m_zback = mm_create(NULL, MM_MEMSIZE); |
| 1937 | mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1938 | |
Damien Miller | 2d6b833 | 2002-06-21 15:59:49 +1000 | [diff] [blame] | 1939 | /* Compression needs to share state across borders */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1940 | ssh_packet_set_compress_hooks(ssh, mon->m_zlib, |
| 1941 | (ssh_packet_comp_alloc_func *)mm_zalloc, |
| 1942 | (ssh_packet_comp_free_func *)mm_zfree); |
Damien Miller | 2d6b833 | 2002-06-21 15:59:49 +1000 | [diff] [blame] | 1943 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1944 | |
| 1945 | return mon; |
| 1946 | } |
| 1947 | |
| 1948 | void |
| 1949 | monitor_reinit(struct monitor *mon) |
| 1950 | { |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 1951 | monitor_openfds(mon, 0); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1952 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1953 | |
| 1954 | #ifdef GSSAPI |
| 1955 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1956 | mm_answer_gss_setup_ctx(int sock, Buffer *m) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1957 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1958 | gss_OID_desc goid; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1959 | OM_uint32 major; |
| 1960 | u_int len; |
| 1961 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1962 | goid.elements = buffer_get_string(m, &len); |
| 1963 | goid.length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1964 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1965 | major = ssh_gssapi_server_ctx(&gsscontext, &goid); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1966 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1967 | free(goid.elements); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1968 | |
| 1969 | buffer_clear(m); |
| 1970 | buffer_put_int(m, major); |
| 1971 | |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 1972 | mm_request_send(sock, MONITOR_ANS_GSSSETUP, m); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1973 | |
| 1974 | /* Now we have a context, enable the step */ |
| 1975 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); |
| 1976 | |
| 1977 | return (0); |
| 1978 | } |
| 1979 | |
| 1980 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1981 | mm_answer_gss_accept_ctx(int sock, Buffer *m) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1982 | { |
| 1983 | gss_buffer_desc in; |
| 1984 | gss_buffer_desc out = GSS_C_EMPTY_BUFFER; |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 1985 | OM_uint32 major, minor; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1986 | OM_uint32 flags = 0; /* GSI needs this */ |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 1987 | u_int len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1988 | |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 1989 | in.value = buffer_get_string(m, &len); |
| 1990 | in.length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1991 | major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1992 | free(in.value); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1993 | |
| 1994 | buffer_clear(m); |
| 1995 | buffer_put_int(m, major); |
| 1996 | buffer_put_string(m, out.value, out.length); |
| 1997 | buffer_put_int(m, flags); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1998 | mm_request_send(sock, MONITOR_ANS_GSSSTEP, m); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1999 | |
| 2000 | gss_release_buffer(&minor, &out); |
| 2001 | |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 2002 | if (major == GSS_S_COMPLETE) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2003 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); |
| 2004 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2005 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2006 | } |
| 2007 | return (0); |
| 2008 | } |
| 2009 | |
| 2010 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2011 | mm_answer_gss_checkmic(int sock, Buffer *m) |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2012 | { |
| 2013 | gss_buffer_desc gssbuf, mic; |
| 2014 | OM_uint32 ret; |
| 2015 | u_int len; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2016 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2017 | gssbuf.value = buffer_get_string(m, &len); |
| 2018 | gssbuf.length = len; |
| 2019 | mic.value = buffer_get_string(m, &len); |
| 2020 | mic.length = len; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2021 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2022 | ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2023 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2024 | free(gssbuf.value); |
| 2025 | free(mic.value); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2026 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2027 | buffer_clear(m); |
| 2028 | buffer_put_int(m, ret); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2029 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2030 | mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2031 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2032 | if (!GSS_ERROR(ret)) |
| 2033 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2034 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2035 | return (0); |
| 2036 | } |
| 2037 | |
| 2038 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2039 | mm_answer_gss_userok(int sock, Buffer *m) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2040 | { |
| 2041 | int authenticated; |
| 2042 | |
| 2043 | authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user); |
| 2044 | |
| 2045 | buffer_clear(m); |
| 2046 | buffer_put_int(m, authenticated); |
| 2047 | |
| 2048 | debug3("%s: sending result %d", __func__, authenticated); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2049 | mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2050 | |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 2051 | auth_method = "gssapi-with-mic"; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2052 | |
| 2053 | /* Monitor loop will terminate if authenticated */ |
| 2054 | return (authenticated); |
| 2055 | } |
| 2056 | #endif /* GSSAPI */ |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 2057 | |