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