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