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