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