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