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