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