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