]>
Commit | Line | Data |
---|---|---|
1 | /* $OpenBSD: monitor.c,v 1.101 2009/02/12 03:26:22 djm Exp $ */ | |
2 | /* | |
3 | * Copyright 2002 Niels Provos <provos@citi.umich.edu> | |
4 | * Copyright 2002 Markus Friedl <markus@openbsd.org> | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 | */ | |
27 | ||
28 | #include "includes.h" | |
29 | ||
30 | #include <sys/types.h> | |
31 | #include <sys/param.h> | |
32 | #include <sys/socket.h> | |
33 | #include "openbsd-compat/sys-tree.h" | |
34 | #include <sys/wait.h> | |
35 | ||
36 | #include <errno.h> | |
37 | #include <fcntl.h> | |
38 | #ifdef HAVE_PATHS_H | |
39 | #include <paths.h> | |
40 | #endif | |
41 | #include <pwd.h> | |
42 | #include <signal.h> | |
43 | #include <stdarg.h> | |
44 | #include <stdlib.h> | |
45 | #include <string.h> | |
46 | #include <unistd.h> | |
47 | ||
48 | #ifdef SKEY | |
49 | #include <skey.h> | |
50 | #endif | |
51 | ||
52 | #include <openssl/dh.h> | |
53 | ||
54 | #include "openbsd-compat/sys-queue.h" | |
55 | #include "xmalloc.h" | |
56 | #include "ssh.h" | |
57 | #include "key.h" | |
58 | #include "buffer.h" | |
59 | #include "hostfile.h" | |
60 | #include "auth.h" | |
61 | #include "cipher.h" | |
62 | #include "kex.h" | |
63 | #include "dh.h" | |
64 | #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */ | |
65 | #undef TARGET_OS_MAC | |
66 | #include "zlib.h" | |
67 | #define TARGET_OS_MAC 1 | |
68 | #else | |
69 | #include "zlib.h" | |
70 | #endif | |
71 | #include "packet.h" | |
72 | #include "auth-options.h" | |
73 | #include "sshpty.h" | |
74 | #include "channels.h" | |
75 | #include "session.h" | |
76 | #include "sshlogin.h" | |
77 | #include "canohost.h" | |
78 | #include "log.h" | |
79 | #include "servconf.h" | |
80 | #include "monitor.h" | |
81 | #include "monitor_mm.h" | |
82 | #ifdef GSSAPI | |
83 | #include "ssh-gss.h" | |
84 | #endif | |
85 | #include "monitor_wrap.h" | |
86 | #include "monitor_fdpass.h" | |
87 | #include "misc.h" | |
88 | #include "compat.h" | |
89 | #include "ssh2.h" | |
90 | #include "jpake.h" | |
91 | ||
92 | #ifdef GSSAPI | |
93 | static Gssctxt *gsscontext = NULL; | |
94 | #endif | |
95 | ||
96 | /* Imports */ | |
97 | extern ServerOptions options; | |
98 | extern u_int utmp_len; | |
99 | extern Newkeys *current_keys[]; | |
100 | extern z_stream incoming_stream; | |
101 | extern z_stream outgoing_stream; | |
102 | extern u_char session_id[]; | |
103 | extern Buffer input, output; | |
104 | extern Buffer auth_debug; | |
105 | extern int auth_debug_init; | |
106 | extern Buffer loginmsg; | |
107 | ||
108 | /* State exported from the child */ | |
109 | ||
110 | struct { | |
111 | z_stream incoming; | |
112 | z_stream outgoing; | |
113 | u_char *keyin; | |
114 | u_int keyinlen; | |
115 | u_char *keyout; | |
116 | u_int keyoutlen; | |
117 | u_char *ivin; | |
118 | u_int ivinlen; | |
119 | u_char *ivout; | |
120 | u_int ivoutlen; | |
121 | u_char *ssh1key; | |
122 | u_int ssh1keylen; | |
123 | int ssh1cipher; | |
124 | int ssh1protoflags; | |
125 | u_char *input; | |
126 | u_int ilen; | |
127 | u_char *output; | |
128 | u_int olen; | |
129 | } child_state; | |
130 | ||
131 | /* Functions on the monitor that answer unprivileged requests */ | |
132 | ||
133 | int mm_answer_moduli(int, Buffer *); | |
134 | int mm_answer_sign(int, Buffer *); | |
135 | int mm_answer_pwnamallow(int, Buffer *); | |
136 | int mm_answer_auth2_read_banner(int, Buffer *); | |
137 | int mm_answer_authserv(int, Buffer *); | |
138 | int mm_answer_authpassword(int, Buffer *); | |
139 | int mm_answer_bsdauthquery(int, Buffer *); | |
140 | int mm_answer_bsdauthrespond(int, Buffer *); | |
141 | int mm_answer_skeyquery(int, Buffer *); | |
142 | int mm_answer_skeyrespond(int, Buffer *); | |
143 | int mm_answer_keyallowed(int, Buffer *); | |
144 | int mm_answer_keyverify(int, Buffer *); | |
145 | int mm_answer_pty(int, Buffer *); | |
146 | int mm_answer_pty_cleanup(int, Buffer *); | |
147 | int mm_answer_term(int, Buffer *); | |
148 | int mm_answer_rsa_keyallowed(int, Buffer *); | |
149 | int mm_answer_rsa_challenge(int, Buffer *); | |
150 | int mm_answer_rsa_response(int, Buffer *); | |
151 | int mm_answer_sesskey(int, Buffer *); | |
152 | int mm_answer_sessid(int, Buffer *); | |
153 | int mm_answer_jpake_get_pwdata(int, Buffer *); | |
154 | int mm_answer_jpake_step1(int, Buffer *); | |
155 | int mm_answer_jpake_step2(int, Buffer *); | |
156 | int mm_answer_jpake_key_confirm(int, Buffer *); | |
157 | int mm_answer_jpake_check_confirm(int, Buffer *); | |
158 | ||
159 | #ifdef USE_PAM | |
160 | int mm_answer_pam_start(int, Buffer *); | |
161 | int mm_answer_pam_account(int, Buffer *); | |
162 | int mm_answer_pam_init_ctx(int, Buffer *); | |
163 | int mm_answer_pam_query(int, Buffer *); | |
164 | int mm_answer_pam_respond(int, Buffer *); | |
165 | int mm_answer_pam_free_ctx(int, Buffer *); | |
166 | #endif | |
167 | ||
168 | #ifdef GSSAPI | |
169 | int mm_answer_gss_setup_ctx(int, Buffer *); | |
170 | int mm_answer_gss_accept_ctx(int, Buffer *); | |
171 | int mm_answer_gss_userok(int, Buffer *); | |
172 | int mm_answer_gss_checkmic(int, Buffer *); | |
173 | int mm_answer_gss_sign(int, Buffer *); | |
174 | int mm_answer_gss_updatecreds(int, Buffer *); | |
175 | #endif | |
176 | ||
177 | #ifdef SSH_AUDIT_EVENTS | |
178 | int mm_answer_audit_event(int, Buffer *); | |
179 | int mm_answer_audit_command(int, Buffer *); | |
180 | #endif | |
181 | ||
182 | static Authctxt *authctxt; | |
183 | static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */ | |
184 | ||
185 | /* local state for key verify */ | |
186 | static u_char *key_blob = NULL; | |
187 | static u_int key_bloblen = 0; | |
188 | static int key_blobtype = MM_NOKEY; | |
189 | static char *hostbased_cuser = NULL; | |
190 | static char *hostbased_chost = NULL; | |
191 | static char *auth_method = "unknown"; | |
192 | static u_int session_id2_len = 0; | |
193 | static u_char *session_id2 = NULL; | |
194 | static pid_t monitor_child_pid; | |
195 | ||
196 | struct mon_table { | |
197 | enum monitor_reqtype type; | |
198 | int flags; | |
199 | int (*f)(int, Buffer *); | |
200 | }; | |
201 | ||
202 | #define MON_ISAUTH 0x0004 /* Required for Authentication */ | |
203 | #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */ | |
204 | #define MON_ONCE 0x0010 /* Disable after calling */ | |
205 | #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */ | |
206 | ||
207 | #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE) | |
208 | ||
209 | #define MON_PERMIT 0x1000 /* Request is permitted */ | |
210 | ||
211 | struct mon_table mon_dispatch_proto20[] = { | |
212 | {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli}, | |
213 | {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign}, | |
214 | {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, | |
215 | {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv}, | |
216 | {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner}, | |
217 | {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, | |
218 | #ifdef USE_PAM | |
219 | {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, | |
220 | {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, | |
221 | {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, | |
222 | {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, | |
223 | {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, | |
224 | {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, | |
225 | #endif | |
226 | #ifdef SSH_AUDIT_EVENTS | |
227 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, | |
228 | #endif | |
229 | #ifdef BSD_AUTH | |
230 | {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, | |
231 | {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, | |
232 | #endif | |
233 | #ifdef SKEY | |
234 | {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, | |
235 | {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, | |
236 | #endif | |
237 | {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, | |
238 | {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify}, | |
239 | #ifdef GSSAPI | |
240 | {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx}, | |
241 | {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx}, | |
242 | {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok}, | |
243 | {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic}, | |
244 | {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign}, | |
245 | #endif | |
246 | #ifdef JPAKE | |
247 | {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata}, | |
248 | {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1}, | |
249 | {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2}, | |
250 | {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm}, | |
251 | {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm}, | |
252 | #endif | |
253 | {0, 0, NULL} | |
254 | }; | |
255 | ||
256 | struct mon_table mon_dispatch_postauth20[] = { | |
257 | #ifdef GSSAPI | |
258 | {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx}, | |
259 | {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx}, | |
260 | {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign}, | |
261 | {MONITOR_REQ_GSSUPCREDS, 0, mm_answer_gss_updatecreds}, | |
262 | #endif | |
263 | {MONITOR_REQ_MODULI, 0, mm_answer_moduli}, | |
264 | {MONITOR_REQ_SIGN, 0, mm_answer_sign}, | |
265 | {MONITOR_REQ_PTY, 0, mm_answer_pty}, | |
266 | {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup}, | |
267 | {MONITOR_REQ_TERM, 0, mm_answer_term}, | |
268 | #ifdef SSH_AUDIT_EVENTS | |
269 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, | |
270 | {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command}, | |
271 | #endif | |
272 | {0, 0, NULL} | |
273 | }; | |
274 | ||
275 | struct mon_table mon_dispatch_proto15[] = { | |
276 | {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, | |
277 | {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey}, | |
278 | {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid}, | |
279 | {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, | |
280 | {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed}, | |
281 | {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed}, | |
282 | {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge}, | |
283 | {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response}, | |
284 | #ifdef BSD_AUTH | |
285 | {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, | |
286 | {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, | |
287 | #endif | |
288 | #ifdef SKEY | |
289 | {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, | |
290 | {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, | |
291 | #endif | |
292 | #ifdef USE_PAM | |
293 | {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, | |
294 | {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, | |
295 | {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, | |
296 | {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, | |
297 | {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, | |
298 | {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, | |
299 | #endif | |
300 | #ifdef SSH_AUDIT_EVENTS | |
301 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, | |
302 | #endif | |
303 | {0, 0, NULL} | |
304 | }; | |
305 | ||
306 | struct mon_table mon_dispatch_postauth15[] = { | |
307 | {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty}, | |
308 | {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup}, | |
309 | {MONITOR_REQ_TERM, 0, mm_answer_term}, | |
310 | #ifdef SSH_AUDIT_EVENTS | |
311 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, | |
312 | {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command}, | |
313 | #endif | |
314 | {0, 0, NULL} | |
315 | }; | |
316 | ||
317 | struct mon_table *mon_dispatch; | |
318 | ||
319 | /* Specifies if a certain message is allowed at the moment */ | |
320 | ||
321 | static void | |
322 | monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit) | |
323 | { | |
324 | while (ent->f != NULL) { | |
325 | if (ent->type == type) { | |
326 | ent->flags &= ~MON_PERMIT; | |
327 | ent->flags |= permit ? MON_PERMIT : 0; | |
328 | return; | |
329 | } | |
330 | ent++; | |
331 | } | |
332 | } | |
333 | ||
334 | static void | |
335 | monitor_permit_authentications(int permit) | |
336 | { | |
337 | struct mon_table *ent = mon_dispatch; | |
338 | ||
339 | while (ent->f != NULL) { | |
340 | if (ent->flags & MON_AUTH) { | |
341 | ent->flags &= ~MON_PERMIT; | |
342 | ent->flags |= permit ? MON_PERMIT : 0; | |
343 | } | |
344 | ent++; | |
345 | } | |
346 | } | |
347 | ||
348 | void | |
349 | monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor) | |
350 | { | |
351 | struct mon_table *ent; | |
352 | int authenticated = 0; | |
353 | ||
354 | debug3("preauth child monitor started"); | |
355 | ||
356 | authctxt = _authctxt; | |
357 | memset(authctxt, 0, sizeof(*authctxt)); | |
358 | ||
359 | authctxt->loginmsg = &loginmsg; | |
360 | ||
361 | if (compat20) { | |
362 | mon_dispatch = mon_dispatch_proto20; | |
363 | ||
364 | /* Permit requests for moduli and signatures */ | |
365 | monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); | |
366 | monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); | |
367 | #ifdef GSSAPI | |
368 | /* and for the GSSAPI key exchange */ | |
369 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1); | |
370 | #endif | |
371 | } else { | |
372 | mon_dispatch = mon_dispatch_proto15; | |
373 | ||
374 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1); | |
375 | } | |
376 | ||
377 | /* The first few requests do not require asynchronous access */ | |
378 | while (!authenticated) { | |
379 | auth_method = "unknown"; | |
380 | authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1); | |
381 | if (authenticated) { | |
382 | if (!(ent->flags & MON_AUTHDECIDE)) | |
383 | fatal("%s: unexpected authentication from %d", | |
384 | __func__, ent->type); | |
385 | if (authctxt->pw->pw_uid == 0 && | |
386 | !auth_root_allowed(auth_method)) | |
387 | authenticated = 0; | |
388 | #ifdef USE_PAM | |
389 | /* PAM needs to perform account checks after auth */ | |
390 | if (options.use_pam && authenticated) { | |
391 | Buffer m; | |
392 | ||
393 | buffer_init(&m); | |
394 | mm_request_receive_expect(pmonitor->m_sendfd, | |
395 | MONITOR_REQ_PAM_ACCOUNT, &m); | |
396 | authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m); | |
397 | buffer_free(&m); | |
398 | } | |
399 | #endif | |
400 | } | |
401 | ||
402 | if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) { | |
403 | auth_log(authctxt, authenticated, auth_method, | |
404 | compat20 ? " ssh2" : ""); | |
405 | if (!authenticated) | |
406 | authctxt->failures++; | |
407 | } | |
408 | #ifdef JPAKE | |
409 | /* Cleanup JPAKE context after authentication */ | |
410 | if (ent->flags & MON_AUTHDECIDE) { | |
411 | if (authctxt->jpake_ctx != NULL) { | |
412 | jpake_free(authctxt->jpake_ctx); | |
413 | authctxt->jpake_ctx = NULL; | |
414 | } | |
415 | } | |
416 | #endif | |
417 | } | |
418 | ||
419 | if (!authctxt->valid) | |
420 | fatal("%s: authenticated invalid user", __func__); | |
421 | if (strcmp(auth_method, "unknown") == 0) | |
422 | fatal("%s: authentication method name unknown", __func__); | |
423 | ||
424 | debug("%s: %s has been authenticated by privileged process", | |
425 | __func__, authctxt->user); | |
426 | ||
427 | mm_get_keystate(pmonitor); | |
428 | } | |
429 | ||
430 | static void | |
431 | monitor_set_child_handler(pid_t pid) | |
432 | { | |
433 | monitor_child_pid = pid; | |
434 | } | |
435 | ||
436 | static void | |
437 | monitor_child_handler(int sig) | |
438 | { | |
439 | kill(monitor_child_pid, sig); | |
440 | } | |
441 | ||
442 | void | |
443 | monitor_child_postauth(struct monitor *pmonitor) | |
444 | { | |
445 | monitor_set_child_handler(pmonitor->m_pid); | |
446 | signal(SIGHUP, &monitor_child_handler); | |
447 | signal(SIGTERM, &monitor_child_handler); | |
448 | signal(SIGINT, &monitor_child_handler); | |
449 | ||
450 | if (compat20) { | |
451 | mon_dispatch = mon_dispatch_postauth20; | |
452 | ||
453 | /* Permit requests for moduli and signatures */ | |
454 | monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); | |
455 | monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); | |
456 | monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); | |
457 | #ifdef GSSAPI | |
458 | /* and for the GSSAPI key exchange */ | |
459 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1); | |
460 | #endif | |
461 | } else { | |
462 | mon_dispatch = mon_dispatch_postauth15; | |
463 | monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); | |
464 | } | |
465 | if (!no_pty_flag) { | |
466 | monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1); | |
467 | monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1); | |
468 | } | |
469 | ||
470 | for (;;) | |
471 | monitor_read(pmonitor, mon_dispatch, NULL); | |
472 | } | |
473 | ||
474 | void | |
475 | monitor_sync(struct monitor *pmonitor) | |
476 | { | |
477 | if (options.compression) { | |
478 | /* The member allocation is not visible, so sync it */ | |
479 | mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback); | |
480 | } | |
481 | } | |
482 | ||
483 | int | |
484 | monitor_read(struct monitor *pmonitor, struct mon_table *ent, | |
485 | struct mon_table **pent) | |
486 | { | |
487 | Buffer m; | |
488 | int ret; | |
489 | u_char type; | |
490 | ||
491 | buffer_init(&m); | |
492 | ||
493 | mm_request_receive(pmonitor->m_sendfd, &m); | |
494 | type = buffer_get_char(&m); | |
495 | ||
496 | debug3("%s: checking request %d", __func__, type); | |
497 | ||
498 | while (ent->f != NULL) { | |
499 | if (ent->type == type) | |
500 | break; | |
501 | ent++; | |
502 | } | |
503 | ||
504 | if (ent->f != NULL) { | |
505 | if (!(ent->flags & MON_PERMIT)) | |
506 | fatal("%s: unpermitted request %d", __func__, | |
507 | type); | |
508 | ret = (*ent->f)(pmonitor->m_sendfd, &m); | |
509 | buffer_free(&m); | |
510 | ||
511 | /* The child may use this request only once, disable it */ | |
512 | if (ent->flags & MON_ONCE) { | |
513 | debug2("%s: %d used once, disabling now", __func__, | |
514 | type); | |
515 | ent->flags &= ~MON_PERMIT; | |
516 | } | |
517 | ||
518 | if (pent != NULL) | |
519 | *pent = ent; | |
520 | ||
521 | return ret; | |
522 | } | |
523 | ||
524 | fatal("%s: unsupported request: %d", __func__, type); | |
525 | ||
526 | /* NOTREACHED */ | |
527 | return (-1); | |
528 | } | |
529 | ||
530 | /* allowed key state */ | |
531 | static int | |
532 | monitor_allowed_key(u_char *blob, u_int bloblen) | |
533 | { | |
534 | /* make sure key is allowed */ | |
535 | if (key_blob == NULL || key_bloblen != bloblen || | |
536 | memcmp(key_blob, blob, key_bloblen)) | |
537 | return (0); | |
538 | return (1); | |
539 | } | |
540 | ||
541 | static void | |
542 | monitor_reset_key_state(void) | |
543 | { | |
544 | /* reset state */ | |
545 | if (key_blob != NULL) | |
546 | xfree(key_blob); | |
547 | if (hostbased_cuser != NULL) | |
548 | xfree(hostbased_cuser); | |
549 | if (hostbased_chost != NULL) | |
550 | xfree(hostbased_chost); | |
551 | key_blob = NULL; | |
552 | key_bloblen = 0; | |
553 | key_blobtype = MM_NOKEY; | |
554 | hostbased_cuser = NULL; | |
555 | hostbased_chost = NULL; | |
556 | } | |
557 | ||
558 | int | |
559 | mm_answer_moduli(int sock, Buffer *m) | |
560 | { | |
561 | DH *dh; | |
562 | int min, want, max; | |
563 | ||
564 | min = buffer_get_int(m); | |
565 | want = buffer_get_int(m); | |
566 | max = buffer_get_int(m); | |
567 | ||
568 | debug3("%s: got parameters: %d %d %d", | |
569 | __func__, min, want, max); | |
570 | /* We need to check here, too, in case the child got corrupted */ | |
571 | if (max < min || want < min || max < want) | |
572 | fatal("%s: bad parameters: %d %d %d", | |
573 | __func__, min, want, max); | |
574 | ||
575 | buffer_clear(m); | |
576 | ||
577 | dh = choose_dh(min, want, max); | |
578 | if (dh == NULL) { | |
579 | buffer_put_char(m, 0); | |
580 | return (0); | |
581 | } else { | |
582 | /* Send first bignum */ | |
583 | buffer_put_char(m, 1); | |
584 | buffer_put_bignum2(m, dh->p); | |
585 | buffer_put_bignum2(m, dh->g); | |
586 | ||
587 | DH_free(dh); | |
588 | } | |
589 | mm_request_send(sock, MONITOR_ANS_MODULI, m); | |
590 | return (0); | |
591 | } | |
592 | ||
593 | int | |
594 | mm_answer_sign(int sock, Buffer *m) | |
595 | { | |
596 | Key *key; | |
597 | u_char *p; | |
598 | u_char *signature; | |
599 | u_int siglen, datlen; | |
600 | int keyid; | |
601 | ||
602 | debug3("%s", __func__); | |
603 | ||
604 | keyid = buffer_get_int(m); | |
605 | p = buffer_get_string(m, &datlen); | |
606 | ||
607 | /* | |
608 | * Supported KEX types will only return SHA1 (20 byte) or | |
609 | * SHA256 (32 byte) hashes | |
610 | */ | |
611 | if (datlen != 20 && datlen != 32) | |
612 | fatal("%s: data length incorrect: %u", __func__, datlen); | |
613 | ||
614 | /* save session id, it will be passed on the first call */ | |
615 | if (session_id2_len == 0) { | |
616 | session_id2_len = datlen; | |
617 | session_id2 = xmalloc(session_id2_len); | |
618 | memcpy(session_id2, p, session_id2_len); | |
619 | } | |
620 | ||
621 | if ((key = get_hostkey_by_index(keyid)) == NULL) | |
622 | fatal("%s: no hostkey from index %d", __func__, keyid); | |
623 | if (key_sign(key, &signature, &siglen, p, datlen) < 0) | |
624 | fatal("%s: key_sign failed", __func__); | |
625 | ||
626 | debug3("%s: signature %p(%u)", __func__, signature, siglen); | |
627 | ||
628 | buffer_clear(m); | |
629 | buffer_put_string(m, signature, siglen); | |
630 | ||
631 | xfree(p); | |
632 | xfree(signature); | |
633 | ||
634 | mm_request_send(sock, MONITOR_ANS_SIGN, m); | |
635 | ||
636 | /* Turn on permissions for getpwnam */ | |
637 | monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); | |
638 | ||
639 | return (0); | |
640 | } | |
641 | ||
642 | /* Retrieves the password entry and also checks if the user is permitted */ | |
643 | ||
644 | int | |
645 | mm_answer_pwnamallow(int sock, Buffer *m) | |
646 | { | |
647 | char *username; | |
648 | struct passwd *pwent; | |
649 | int allowed = 0; | |
650 | ||
651 | debug3("%s", __func__); | |
652 | ||
653 | if (authctxt->attempt++ != 0) | |
654 | fatal("%s: multiple attempts for getpwnam", __func__); | |
655 | ||
656 | username = buffer_get_string(m, NULL); | |
657 | ||
658 | pwent = getpwnamallow(username); | |
659 | ||
660 | authctxt->user = xstrdup(username); | |
661 | setproctitle("%s [priv]", pwent ? username : "unknown"); | |
662 | xfree(username); | |
663 | ||
664 | buffer_clear(m); | |
665 | ||
666 | if (pwent == NULL) { | |
667 | buffer_put_char(m, 0); | |
668 | authctxt->pw = fakepw(); | |
669 | goto out; | |
670 | } | |
671 | ||
672 | allowed = 1; | |
673 | authctxt->pw = pwent; | |
674 | authctxt->valid = 1; | |
675 | ||
676 | buffer_put_char(m, 1); | |
677 | buffer_put_string(m, pwent, sizeof(struct passwd)); | |
678 | buffer_put_cstring(m, pwent->pw_name); | |
679 | buffer_put_cstring(m, "*"); | |
680 | buffer_put_cstring(m, pwent->pw_gecos); | |
681 | #ifdef HAVE_PW_CLASS_IN_PASSWD | |
682 | buffer_put_cstring(m, pwent->pw_class); | |
683 | #endif | |
684 | buffer_put_cstring(m, pwent->pw_dir); | |
685 | buffer_put_cstring(m, pwent->pw_shell); | |
686 | ||
687 | out: | |
688 | buffer_put_string(m, &options, sizeof(options)); | |
689 | if (options.banner != NULL) | |
690 | buffer_put_cstring(m, options.banner); | |
691 | debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed); | |
692 | mm_request_send(sock, MONITOR_ANS_PWNAM, m); | |
693 | ||
694 | /* For SSHv1 allow authentication now */ | |
695 | if (!compat20) | |
696 | monitor_permit_authentications(1); | |
697 | else { | |
698 | /* Allow service/style information on the auth context */ | |
699 | monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1); | |
700 | monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1); | |
701 | } | |
702 | ||
703 | #ifdef USE_PAM | |
704 | if (options.use_pam) | |
705 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1); | |
706 | #endif | |
707 | ||
708 | return (0); | |
709 | } | |
710 | ||
711 | int mm_answer_auth2_read_banner(int sock, Buffer *m) | |
712 | { | |
713 | char *banner; | |
714 | ||
715 | buffer_clear(m); | |
716 | banner = auth2_read_banner(); | |
717 | buffer_put_cstring(m, banner != NULL ? banner : ""); | |
718 | mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m); | |
719 | ||
720 | if (banner != NULL) | |
721 | xfree(banner); | |
722 | ||
723 | return (0); | |
724 | } | |
725 | ||
726 | int | |
727 | mm_answer_authserv(int sock, Buffer *m) | |
728 | { | |
729 | monitor_permit_authentications(1); | |
730 | ||
731 | authctxt->service = buffer_get_string(m, NULL); | |
732 | authctxt->style = buffer_get_string(m, NULL); | |
733 | debug3("%s: service=%s, style=%s", | |
734 | __func__, authctxt->service, authctxt->style); | |
735 | ||
736 | if (strlen(authctxt->style) == 0) { | |
737 | xfree(authctxt->style); | |
738 | authctxt->style = NULL; | |
739 | } | |
740 | ||
741 | return (0); | |
742 | } | |
743 | ||
744 | int | |
745 | mm_answer_authpassword(int sock, Buffer *m) | |
746 | { | |
747 | static int call_count; | |
748 | char *passwd; | |
749 | int authenticated; | |
750 | u_int plen; | |
751 | ||
752 | passwd = buffer_get_string(m, &plen); | |
753 | /* Only authenticate if the context is valid */ | |
754 | authenticated = options.password_authentication && | |
755 | auth_password(authctxt, passwd); | |
756 | memset(passwd, 0, strlen(passwd)); | |
757 | xfree(passwd); | |
758 | ||
759 | buffer_clear(m); | |
760 | buffer_put_int(m, authenticated); | |
761 | ||
762 | debug3("%s: sending result %d", __func__, authenticated); | |
763 | mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m); | |
764 | ||
765 | call_count++; | |
766 | if (plen == 0 && call_count == 1) | |
767 | auth_method = "none"; | |
768 | else | |
769 | auth_method = "password"; | |
770 | ||
771 | /* Causes monitor loop to terminate if authenticated */ | |
772 | return (authenticated); | |
773 | } | |
774 | ||
775 | #ifdef BSD_AUTH | |
776 | int | |
777 | mm_answer_bsdauthquery(int sock, Buffer *m) | |
778 | { | |
779 | char *name, *infotxt; | |
780 | u_int numprompts; | |
781 | u_int *echo_on; | |
782 | char **prompts; | |
783 | u_int success; | |
784 | ||
785 | success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, | |
786 | &prompts, &echo_on) < 0 ? 0 : 1; | |
787 | ||
788 | buffer_clear(m); | |
789 | buffer_put_int(m, success); | |
790 | if (success) | |
791 | buffer_put_cstring(m, prompts[0]); | |
792 | ||
793 | debug3("%s: sending challenge success: %u", __func__, success); | |
794 | mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m); | |
795 | ||
796 | if (success) { | |
797 | xfree(name); | |
798 | xfree(infotxt); | |
799 | xfree(prompts); | |
800 | xfree(echo_on); | |
801 | } | |
802 | ||
803 | return (0); | |
804 | } | |
805 | ||
806 | int | |
807 | mm_answer_bsdauthrespond(int sock, Buffer *m) | |
808 | { | |
809 | char *response; | |
810 | int authok; | |
811 | ||
812 | if (authctxt->as == 0) | |
813 | fatal("%s: no bsd auth session", __func__); | |
814 | ||
815 | response = buffer_get_string(m, NULL); | |
816 | authok = options.challenge_response_authentication && | |
817 | auth_userresponse(authctxt->as, response, 0); | |
818 | authctxt->as = NULL; | |
819 | debug3("%s: <%s> = <%d>", __func__, response, authok); | |
820 | xfree(response); | |
821 | ||
822 | buffer_clear(m); | |
823 | buffer_put_int(m, authok); | |
824 | ||
825 | debug3("%s: sending authenticated: %d", __func__, authok); | |
826 | mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m); | |
827 | ||
828 | auth_method = "bsdauth"; | |
829 | ||
830 | return (authok != 0); | |
831 | } | |
832 | #endif | |
833 | ||
834 | #ifdef SKEY | |
835 | int | |
836 | mm_answer_skeyquery(int sock, Buffer *m) | |
837 | { | |
838 | struct skey skey; | |
839 | char challenge[1024]; | |
840 | u_int success; | |
841 | ||
842 | success = _compat_skeychallenge(&skey, authctxt->user, challenge, | |
843 | sizeof(challenge)) < 0 ? 0 : 1; | |
844 | ||
845 | buffer_clear(m); | |
846 | buffer_put_int(m, success); | |
847 | if (success) | |
848 | buffer_put_cstring(m, challenge); | |
849 | ||
850 | debug3("%s: sending challenge success: %u", __func__, success); | |
851 | mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m); | |
852 | ||
853 | return (0); | |
854 | } | |
855 | ||
856 | int | |
857 | mm_answer_skeyrespond(int sock, Buffer *m) | |
858 | { | |
859 | char *response; | |
860 | int authok; | |
861 | ||
862 | response = buffer_get_string(m, NULL); | |
863 | ||
864 | authok = (options.challenge_response_authentication && | |
865 | authctxt->valid && | |
866 | skey_haskey(authctxt->pw->pw_name) == 0 && | |
867 | skey_passcheck(authctxt->pw->pw_name, response) != -1); | |
868 | ||
869 | xfree(response); | |
870 | ||
871 | buffer_clear(m); | |
872 | buffer_put_int(m, authok); | |
873 | ||
874 | debug3("%s: sending authenticated: %d", __func__, authok); | |
875 | mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m); | |
876 | ||
877 | auth_method = "skey"; | |
878 | ||
879 | return (authok != 0); | |
880 | } | |
881 | #endif | |
882 | ||
883 | #ifdef USE_PAM | |
884 | int | |
885 | mm_answer_pam_start(int sock, Buffer *m) | |
886 | { | |
887 | if (!options.use_pam) | |
888 | fatal("UsePAM not set, but ended up in %s anyway", __func__); | |
889 | ||
890 | start_pam(authctxt); | |
891 | ||
892 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1); | |
893 | ||
894 | return (0); | |
895 | } | |
896 | ||
897 | int | |
898 | mm_answer_pam_account(int sock, Buffer *m) | |
899 | { | |
900 | u_int ret; | |
901 | ||
902 | if (!options.use_pam) | |
903 | fatal("UsePAM not set, but ended up in %s anyway", __func__); | |
904 | ||
905 | ret = do_pam_account(); | |
906 | ||
907 | buffer_put_int(m, ret); | |
908 | buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); | |
909 | ||
910 | mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m); | |
911 | ||
912 | return (ret); | |
913 | } | |
914 | ||
915 | static void *sshpam_ctxt, *sshpam_authok; | |
916 | extern KbdintDevice sshpam_device; | |
917 | ||
918 | int | |
919 | mm_answer_pam_init_ctx(int sock, Buffer *m) | |
920 | { | |
921 | ||
922 | debug3("%s", __func__); | |
923 | authctxt->user = buffer_get_string(m, NULL); | |
924 | sshpam_ctxt = (sshpam_device.init_ctx)(authctxt); | |
925 | sshpam_authok = NULL; | |
926 | buffer_clear(m); | |
927 | if (sshpam_ctxt != NULL) { | |
928 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1); | |
929 | buffer_put_int(m, 1); | |
930 | } else { | |
931 | buffer_put_int(m, 0); | |
932 | } | |
933 | mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m); | |
934 | return (0); | |
935 | } | |
936 | ||
937 | int | |
938 | mm_answer_pam_query(int sock, Buffer *m) | |
939 | { | |
940 | char *name, *info, **prompts; | |
941 | u_int i, num, *echo_on; | |
942 | int ret; | |
943 | ||
944 | debug3("%s", __func__); | |
945 | sshpam_authok = NULL; | |
946 | ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on); | |
947 | if (ret == 0 && num == 0) | |
948 | sshpam_authok = sshpam_ctxt; | |
949 | if (num > 1 || name == NULL || info == NULL) | |
950 | ret = -1; | |
951 | buffer_clear(m); | |
952 | buffer_put_int(m, ret); | |
953 | buffer_put_cstring(m, name); | |
954 | xfree(name); | |
955 | buffer_put_cstring(m, info); | |
956 | xfree(info); | |
957 | buffer_put_int(m, num); | |
958 | for (i = 0; i < num; ++i) { | |
959 | buffer_put_cstring(m, prompts[i]); | |
960 | xfree(prompts[i]); | |
961 | buffer_put_int(m, echo_on[i]); | |
962 | } | |
963 | if (prompts != NULL) | |
964 | xfree(prompts); | |
965 | if (echo_on != NULL) | |
966 | xfree(echo_on); | |
967 | auth_method = "keyboard-interactive/pam"; | |
968 | mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m); | |
969 | return (0); | |
970 | } | |
971 | ||
972 | int | |
973 | mm_answer_pam_respond(int sock, Buffer *m) | |
974 | { | |
975 | char **resp; | |
976 | u_int i, num; | |
977 | int ret; | |
978 | ||
979 | debug3("%s", __func__); | |
980 | sshpam_authok = NULL; | |
981 | num = buffer_get_int(m); | |
982 | if (num > 0) { | |
983 | resp = xcalloc(num, sizeof(char *)); | |
984 | for (i = 0; i < num; ++i) | |
985 | resp[i] = buffer_get_string(m, NULL); | |
986 | ret = (sshpam_device.respond)(sshpam_ctxt, num, resp); | |
987 | for (i = 0; i < num; ++i) | |
988 | xfree(resp[i]); | |
989 | xfree(resp); | |
990 | } else { | |
991 | ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL); | |
992 | } | |
993 | buffer_clear(m); | |
994 | buffer_put_int(m, ret); | |
995 | mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m); | |
996 | auth_method = "keyboard-interactive/pam"; | |
997 | if (ret == 0) | |
998 | sshpam_authok = sshpam_ctxt; | |
999 | return (0); | |
1000 | } | |
1001 | ||
1002 | int | |
1003 | mm_answer_pam_free_ctx(int sock, Buffer *m) | |
1004 | { | |
1005 | ||
1006 | debug3("%s", __func__); | |
1007 | (sshpam_device.free_ctx)(sshpam_ctxt); | |
1008 | buffer_clear(m); | |
1009 | mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m); | |
1010 | auth_method = "keyboard-interactive/pam"; | |
1011 | return (sshpam_authok == sshpam_ctxt); | |
1012 | } | |
1013 | #endif | |
1014 | ||
1015 | static void | |
1016 | mm_append_debug(Buffer *m) | |
1017 | { | |
1018 | if (auth_debug_init && buffer_len(&auth_debug)) { | |
1019 | debug3("%s: Appending debug messages for child", __func__); | |
1020 | buffer_append(m, buffer_ptr(&auth_debug), | |
1021 | buffer_len(&auth_debug)); | |
1022 | buffer_clear(&auth_debug); | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | int | |
1027 | mm_answer_keyallowed(int sock, Buffer *m) | |
1028 | { | |
1029 | Key *key; | |
1030 | char *cuser, *chost; | |
1031 | u_char *blob; | |
1032 | u_int bloblen; | |
1033 | enum mm_keytype type = 0; | |
1034 | int allowed = 0; | |
1035 | ||
1036 | debug3("%s entering", __func__); | |
1037 | ||
1038 | type = buffer_get_int(m); | |
1039 | cuser = buffer_get_string(m, NULL); | |
1040 | chost = buffer_get_string(m, NULL); | |
1041 | blob = buffer_get_string(m, &bloblen); | |
1042 | ||
1043 | key = key_from_blob(blob, bloblen); | |
1044 | ||
1045 | if ((compat20 && type == MM_RSAHOSTKEY) || | |
1046 | (!compat20 && type != MM_RSAHOSTKEY)) | |
1047 | fatal("%s: key type and protocol mismatch", __func__); | |
1048 | ||
1049 | debug3("%s: key_from_blob: %p", __func__, key); | |
1050 | ||
1051 | if (key != NULL && authctxt->valid) { | |
1052 | switch (type) { | |
1053 | case MM_USERKEY: | |
1054 | allowed = options.pubkey_authentication && | |
1055 | user_key_allowed(authctxt->pw, key); | |
1056 | auth_method = "publickey"; | |
1057 | if (options.pubkey_authentication && allowed != 1) | |
1058 | auth_clear_options(); | |
1059 | break; | |
1060 | case MM_HOSTKEY: | |
1061 | allowed = options.hostbased_authentication && | |
1062 | hostbased_key_allowed(authctxt->pw, | |
1063 | cuser, chost, key); | |
1064 | auth_method = "hostbased"; | |
1065 | break; | |
1066 | case MM_RSAHOSTKEY: | |
1067 | key->type = KEY_RSA1; /* XXX */ | |
1068 | allowed = options.rhosts_rsa_authentication && | |
1069 | auth_rhosts_rsa_key_allowed(authctxt->pw, | |
1070 | cuser, chost, key); | |
1071 | if (options.rhosts_rsa_authentication && allowed != 1) | |
1072 | auth_clear_options(); | |
1073 | auth_method = "rsa"; | |
1074 | break; | |
1075 | default: | |
1076 | fatal("%s: unknown key type %d", __func__, type); | |
1077 | break; | |
1078 | } | |
1079 | } | |
1080 | if (key != NULL) | |
1081 | key_free(key); | |
1082 | ||
1083 | /* clear temporarily storage (used by verify) */ | |
1084 | monitor_reset_key_state(); | |
1085 | ||
1086 | if (allowed) { | |
1087 | /* Save temporarily for comparison in verify */ | |
1088 | key_blob = blob; | |
1089 | key_bloblen = bloblen; | |
1090 | key_blobtype = type; | |
1091 | hostbased_cuser = cuser; | |
1092 | hostbased_chost = chost; | |
1093 | } else { | |
1094 | /* Log failed attempt */ | |
1095 | auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : ""); | |
1096 | xfree(blob); | |
1097 | xfree(cuser); | |
1098 | xfree(chost); | |
1099 | } | |
1100 | ||
1101 | debug3("%s: key %p is %s", | |
1102 | __func__, key, allowed ? "allowed" : "not allowed"); | |
1103 | ||
1104 | buffer_clear(m); | |
1105 | buffer_put_int(m, allowed); | |
1106 | buffer_put_int(m, forced_command != NULL); | |
1107 | ||
1108 | mm_append_debug(m); | |
1109 | ||
1110 | mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m); | |
1111 | ||
1112 | if (type == MM_RSAHOSTKEY) | |
1113 | monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); | |
1114 | ||
1115 | return (0); | |
1116 | } | |
1117 | ||
1118 | static int | |
1119 | monitor_valid_userblob(u_char *data, u_int datalen) | |
1120 | { | |
1121 | Buffer b; | |
1122 | char *p; | |
1123 | u_int len; | |
1124 | int fail = 0; | |
1125 | ||
1126 | buffer_init(&b); | |
1127 | buffer_append(&b, data, datalen); | |
1128 | ||
1129 | if (datafellows & SSH_OLD_SESSIONID) { | |
1130 | p = buffer_ptr(&b); | |
1131 | len = buffer_len(&b); | |
1132 | if ((session_id2 == NULL) || | |
1133 | (len < session_id2_len) || | |
1134 | (memcmp(p, session_id2, session_id2_len) != 0)) | |
1135 | fail++; | |
1136 | buffer_consume(&b, session_id2_len); | |
1137 | } else { | |
1138 | p = buffer_get_string(&b, &len); | |
1139 | if ((session_id2 == NULL) || | |
1140 | (len != session_id2_len) || | |
1141 | (memcmp(p, session_id2, session_id2_len) != 0)) | |
1142 | fail++; | |
1143 | xfree(p); | |
1144 | } | |
1145 | if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) | |
1146 | fail++; | |
1147 | p = buffer_get_string(&b, NULL); | |
1148 | if (strcmp(authctxt->user, p) != 0) { | |
1149 | logit("wrong user name passed to monitor: expected %s != %.100s", | |
1150 | authctxt->user, p); | |
1151 | fail++; | |
1152 | } | |
1153 | xfree(p); | |
1154 | buffer_skip_string(&b); | |
1155 | if (datafellows & SSH_BUG_PKAUTH) { | |
1156 | if (!buffer_get_char(&b)) | |
1157 | fail++; | |
1158 | } else { | |
1159 | p = buffer_get_string(&b, NULL); | |
1160 | if (strcmp("publickey", p) != 0) | |
1161 | fail++; | |
1162 | xfree(p); | |
1163 | if (!buffer_get_char(&b)) | |
1164 | fail++; | |
1165 | buffer_skip_string(&b); | |
1166 | } | |
1167 | buffer_skip_string(&b); | |
1168 | if (buffer_len(&b) != 0) | |
1169 | fail++; | |
1170 | buffer_free(&b); | |
1171 | return (fail == 0); | |
1172 | } | |
1173 | ||
1174 | static int | |
1175 | monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, | |
1176 | char *chost) | |
1177 | { | |
1178 | Buffer b; | |
1179 | char *p; | |
1180 | u_int len; | |
1181 | int fail = 0; | |
1182 | ||
1183 | buffer_init(&b); | |
1184 | buffer_append(&b, data, datalen); | |
1185 | ||
1186 | p = buffer_get_string(&b, &len); | |
1187 | if ((session_id2 == NULL) || | |
1188 | (len != session_id2_len) || | |
1189 | (memcmp(p, session_id2, session_id2_len) != 0)) | |
1190 | fail++; | |
1191 | xfree(p); | |
1192 | ||
1193 | if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) | |
1194 | fail++; | |
1195 | p = buffer_get_string(&b, NULL); | |
1196 | if (strcmp(authctxt->user, p) != 0) { | |
1197 | logit("wrong user name passed to monitor: expected %s != %.100s", | |
1198 | authctxt->user, p); | |
1199 | fail++; | |
1200 | } | |
1201 | xfree(p); | |
1202 | buffer_skip_string(&b); /* service */ | |
1203 | p = buffer_get_string(&b, NULL); | |
1204 | if (strcmp(p, "hostbased") != 0) | |
1205 | fail++; | |
1206 | xfree(p); | |
1207 | buffer_skip_string(&b); /* pkalg */ | |
1208 | buffer_skip_string(&b); /* pkblob */ | |
1209 | ||
1210 | /* verify client host, strip trailing dot if necessary */ | |
1211 | p = buffer_get_string(&b, NULL); | |
1212 | if (((len = strlen(p)) > 0) && p[len - 1] == '.') | |
1213 | p[len - 1] = '\0'; | |
1214 | if (strcmp(p, chost) != 0) | |
1215 | fail++; | |
1216 | xfree(p); | |
1217 | ||
1218 | /* verify client user */ | |
1219 | p = buffer_get_string(&b, NULL); | |
1220 | if (strcmp(p, cuser) != 0) | |
1221 | fail++; | |
1222 | xfree(p); | |
1223 | ||
1224 | if (buffer_len(&b) != 0) | |
1225 | fail++; | |
1226 | buffer_free(&b); | |
1227 | return (fail == 0); | |
1228 | } | |
1229 | ||
1230 | int | |
1231 | mm_answer_keyverify(int sock, Buffer *m) | |
1232 | { | |
1233 | Key *key; | |
1234 | u_char *signature, *data, *blob; | |
1235 | u_int signaturelen, datalen, bloblen; | |
1236 | int verified = 0; | |
1237 | int valid_data = 0; | |
1238 | ||
1239 | blob = buffer_get_string(m, &bloblen); | |
1240 | signature = buffer_get_string(m, &signaturelen); | |
1241 | data = buffer_get_string(m, &datalen); | |
1242 | ||
1243 | if (hostbased_cuser == NULL || hostbased_chost == NULL || | |
1244 | !monitor_allowed_key(blob, bloblen)) | |
1245 | fatal("%s: bad key, not previously allowed", __func__); | |
1246 | ||
1247 | key = key_from_blob(blob, bloblen); | |
1248 | if (key == NULL) | |
1249 | fatal("%s: bad public key blob", __func__); | |
1250 | ||
1251 | switch (key_blobtype) { | |
1252 | case MM_USERKEY: | |
1253 | valid_data = monitor_valid_userblob(data, datalen); | |
1254 | break; | |
1255 | case MM_HOSTKEY: | |
1256 | valid_data = monitor_valid_hostbasedblob(data, datalen, | |
1257 | hostbased_cuser, hostbased_chost); | |
1258 | break; | |
1259 | default: | |
1260 | valid_data = 0; | |
1261 | break; | |
1262 | } | |
1263 | if (!valid_data) | |
1264 | fatal("%s: bad signature data blob", __func__); | |
1265 | ||
1266 | verified = key_verify(key, signature, signaturelen, data, datalen); | |
1267 | debug3("%s: key %p signature %s", | |
1268 | __func__, key, (verified == 1) ? "verified" : "unverified"); | |
1269 | ||
1270 | key_free(key); | |
1271 | xfree(blob); | |
1272 | xfree(signature); | |
1273 | xfree(data); | |
1274 | ||
1275 | auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased"; | |
1276 | ||
1277 | monitor_reset_key_state(); | |
1278 | ||
1279 | buffer_clear(m); | |
1280 | buffer_put_int(m, verified); | |
1281 | mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m); | |
1282 | ||
1283 | return (verified == 1); | |
1284 | } | |
1285 | ||
1286 | static void | |
1287 | mm_record_login(Session *s, struct passwd *pw) | |
1288 | { | |
1289 | socklen_t fromlen; | |
1290 | struct sockaddr_storage from; | |
1291 | ||
1292 | /* | |
1293 | * Get IP address of client. If the connection is not a socket, let | |
1294 | * the address be 0.0.0.0. | |
1295 | */ | |
1296 | memset(&from, 0, sizeof(from)); | |
1297 | fromlen = sizeof(from); | |
1298 | if (packet_connection_is_on_socket()) { | |
1299 | if (getpeername(packet_get_connection_in(), | |
1300 | (struct sockaddr *)&from, &fromlen) < 0) { | |
1301 | debug("getpeername: %.100s", strerror(errno)); | |
1302 | cleanup_exit(255); | |
1303 | } | |
1304 | } | |
1305 | /* Record that there was a login on that tty from the remote host. */ | |
1306 | record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, | |
1307 | get_remote_name_or_ip(utmp_len, options.use_dns), | |
1308 | (struct sockaddr *)&from, fromlen); | |
1309 | } | |
1310 | ||
1311 | static void | |
1312 | mm_session_close(Session *s) | |
1313 | { | |
1314 | debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); | |
1315 | if (s->ttyfd != -1) { | |
1316 | debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); | |
1317 | session_pty_cleanup2(s); | |
1318 | } | |
1319 | session_unused(s->self); | |
1320 | } | |
1321 | ||
1322 | int | |
1323 | mm_answer_pty(int sock, Buffer *m) | |
1324 | { | |
1325 | extern struct monitor *pmonitor; | |
1326 | Session *s; | |
1327 | int res, fd0; | |
1328 | ||
1329 | debug3("%s entering", __func__); | |
1330 | ||
1331 | buffer_clear(m); | |
1332 | s = session_new(); | |
1333 | if (s == NULL) | |
1334 | goto error; | |
1335 | s->authctxt = authctxt; | |
1336 | s->pw = authctxt->pw; | |
1337 | s->pid = pmonitor->m_pid; | |
1338 | res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); | |
1339 | if (res == 0) | |
1340 | goto error; | |
1341 | pty_setowner(authctxt->pw, s->tty); | |
1342 | ||
1343 | buffer_put_int(m, 1); | |
1344 | buffer_put_cstring(m, s->tty); | |
1345 | ||
1346 | /* We need to trick ttyslot */ | |
1347 | if (dup2(s->ttyfd, 0) == -1) | |
1348 | fatal("%s: dup2", __func__); | |
1349 | ||
1350 | mm_record_login(s, authctxt->pw); | |
1351 | ||
1352 | /* Now we can close the file descriptor again */ | |
1353 | close(0); | |
1354 | ||
1355 | /* send messages generated by record_login */ | |
1356 | buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); | |
1357 | buffer_clear(&loginmsg); | |
1358 | ||
1359 | mm_request_send(sock, MONITOR_ANS_PTY, m); | |
1360 | ||
1361 | if (mm_send_fd(sock, s->ptyfd) == -1 || | |
1362 | mm_send_fd(sock, s->ttyfd) == -1) | |
1363 | fatal("%s: send fds failed", __func__); | |
1364 | ||
1365 | /* make sure nothing uses fd 0 */ | |
1366 | if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) | |
1367 | fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); | |
1368 | if (fd0 != 0) | |
1369 | error("%s: fd0 %d != 0", __func__, fd0); | |
1370 | ||
1371 | /* slave is not needed */ | |
1372 | close(s->ttyfd); | |
1373 | s->ttyfd = s->ptyfd; | |
1374 | /* no need to dup() because nobody closes ptyfd */ | |
1375 | s->ptymaster = s->ptyfd; | |
1376 | ||
1377 | debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); | |
1378 | ||
1379 | return (0); | |
1380 | ||
1381 | error: | |
1382 | if (s != NULL) | |
1383 | mm_session_close(s); | |
1384 | buffer_put_int(m, 0); | |
1385 | mm_request_send(sock, MONITOR_ANS_PTY, m); | |
1386 | return (0); | |
1387 | } | |
1388 | ||
1389 | int | |
1390 | mm_answer_pty_cleanup(int sock, Buffer *m) | |
1391 | { | |
1392 | Session *s; | |
1393 | char *tty; | |
1394 | ||
1395 | debug3("%s entering", __func__); | |
1396 | ||
1397 | tty = buffer_get_string(m, NULL); | |
1398 | if ((s = session_by_tty(tty)) != NULL) | |
1399 | mm_session_close(s); | |
1400 | buffer_clear(m); | |
1401 | xfree(tty); | |
1402 | return (0); | |
1403 | } | |
1404 | ||
1405 | int | |
1406 | mm_answer_sesskey(int sock, Buffer *m) | |
1407 | { | |
1408 | BIGNUM *p; | |
1409 | int rsafail; | |
1410 | ||
1411 | /* Turn off permissions */ | |
1412 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0); | |
1413 | ||
1414 | if ((p = BN_new()) == NULL) | |
1415 | fatal("%s: BN_new", __func__); | |
1416 | ||
1417 | buffer_get_bignum2(m, p); | |
1418 | ||
1419 | rsafail = ssh1_session_key(p); | |
1420 | ||
1421 | buffer_clear(m); | |
1422 | buffer_put_int(m, rsafail); | |
1423 | buffer_put_bignum2(m, p); | |
1424 | ||
1425 | BN_clear_free(p); | |
1426 | ||
1427 | mm_request_send(sock, MONITOR_ANS_SESSKEY, m); | |
1428 | ||
1429 | /* Turn on permissions for sessid passing */ | |
1430 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1); | |
1431 | ||
1432 | return (0); | |
1433 | } | |
1434 | ||
1435 | int | |
1436 | mm_answer_sessid(int sock, Buffer *m) | |
1437 | { | |
1438 | int i; | |
1439 | ||
1440 | debug3("%s entering", __func__); | |
1441 | ||
1442 | if (buffer_len(m) != 16) | |
1443 | fatal("%s: bad ssh1 session id", __func__); | |
1444 | for (i = 0; i < 16; i++) | |
1445 | session_id[i] = buffer_get_char(m); | |
1446 | ||
1447 | /* Turn on permissions for getpwnam */ | |
1448 | monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); | |
1449 | ||
1450 | return (0); | |
1451 | } | |
1452 | ||
1453 | int | |
1454 | mm_answer_rsa_keyallowed(int sock, Buffer *m) | |
1455 | { | |
1456 | BIGNUM *client_n; | |
1457 | Key *key = NULL; | |
1458 | u_char *blob = NULL; | |
1459 | u_int blen = 0; | |
1460 | int allowed = 0; | |
1461 | ||
1462 | debug3("%s entering", __func__); | |
1463 | ||
1464 | auth_method = "rsa"; | |
1465 | if (options.rsa_authentication && authctxt->valid) { | |
1466 | if ((client_n = BN_new()) == NULL) | |
1467 | fatal("%s: BN_new", __func__); | |
1468 | buffer_get_bignum2(m, client_n); | |
1469 | allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key); | |
1470 | BN_clear_free(client_n); | |
1471 | } | |
1472 | buffer_clear(m); | |
1473 | buffer_put_int(m, allowed); | |
1474 | buffer_put_int(m, forced_command != NULL); | |
1475 | ||
1476 | /* clear temporarily storage (used by generate challenge) */ | |
1477 | monitor_reset_key_state(); | |
1478 | ||
1479 | if (allowed && key != NULL) { | |
1480 | key->type = KEY_RSA; /* cheat for key_to_blob */ | |
1481 | if (key_to_blob(key, &blob, &blen) == 0) | |
1482 | fatal("%s: key_to_blob failed", __func__); | |
1483 | buffer_put_string(m, blob, blen); | |
1484 | ||
1485 | /* Save temporarily for comparison in verify */ | |
1486 | key_blob = blob; | |
1487 | key_bloblen = blen; | |
1488 | key_blobtype = MM_RSAUSERKEY; | |
1489 | } | |
1490 | if (key != NULL) | |
1491 | key_free(key); | |
1492 | ||
1493 | mm_append_debug(m); | |
1494 | ||
1495 | mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m); | |
1496 | ||
1497 | monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); | |
1498 | monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0); | |
1499 | return (0); | |
1500 | } | |
1501 | ||
1502 | int | |
1503 | mm_answer_rsa_challenge(int sock, Buffer *m) | |
1504 | { | |
1505 | Key *key = NULL; | |
1506 | u_char *blob; | |
1507 | u_int blen; | |
1508 | ||
1509 | debug3("%s entering", __func__); | |
1510 | ||
1511 | if (!authctxt->valid) | |
1512 | fatal("%s: authctxt not valid", __func__); | |
1513 | blob = buffer_get_string(m, &blen); | |
1514 | if (!monitor_allowed_key(blob, blen)) | |
1515 | fatal("%s: bad key, not previously allowed", __func__); | |
1516 | if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) | |
1517 | fatal("%s: key type mismatch", __func__); | |
1518 | if ((key = key_from_blob(blob, blen)) == NULL) | |
1519 | fatal("%s: received bad key", __func__); | |
1520 | if (key->type != KEY_RSA) | |
1521 | fatal("%s: received bad key type %d", __func__, key->type); | |
1522 | key->type = KEY_RSA1; | |
1523 | if (ssh1_challenge) | |
1524 | BN_clear_free(ssh1_challenge); | |
1525 | ssh1_challenge = auth_rsa_generate_challenge(key); | |
1526 | ||
1527 | buffer_clear(m); | |
1528 | buffer_put_bignum2(m, ssh1_challenge); | |
1529 | ||
1530 | debug3("%s sending reply", __func__); | |
1531 | mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m); | |
1532 | ||
1533 | monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1); | |
1534 | ||
1535 | xfree(blob); | |
1536 | key_free(key); | |
1537 | return (0); | |
1538 | } | |
1539 | ||
1540 | int | |
1541 | mm_answer_rsa_response(int sock, Buffer *m) | |
1542 | { | |
1543 | Key *key = NULL; | |
1544 | u_char *blob, *response; | |
1545 | u_int blen, len; | |
1546 | int success; | |
1547 | ||
1548 | debug3("%s entering", __func__); | |
1549 | ||
1550 | if (!authctxt->valid) | |
1551 | fatal("%s: authctxt not valid", __func__); | |
1552 | if (ssh1_challenge == NULL) | |
1553 | fatal("%s: no ssh1_challenge", __func__); | |
1554 | ||
1555 | blob = buffer_get_string(m, &blen); | |
1556 | if (!monitor_allowed_key(blob, blen)) | |
1557 | fatal("%s: bad key, not previously allowed", __func__); | |
1558 | if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) | |
1559 | fatal("%s: key type mismatch: %d", __func__, key_blobtype); | |
1560 | if ((key = key_from_blob(blob, blen)) == NULL) | |
1561 | fatal("%s: received bad key", __func__); | |
1562 | response = buffer_get_string(m, &len); | |
1563 | if (len != 16) | |
1564 | fatal("%s: received bad response to challenge", __func__); | |
1565 | success = auth_rsa_verify_response(key, ssh1_challenge, response); | |
1566 | ||
1567 | xfree(blob); | |
1568 | key_free(key); | |
1569 | xfree(response); | |
1570 | ||
1571 | auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa"; | |
1572 | ||
1573 | /* reset state */ | |
1574 | BN_clear_free(ssh1_challenge); | |
1575 | ssh1_challenge = NULL; | |
1576 | monitor_reset_key_state(); | |
1577 | ||
1578 | buffer_clear(m); | |
1579 | buffer_put_int(m, success); | |
1580 | mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m); | |
1581 | ||
1582 | return (success); | |
1583 | } | |
1584 | ||
1585 | int | |
1586 | mm_answer_term(int sock, Buffer *req) | |
1587 | { | |
1588 | extern struct monitor *pmonitor; | |
1589 | int res, status; | |
1590 | ||
1591 | debug3("%s: tearing down sessions", __func__); | |
1592 | ||
1593 | /* The child is terminating */ | |
1594 | session_destroy_all(&mm_session_close); | |
1595 | ||
1596 | #ifdef USE_PAM | |
1597 | if (options.use_pam) | |
1598 | sshpam_cleanup(); | |
1599 | #endif | |
1600 | ||
1601 | while (waitpid(pmonitor->m_pid, &status, 0) == -1) | |
1602 | if (errno != EINTR) | |
1603 | exit(1); | |
1604 | ||
1605 | res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; | |
1606 | ||
1607 | /* Terminate process */ | |
1608 | exit(res); | |
1609 | } | |
1610 | ||
1611 | #ifdef SSH_AUDIT_EVENTS | |
1612 | /* Report that an audit event occurred */ | |
1613 | int | |
1614 | mm_answer_audit_event(int socket, Buffer *m) | |
1615 | { | |
1616 | ssh_audit_event_t event; | |
1617 | ||
1618 | debug3("%s entering", __func__); | |
1619 | ||
1620 | event = buffer_get_int(m); | |
1621 | switch(event) { | |
1622 | case SSH_AUTH_FAIL_PUBKEY: | |
1623 | case SSH_AUTH_FAIL_HOSTBASED: | |
1624 | case SSH_AUTH_FAIL_GSSAPI: | |
1625 | case SSH_LOGIN_EXCEED_MAXTRIES: | |
1626 | case SSH_LOGIN_ROOT_DENIED: | |
1627 | case SSH_CONNECTION_CLOSE: | |
1628 | case SSH_INVALID_USER: | |
1629 | audit_event(event); | |
1630 | break; | |
1631 | default: | |
1632 | fatal("Audit event type %d not permitted", event); | |
1633 | } | |
1634 | ||
1635 | return (0); | |
1636 | } | |
1637 | ||
1638 | int | |
1639 | mm_answer_audit_command(int socket, Buffer *m) | |
1640 | { | |
1641 | u_int len; | |
1642 | char *cmd; | |
1643 | ||
1644 | debug3("%s entering", __func__); | |
1645 | cmd = buffer_get_string(m, &len); | |
1646 | /* sanity check command, if so how? */ | |
1647 | audit_run_command(cmd); | |
1648 | xfree(cmd); | |
1649 | return (0); | |
1650 | } | |
1651 | #endif /* SSH_AUDIT_EVENTS */ | |
1652 | ||
1653 | void | |
1654 | monitor_apply_keystate(struct monitor *pmonitor) | |
1655 | { | |
1656 | if (compat20) { | |
1657 | set_newkeys(MODE_IN); | |
1658 | set_newkeys(MODE_OUT); | |
1659 | } else { | |
1660 | packet_set_protocol_flags(child_state.ssh1protoflags); | |
1661 | packet_set_encryption_key(child_state.ssh1key, | |
1662 | child_state.ssh1keylen, child_state.ssh1cipher); | |
1663 | xfree(child_state.ssh1key); | |
1664 | } | |
1665 | ||
1666 | /* for rc4 and other stateful ciphers */ | |
1667 | packet_set_keycontext(MODE_OUT, child_state.keyout); | |
1668 | xfree(child_state.keyout); | |
1669 | packet_set_keycontext(MODE_IN, child_state.keyin); | |
1670 | xfree(child_state.keyin); | |
1671 | ||
1672 | if (!compat20) { | |
1673 | packet_set_iv(MODE_OUT, child_state.ivout); | |
1674 | xfree(child_state.ivout); | |
1675 | packet_set_iv(MODE_IN, child_state.ivin); | |
1676 | xfree(child_state.ivin); | |
1677 | } | |
1678 | ||
1679 | memcpy(&incoming_stream, &child_state.incoming, | |
1680 | sizeof(incoming_stream)); | |
1681 | memcpy(&outgoing_stream, &child_state.outgoing, | |
1682 | sizeof(outgoing_stream)); | |
1683 | ||
1684 | /* Update with new address */ | |
1685 | if (options.compression) | |
1686 | mm_init_compression(pmonitor->m_zlib); | |
1687 | ||
1688 | /* Network I/O buffers */ | |
1689 | /* XXX inefficient for large buffers, need: buffer_init_from_string */ | |
1690 | buffer_clear(&input); | |
1691 | buffer_append(&input, child_state.input, child_state.ilen); | |
1692 | memset(child_state.input, 0, child_state.ilen); | |
1693 | xfree(child_state.input); | |
1694 | ||
1695 | buffer_clear(&output); | |
1696 | buffer_append(&output, child_state.output, child_state.olen); | |
1697 | memset(child_state.output, 0, child_state.olen); | |
1698 | xfree(child_state.output); | |
1699 | } | |
1700 | ||
1701 | static Kex * | |
1702 | mm_get_kex(Buffer *m) | |
1703 | { | |
1704 | Kex *kex; | |
1705 | void *blob; | |
1706 | u_int bloblen; | |
1707 | ||
1708 | kex = xcalloc(1, sizeof(*kex)); | |
1709 | kex->session_id = buffer_get_string(m, &kex->session_id_len); | |
1710 | if ((session_id2 == NULL) || | |
1711 | (kex->session_id_len != session_id2_len) || | |
1712 | (memcmp(kex->session_id, session_id2, session_id2_len) != 0)) | |
1713 | fatal("mm_get_get: internal error: bad session id"); | |
1714 | kex->we_need = buffer_get_int(m); | |
1715 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; | |
1716 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; | |
1717 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; | |
1718 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; | |
1719 | #ifdef GSSAPI | |
1720 | if (options.gss_keyex) { | |
1721 | kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server; | |
1722 | kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server; | |
1723 | kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server; | |
1724 | } | |
1725 | #endif | |
1726 | kex->server = 1; | |
1727 | kex->hostkey_type = buffer_get_int(m); | |
1728 | kex->kex_type = buffer_get_int(m); | |
1729 | blob = buffer_get_string(m, &bloblen); | |
1730 | buffer_init(&kex->my); | |
1731 | buffer_append(&kex->my, blob, bloblen); | |
1732 | xfree(blob); | |
1733 | blob = buffer_get_string(m, &bloblen); | |
1734 | buffer_init(&kex->peer); | |
1735 | buffer_append(&kex->peer, blob, bloblen); | |
1736 | xfree(blob); | |
1737 | kex->done = 1; | |
1738 | kex->flags = buffer_get_int(m); | |
1739 | kex->client_version_string = buffer_get_string(m, NULL); | |
1740 | kex->server_version_string = buffer_get_string(m, NULL); | |
1741 | kex->load_host_key=&get_hostkey_by_type; | |
1742 | kex->host_key_index=&get_hostkey_index; | |
1743 | ||
1744 | return (kex); | |
1745 | } | |
1746 | ||
1747 | /* This function requries careful sanity checking */ | |
1748 | ||
1749 | void | |
1750 | mm_get_keystate(struct monitor *pmonitor) | |
1751 | { | |
1752 | Buffer m; | |
1753 | u_char *blob, *p; | |
1754 | u_int bloblen, plen; | |
1755 | u_int32_t seqnr, packets; | |
1756 | u_int64_t blocks, bytes; | |
1757 | ||
1758 | debug3("%s: Waiting for new keys", __func__); | |
1759 | ||
1760 | buffer_init(&m); | |
1761 | mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m); | |
1762 | if (!compat20) { | |
1763 | child_state.ssh1protoflags = buffer_get_int(&m); | |
1764 | child_state.ssh1cipher = buffer_get_int(&m); | |
1765 | child_state.ssh1key = buffer_get_string(&m, | |
1766 | &child_state.ssh1keylen); | |
1767 | child_state.ivout = buffer_get_string(&m, | |
1768 | &child_state.ivoutlen); | |
1769 | child_state.ivin = buffer_get_string(&m, &child_state.ivinlen); | |
1770 | goto skip; | |
1771 | } else { | |
1772 | /* Get the Kex for rekeying */ | |
1773 | *pmonitor->m_pkex = mm_get_kex(&m); | |
1774 | } | |
1775 | ||
1776 | blob = buffer_get_string(&m, &bloblen); | |
1777 | current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen); | |
1778 | xfree(blob); | |
1779 | ||
1780 | debug3("%s: Waiting for second key", __func__); | |
1781 | blob = buffer_get_string(&m, &bloblen); | |
1782 | current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen); | |
1783 | xfree(blob); | |
1784 | ||
1785 | /* Now get sequence numbers for the packets */ | |
1786 | seqnr = buffer_get_int(&m); | |
1787 | blocks = buffer_get_int64(&m); | |
1788 | packets = buffer_get_int(&m); | |
1789 | bytes = buffer_get_int64(&m); | |
1790 | packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes); | |
1791 | seqnr = buffer_get_int(&m); | |
1792 | blocks = buffer_get_int64(&m); | |
1793 | packets = buffer_get_int(&m); | |
1794 | bytes = buffer_get_int64(&m); | |
1795 | packet_set_state(MODE_IN, seqnr, blocks, packets, bytes); | |
1796 | ||
1797 | skip: | |
1798 | /* Get the key context */ | |
1799 | child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen); | |
1800 | child_state.keyin = buffer_get_string(&m, &child_state.keyinlen); | |
1801 | ||
1802 | debug3("%s: Getting compression state", __func__); | |
1803 | /* Get compression state */ | |
1804 | p = buffer_get_string(&m, &plen); | |
1805 | if (plen != sizeof(child_state.outgoing)) | |
1806 | fatal("%s: bad request size", __func__); | |
1807 | memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing)); | |
1808 | xfree(p); | |
1809 | ||
1810 | p = buffer_get_string(&m, &plen); | |
1811 | if (plen != sizeof(child_state.incoming)) | |
1812 | fatal("%s: bad request size", __func__); | |
1813 | memcpy(&child_state.incoming, p, sizeof(child_state.incoming)); | |
1814 | xfree(p); | |
1815 | ||
1816 | /* Network I/O buffers */ | |
1817 | debug3("%s: Getting Network I/O buffers", __func__); | |
1818 | child_state.input = buffer_get_string(&m, &child_state.ilen); | |
1819 | child_state.output = buffer_get_string(&m, &child_state.olen); | |
1820 | ||
1821 | buffer_free(&m); | |
1822 | } | |
1823 | ||
1824 | ||
1825 | /* Allocation functions for zlib */ | |
1826 | void * | |
1827 | mm_zalloc(struct mm_master *mm, u_int ncount, u_int size) | |
1828 | { | |
1829 | size_t len = (size_t) size * ncount; | |
1830 | void *address; | |
1831 | ||
1832 | if (len == 0 || ncount > SIZE_T_MAX / size) | |
1833 | fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size); | |
1834 | ||
1835 | address = mm_malloc(mm, len); | |
1836 | ||
1837 | return (address); | |
1838 | } | |
1839 | ||
1840 | void | |
1841 | mm_zfree(struct mm_master *mm, void *address) | |
1842 | { | |
1843 | mm_free(mm, address); | |
1844 | } | |
1845 | ||
1846 | void | |
1847 | mm_init_compression(struct mm_master *mm) | |
1848 | { | |
1849 | outgoing_stream.zalloc = (alloc_func)mm_zalloc; | |
1850 | outgoing_stream.zfree = (free_func)mm_zfree; | |
1851 | outgoing_stream.opaque = mm; | |
1852 | ||
1853 | incoming_stream.zalloc = (alloc_func)mm_zalloc; | |
1854 | incoming_stream.zfree = (free_func)mm_zfree; | |
1855 | incoming_stream.opaque = mm; | |
1856 | } | |
1857 | ||
1858 | /* XXX */ | |
1859 | ||
1860 | #define FD_CLOSEONEXEC(x) do { \ | |
1861 | if (fcntl(x, F_SETFD, 1) == -1) \ | |
1862 | fatal("fcntl(%d, F_SETFD)", x); \ | |
1863 | } while (0) | |
1864 | ||
1865 | static void | |
1866 | monitor_socketpair(int *pair) | |
1867 | { | |
1868 | #ifdef HAVE_SOCKETPAIR | |
1869 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) | |
1870 | fatal("%s: socketpair", __func__); | |
1871 | #else | |
1872 | fatal("%s: UsePrivilegeSeparation=yes not supported", | |
1873 | __func__); | |
1874 | #endif | |
1875 | FD_CLOSEONEXEC(pair[0]); | |
1876 | FD_CLOSEONEXEC(pair[1]); | |
1877 | } | |
1878 | ||
1879 | #define MM_MEMSIZE 65536 | |
1880 | ||
1881 | struct monitor * | |
1882 | monitor_init(void) | |
1883 | { | |
1884 | struct monitor *mon; | |
1885 | int pair[2]; | |
1886 | ||
1887 | mon = xcalloc(1, sizeof(*mon)); | |
1888 | ||
1889 | monitor_socketpair(pair); | |
1890 | ||
1891 | mon->m_recvfd = pair[0]; | |
1892 | mon->m_sendfd = pair[1]; | |
1893 | ||
1894 | /* Used to share zlib space across processes */ | |
1895 | if (options.compression) { | |
1896 | mon->m_zback = mm_create(NULL, MM_MEMSIZE); | |
1897 | mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE); | |
1898 | ||
1899 | /* Compression needs to share state across borders */ | |
1900 | mm_init_compression(mon->m_zlib); | |
1901 | } | |
1902 | ||
1903 | return mon; | |
1904 | } | |
1905 | ||
1906 | void | |
1907 | monitor_reinit(struct monitor *mon) | |
1908 | { | |
1909 | int pair[2]; | |
1910 | ||
1911 | monitor_socketpair(pair); | |
1912 | ||
1913 | mon->m_recvfd = pair[0]; | |
1914 | mon->m_sendfd = pair[1]; | |
1915 | } | |
1916 | ||
1917 | #ifdef GSSAPI | |
1918 | int | |
1919 | mm_answer_gss_setup_ctx(int sock, Buffer *m) | |
1920 | { | |
1921 | gss_OID_desc goid; | |
1922 | OM_uint32 major; | |
1923 | u_int len; | |
1924 | ||
1925 | if (!options.gss_authentication && !options.gss_keyex) | |
1926 | fatal("In GSSAPI monitor when GSSAPI is disabled"); | |
1927 | ||
1928 | goid.elements = buffer_get_string(m, &len); | |
1929 | goid.length = len; | |
1930 | ||
1931 | major = ssh_gssapi_server_ctx(&gsscontext, &goid); | |
1932 | ||
1933 | xfree(goid.elements); | |
1934 | ||
1935 | buffer_clear(m); | |
1936 | buffer_put_int(m, major); | |
1937 | ||
1938 | mm_request_send(sock, MONITOR_ANS_GSSSETUP, m); | |
1939 | ||
1940 | /* Now we have a context, enable the step */ | |
1941 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); | |
1942 | ||
1943 | return (0); | |
1944 | } | |
1945 | ||
1946 | int | |
1947 | mm_answer_gss_accept_ctx(int sock, Buffer *m) | |
1948 | { | |
1949 | gss_buffer_desc in; | |
1950 | gss_buffer_desc out = GSS_C_EMPTY_BUFFER; | |
1951 | OM_uint32 major, minor; | |
1952 | OM_uint32 flags = 0; /* GSI needs this */ | |
1953 | u_int len; | |
1954 | ||
1955 | if (!options.gss_authentication && !options.gss_keyex) | |
1956 | fatal("In GSSAPI monitor when GSSAPI is disabled"); | |
1957 | ||
1958 | in.value = buffer_get_string(m, &len); | |
1959 | in.length = len; | |
1960 | major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); | |
1961 | xfree(in.value); | |
1962 | ||
1963 | buffer_clear(m); | |
1964 | buffer_put_int(m, major); | |
1965 | buffer_put_string(m, out.value, out.length); | |
1966 | buffer_put_int(m, flags); | |
1967 | mm_request_send(sock, MONITOR_ANS_GSSSTEP, m); | |
1968 | ||
1969 | gss_release_buffer(&minor, &out); | |
1970 | ||
1971 | if (major == GSS_S_COMPLETE) { | |
1972 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); | |
1973 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); | |
1974 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1); | |
1975 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1); | |
1976 | } | |
1977 | return (0); | |
1978 | } | |
1979 | ||
1980 | int | |
1981 | mm_answer_gss_checkmic(int sock, Buffer *m) | |
1982 | { | |
1983 | gss_buffer_desc gssbuf, mic; | |
1984 | OM_uint32 ret; | |
1985 | u_int len; | |
1986 | ||
1987 | if (!options.gss_authentication && !options.gss_keyex) | |
1988 | fatal("In GSSAPI monitor when GSSAPI is disabled"); | |
1989 | ||
1990 | gssbuf.value = buffer_get_string(m, &len); | |
1991 | gssbuf.length = len; | |
1992 | mic.value = buffer_get_string(m, &len); | |
1993 | mic.length = len; | |
1994 | ||
1995 | ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic); | |
1996 | ||
1997 | xfree(gssbuf.value); | |
1998 | xfree(mic.value); | |
1999 | ||
2000 | buffer_clear(m); | |
2001 | buffer_put_int(m, ret); | |
2002 | ||
2003 | mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m); | |
2004 | ||
2005 | if (!GSS_ERROR(ret)) | |
2006 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); | |
2007 | ||
2008 | return (0); | |
2009 | } | |
2010 | ||
2011 | int | |
2012 | mm_answer_gss_userok(int sock, Buffer *m) | |
2013 | { | |
2014 | int authenticated; | |
2015 | ||
2016 | if (!options.gss_authentication && !options.gss_keyex) | |
2017 | fatal("In GSSAPI monitor when GSSAPI is disabled"); | |
2018 | ||
2019 | authenticated = authctxt->valid && | |
2020 | ssh_gssapi_userok(authctxt->user, authctxt->pw); | |
2021 | ||
2022 | buffer_clear(m); | |
2023 | buffer_put_int(m, authenticated); | |
2024 | ||
2025 | debug3("%s: sending result %d", __func__, authenticated); | |
2026 | mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m); | |
2027 | ||
2028 | auth_method = "gssapi-with-mic"; | |
2029 | ||
2030 | /* Monitor loop will terminate if authenticated */ | |
2031 | return (authenticated); | |
2032 | } | |
2033 | ||
2034 | int | |
2035 | mm_answer_gss_sign(int socket, Buffer *m) | |
2036 | { | |
2037 | gss_buffer_desc data; | |
2038 | gss_buffer_desc hash = GSS_C_EMPTY_BUFFER; | |
2039 | OM_uint32 major, minor; | |
2040 | u_int len; | |
2041 | ||
2042 | if (!options.gss_authentication && !options.gss_keyex) | |
2043 | fatal("In GSSAPI monitor when GSSAPI is disabled"); | |
2044 | ||
2045 | data.value = buffer_get_string(m, &len); | |
2046 | data.length = len; | |
2047 | if (data.length != 20) | |
2048 | fatal("%s: data length incorrect: %d", __func__, | |
2049 | (int) data.length); | |
2050 | ||
2051 | /* Save the session ID on the first time around */ | |
2052 | if (session_id2_len == 0) { | |
2053 | session_id2_len = data.length; | |
2054 | session_id2 = xmalloc(session_id2_len); | |
2055 | memcpy(session_id2, data.value, session_id2_len); | |
2056 | } | |
2057 | major = ssh_gssapi_sign(gsscontext, &data, &hash); | |
2058 | ||
2059 | xfree(data.value); | |
2060 | ||
2061 | buffer_clear(m); | |
2062 | buffer_put_int(m, major); | |
2063 | buffer_put_string(m, hash.value, hash.length); | |
2064 | ||
2065 | mm_request_send(socket, MONITOR_ANS_GSSSIGN, m); | |
2066 | ||
2067 | gss_release_buffer(&minor, &hash); | |
2068 | ||
2069 | /* Turn on getpwnam permissions */ | |
2070 | monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); | |
2071 | ||
2072 | /* And credential updating, for when rekeying */ | |
2073 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1); | |
2074 | ||
2075 | return (0); | |
2076 | } | |
2077 | ||
2078 | int | |
2079 | mm_answer_gss_updatecreds(int socket, Buffer *m) { | |
2080 | ssh_gssapi_ccache store; | |
2081 | int ok; | |
2082 | ||
2083 | store.filename = buffer_get_string(m, NULL); | |
2084 | store.envvar = buffer_get_string(m, NULL); | |
2085 | store.envval = buffer_get_string(m, NULL); | |
2086 | ||
2087 | ok = ssh_gssapi_update_creds(&store); | |
2088 | ||
2089 | xfree(store.filename); | |
2090 | xfree(store.envvar); | |
2091 | xfree(store.envval); | |
2092 | ||
2093 | buffer_clear(m); | |
2094 | buffer_put_int(m, ok); | |
2095 | ||
2096 | mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m); | |
2097 | ||
2098 | return(0); | |
2099 | } | |
2100 | ||
2101 | #endif /* GSSAPI */ | |
2102 | ||
2103 | #ifdef JPAKE | |
2104 | int | |
2105 | mm_answer_jpake_step1(int sock, Buffer *m) | |
2106 | { | |
2107 | struct jpake_ctx *pctx; | |
2108 | u_char *x3_proof, *x4_proof; | |
2109 | u_int x3_proof_len, x4_proof_len; | |
2110 | ||
2111 | if (!options.zero_knowledge_password_authentication) | |
2112 | fatal("zero_knowledge_password_authentication disabled"); | |
2113 | ||
2114 | if (authctxt->jpake_ctx != NULL) | |
2115 | fatal("%s: authctxt->jpake_ctx already set (%p)", | |
2116 | __func__, authctxt->jpake_ctx); | |
2117 | authctxt->jpake_ctx = pctx = jpake_new(); | |
2118 | ||
2119 | jpake_step1(pctx->grp, | |
2120 | &pctx->server_id, &pctx->server_id_len, | |
2121 | &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4, | |
2122 | &x3_proof, &x3_proof_len, | |
2123 | &x4_proof, &x4_proof_len); | |
2124 | ||
2125 | JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__)); | |
2126 | ||
2127 | buffer_clear(m); | |
2128 | ||
2129 | buffer_put_string(m, pctx->server_id, pctx->server_id_len); | |
2130 | buffer_put_bignum2(m, pctx->g_x3); | |
2131 | buffer_put_bignum2(m, pctx->g_x4); | |
2132 | buffer_put_string(m, x3_proof, x3_proof_len); | |
2133 | buffer_put_string(m, x4_proof, x4_proof_len); | |
2134 | ||
2135 | debug3("%s: sending step1", __func__); | |
2136 | mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m); | |
2137 | ||
2138 | bzero(x3_proof, x3_proof_len); | |
2139 | bzero(x4_proof, x4_proof_len); | |
2140 | xfree(x3_proof); | |
2141 | xfree(x4_proof); | |
2142 | ||
2143 | monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1); | |
2144 | monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0); | |
2145 | ||
2146 | return 0; | |
2147 | } | |
2148 | ||
2149 | int | |
2150 | mm_answer_jpake_get_pwdata(int sock, Buffer *m) | |
2151 | { | |
2152 | struct jpake_ctx *pctx = authctxt->jpake_ctx; | |
2153 | char *hash_scheme, *salt; | |
2154 | ||
2155 | if (pctx == NULL) | |
2156 | fatal("%s: pctx == NULL", __func__); | |
2157 | ||
2158 | auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt); | |
2159 | ||
2160 | buffer_clear(m); | |
2161 | /* pctx->s is sensitive, not returned to slave */ | |
2162 | buffer_put_cstring(m, hash_scheme); | |
2163 | buffer_put_cstring(m, salt); | |
2164 | ||
2165 | debug3("%s: sending pwdata", __func__); | |
2166 | mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m); | |
2167 | ||
2168 | bzero(hash_scheme, strlen(hash_scheme)); | |
2169 | bzero(salt, strlen(salt)); | |
2170 | xfree(hash_scheme); | |
2171 | xfree(salt); | |
2172 | ||
2173 | monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1); | |
2174 | ||
2175 | return 0; | |
2176 | } | |
2177 | ||
2178 | int | |
2179 | mm_answer_jpake_step2(int sock, Buffer *m) | |
2180 | { | |
2181 | struct jpake_ctx *pctx = authctxt->jpake_ctx; | |
2182 | u_char *x1_proof, *x2_proof, *x4_s_proof; | |
2183 | u_int x1_proof_len, x2_proof_len, x4_s_proof_len; | |
2184 | ||
2185 | if (pctx == NULL) | |
2186 | fatal("%s: pctx == NULL", __func__); | |
2187 | ||
2188 | if ((pctx->g_x1 = BN_new()) == NULL || | |
2189 | (pctx->g_x2 = BN_new()) == NULL) | |
2190 | fatal("%s: BN_new", __func__); | |
2191 | buffer_get_bignum2(m, pctx->g_x1); | |
2192 | buffer_get_bignum2(m, pctx->g_x2); | |
2193 | pctx->client_id = buffer_get_string(m, &pctx->client_id_len); | |
2194 | x1_proof = buffer_get_string(m, &x1_proof_len); | |
2195 | x2_proof = buffer_get_string(m, &x2_proof_len); | |
2196 | ||
2197 | jpake_step2(pctx->grp, pctx->s, pctx->g_x3, | |
2198 | pctx->g_x1, pctx->g_x2, pctx->x4, | |
2199 | pctx->client_id, pctx->client_id_len, | |
2200 | pctx->server_id, pctx->server_id_len, | |
2201 | x1_proof, x1_proof_len, | |
2202 | x2_proof, x2_proof_len, | |
2203 | &pctx->b, | |
2204 | &x4_s_proof, &x4_s_proof_len); | |
2205 | ||
2206 | JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__)); | |
2207 | ||
2208 | bzero(x1_proof, x1_proof_len); | |
2209 | bzero(x2_proof, x2_proof_len); | |
2210 | xfree(x1_proof); | |
2211 | xfree(x2_proof); | |
2212 | ||
2213 | buffer_clear(m); | |
2214 | ||
2215 | buffer_put_bignum2(m, pctx->b); | |
2216 | buffer_put_string(m, x4_s_proof, x4_s_proof_len); | |
2217 | ||
2218 | debug3("%s: sending step2", __func__); | |
2219 | mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m); | |
2220 | ||
2221 | bzero(x4_s_proof, x4_s_proof_len); | |
2222 | xfree(x4_s_proof); | |
2223 | ||
2224 | monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1); | |
2225 | ||
2226 | return 0; | |
2227 | } | |
2228 | ||
2229 | int | |
2230 | mm_answer_jpake_key_confirm(int sock, Buffer *m) | |
2231 | { | |
2232 | struct jpake_ctx *pctx = authctxt->jpake_ctx; | |
2233 | u_char *x2_s_proof; | |
2234 | u_int x2_s_proof_len; | |
2235 | ||
2236 | if (pctx == NULL) | |
2237 | fatal("%s: pctx == NULL", __func__); | |
2238 | ||
2239 | if ((pctx->a = BN_new()) == NULL) | |
2240 | fatal("%s: BN_new", __func__); | |
2241 | buffer_get_bignum2(m, pctx->a); | |
2242 | x2_s_proof = buffer_get_string(m, &x2_s_proof_len); | |
2243 | ||
2244 | jpake_key_confirm(pctx->grp, pctx->s, pctx->a, | |
2245 | pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2, | |
2246 | pctx->server_id, pctx->server_id_len, | |
2247 | pctx->client_id, pctx->client_id_len, | |
2248 | session_id2, session_id2_len, | |
2249 | x2_s_proof, x2_s_proof_len, | |
2250 | &pctx->k, | |
2251 | &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len); | |
2252 | ||
2253 | JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__)); | |
2254 | ||
2255 | bzero(x2_s_proof, x2_s_proof_len); | |
2256 | buffer_clear(m); | |
2257 | ||
2258 | /* pctx->k is sensitive, not sent */ | |
2259 | buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len); | |
2260 | ||
2261 | debug3("%s: sending confirmation hash", __func__); | |
2262 | mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m); | |
2263 | ||
2264 | monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1); | |
2265 | ||
2266 | return 0; | |
2267 | } | |
2268 | ||
2269 | int | |
2270 | mm_answer_jpake_check_confirm(int sock, Buffer *m) | |
2271 | { | |
2272 | int authenticated = 0; | |
2273 | u_char *peer_confirm_hash; | |
2274 | u_int peer_confirm_hash_len; | |
2275 | struct jpake_ctx *pctx = authctxt->jpake_ctx; | |
2276 | ||
2277 | if (pctx == NULL) | |
2278 | fatal("%s: pctx == NULL", __func__); | |
2279 | ||
2280 | peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len); | |
2281 | ||
2282 | authenticated = jpake_check_confirm(pctx->k, | |
2283 | pctx->client_id, pctx->client_id_len, | |
2284 | session_id2, session_id2_len, | |
2285 | peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid; | |
2286 | ||
2287 | JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__)); | |
2288 | ||
2289 | bzero(peer_confirm_hash, peer_confirm_hash_len); | |
2290 | xfree(peer_confirm_hash); | |
2291 | ||
2292 | buffer_clear(m); | |
2293 | buffer_put_int(m, authenticated); | |
2294 | ||
2295 | debug3("%s: sending result %d", __func__, authenticated); | |
2296 | mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m); | |
2297 | ||
2298 | monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1); | |
2299 | ||
2300 | auth_method = "jpake-01@openssh.com"; | |
2301 | return authenticated; | |
2302 | } | |
2303 | ||
2304 | #endif /* JPAKE */ |