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