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