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