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