blob: 0fe5bc10d8790f8bdc9281585a5baf41df9cf367 [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Ben Lindstromc8615472002-03-26 03:20:45 +000028RCSID("$OpenBSD: monitor_wrap.c,v 1.5 2002/03/25 20:12:10 stevesk Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
30#include <openssl/bn.h>
31#include <openssl/dh.h>
32
33#include "ssh.h"
34#include "dh.h"
35#include "kex.h"
36#include "auth.h"
37#include "buffer.h"
38#include "bufaux.h"
39#include "packet.h"
40#include "mac.h"
41#include "log.h"
42#include "zlib.h"
43#include "monitor.h"
44#include "monitor_wrap.h"
45#include "xmalloc.h"
46#include "atomicio.h"
47#include "monitor_fdpass.h"
48#include "getput.h"
49
50#include "auth.h"
51#include "channels.h"
52#include "session.h"
53
54/* Imports */
55extern int compat20;
56extern Newkeys *newkeys[];
57extern z_stream incoming_stream;
58extern z_stream outgoing_stream;
59extern struct monitor *monitor;
60extern Buffer input, output;
61
62void
63mm_request_send(int socket, enum monitor_reqtype type, Buffer *m)
64{
65 u_char buf[5];
66 u_int mlen = buffer_len(m);
67
68 debug3("%s entering: type %d", __FUNCTION__, type);
69
70 PUT_32BIT(buf, mlen + 1);
71 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
72 if (atomicio(write, socket, buf, sizeof(buf)) != sizeof(buf))
73 fatal("%s: write", __FUNCTION__);
74 if (atomicio(write, socket, buffer_ptr(m), mlen) != mlen)
75 fatal("%s: write", __FUNCTION__);
76}
77
78void
79mm_request_receive(int socket, Buffer *m)
80{
81 u_char buf[4];
82 ssize_t res;
83 u_int msg_len;
84
85 debug3("%s entering", __FUNCTION__);
86
87 res = atomicio(read, socket, buf, sizeof(buf));
88 if (res != sizeof(buf)) {
89 if (res == 0)
90 fatal_cleanup();
Ben Lindstromc8615472002-03-26 03:20:45 +000091 fatal("%s: read: %ld", __FUNCTION__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092 }
93 msg_len = GET_32BIT(buf);
94 if (msg_len > 256 * 1024)
95 fatal("%s: read: bad msg_len %d", __FUNCTION__, msg_len);
96 buffer_clear(m);
97 buffer_append_space(m, msg_len);
98 res = atomicio(read, socket, buffer_ptr(m), msg_len);
99 if (res != msg_len)
Ben Lindstromc8615472002-03-26 03:20:45 +0000100 fatal("%s: read: %ld != msg_len", __FUNCTION__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101}
102
103void
104mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m)
105{
106 u_char rtype;
107
108 debug3("%s entering: type %d", __FUNCTION__, type);
109
110 mm_request_receive(socket, m);
111 rtype = buffer_get_char(m);
112 if (rtype != type)
113 fatal("%s: read: rtype %d != type %d", __FUNCTION__,
114 rtype, type);
115}
116
117DH *
118mm_choose_dh(int min, int nbits, int max)
119{
120 BIGNUM *p, *g;
121 int success = 0;
122 Buffer m;
123
124 buffer_init(&m);
125 buffer_put_int(&m, min);
126 buffer_put_int(&m, nbits);
127 buffer_put_int(&m, max);
128
129 mm_request_send(monitor->m_recvfd, MONITOR_REQ_MODULI, &m);
130
131 debug3("%s: waiting for MONITOR_ANS_MODULI", __FUNCTION__);
132 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_MODULI, &m);
133
134 success = buffer_get_char(&m);
135 if (success == 0)
136 fatal("%s: MONITOR_ANS_MODULI failed", __FUNCTION__);
137
138 if ((p = BN_new()) == NULL)
139 fatal("%s: BN_new failed", __FUNCTION__);
140 if ((g = BN_new()) == NULL)
141 fatal("%s: BN_new failed", __FUNCTION__);
142 buffer_get_bignum2(&m, p);
143 buffer_get_bignum2(&m, g);
144
145 debug3("%s: remaining %d", __FUNCTION__, buffer_len(&m));
146 buffer_free(&m);
147
148 return (dh_new_group(g, p));
149}
150
151int
152mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
153{
154 Kex *kex = *monitor->m_pkex;
155 Buffer m;
156
157 debug3("%s entering", __FUNCTION__);
158
159 buffer_init(&m);
160 buffer_put_int(&m, kex->host_key_index(key));
161 buffer_put_string(&m, data, datalen);
162
163 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SIGN, &m);
164
165 debug3("%s: waiting for MONITOR_ANS_SIGN", __FUNCTION__);
166 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SIGN, &m);
167 *sigp = buffer_get_string(&m, lenp);
168 buffer_free(&m);
169
170 return (0);
171}
172
173struct passwd *
174mm_getpwnamallow(const char *login)
175{
176 Buffer m;
177 struct passwd *pw;
178 u_int pwlen;
179
180 debug3("%s entering", __FUNCTION__);
181
182 buffer_init(&m);
183 buffer_put_cstring(&m, login);
184
185 mm_request_send(monitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
186
187 debug3("%s: waiting for MONITOR_ANS_PWNAM", __FUNCTION__);
188 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
189
190 if (buffer_get_char(&m) == 0) {
191 buffer_free(&m);
192 return (NULL);
193 }
194 pw = buffer_get_string(&m, &pwlen);
195 if (pwlen != sizeof(struct passwd))
196 fatal("%s: struct passwd size mismatch", __FUNCTION__);
197 pw->pw_name = buffer_get_string(&m, NULL);
198 pw->pw_passwd = buffer_get_string(&m, NULL);
199 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000200#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000202#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203 pw->pw_dir = buffer_get_string(&m, NULL);
204 pw->pw_shell = buffer_get_string(&m, NULL);
205 buffer_free(&m);
206
207 return (pw);
208}
209
210/* Inform the privileged process about service and style */
211
212void
213mm_inform_authserv(char *service, char *style)
214{
215 Buffer m;
216
217 debug3("%s entering", __FUNCTION__);
218
219 buffer_init(&m);
220 buffer_put_cstring(&m, service);
221 buffer_put_cstring(&m, style ? style : "");
222
223 mm_request_send(monitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
224
225 buffer_free(&m);
226}
227
228/* Do the password authentication */
229int
230mm_auth_password(Authctxt *authctxt, char *password)
231{
232 Buffer m;
233 int authenticated = 0;
234
235 debug3("%s entering", __FUNCTION__);
236
237 buffer_init(&m);
238 buffer_put_cstring(&m, password);
239 mm_request_send(monitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
240
241 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __FUNCTION__);
242 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
243
244 authenticated = buffer_get_int(&m);
245
246 buffer_free(&m);
247
248 debug3("%s: user %sauthenticated",
249 __FUNCTION__, authenticated ? "" : "not ");
250 return (authenticated);
251}
252
253int
254mm_user_key_allowed(struct passwd *pw, Key *key)
255{
256 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
257}
258
259int
260mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
261 Key *key)
262{
263 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
264}
265
266int
267mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
268 char *host, Key *key)
269{
270 int ret;
271
272 key->type = KEY_RSA; /* XXX hack for key_to_blob */
273 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
274 key->type = KEY_RSA1;
275 return (ret);
276}
277
278static void
279mm_send_debug(Buffer *m)
280{
281 char *msg;
282
283 while (buffer_len(m)) {
284 msg = buffer_get_string(m, NULL);
285 debug3("%s: Sending debug: %s", __FUNCTION__, msg);
286 packet_send_debug("%s", msg);
287 xfree(msg);
288 }
289}
290
291int
292mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
293{
294 Buffer m;
295 u_char *blob;
296 u_int len;
297 int allowed = 0;
298
299 debug3("%s entering", __FUNCTION__);
300
301 /* Convert the key to a blob and the pass it over */
302 if (!key_to_blob(key, &blob, &len))
303 return (0);
304
305 buffer_init(&m);
306 buffer_put_int(&m, type);
307 buffer_put_cstring(&m, user ? user : "");
308 buffer_put_cstring(&m, host ? host : "");
309 buffer_put_string(&m, blob, len);
310 xfree(blob);
311
312 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
313
314 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __FUNCTION__);
315 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
316
317 allowed = buffer_get_int(&m);
318
319 /* Send potential debug messages */
320 mm_send_debug(&m);
321
322 buffer_free(&m);
323
324 return (allowed);
325}
326
327/*
328 * This key verify needs to send the key type along, because the
329 * privileged parent makes the decision if the key is allowed
330 * for authentication.
331 */
332
333int
334mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
335{
336 Buffer m;
337 u_char *blob;
338 u_int len;
339 int verified = 0;
340
341 debug3("%s entering", __FUNCTION__);
342
343 /* Convert the key to a blob and the pass it over */
344 if (!key_to_blob(key, &blob, &len))
345 return (0);
346
347 buffer_init(&m);
348 buffer_put_string(&m, blob, len);
349 buffer_put_string(&m, sig, siglen);
350 buffer_put_string(&m, data, datalen);
351 xfree(blob);
352
353 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
354
355 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __FUNCTION__);
356 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
357
358 verified = buffer_get_int(&m);
359
360 buffer_free(&m);
361
362 return (verified);
363}
364
365/* Export key state after authentication */
366Newkeys *
367mm_newkeys_from_blob(u_char *blob, int blen)
368{
369 Buffer b;
370 u_int len;
371 Newkeys *newkey = NULL;
372 Enc *enc;
373 Mac *mac;
374 Comp *comp;
375
376 debug3("%s: %p(%d)", __FUNCTION__, blob, blen);
377#ifdef DEBUG_PK
378 dump_base64(stderr, blob, blen);
379#endif
380 buffer_init(&b);
381 buffer_append(&b, blob, blen);
382
383 newkey = xmalloc(sizeof(*newkey));
384 enc = &newkey->enc;
385 mac = &newkey->mac;
386 comp = &newkey->comp;
387
388 /* Enc structure */
389 enc->name = buffer_get_string(&b, NULL);
390 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
391 enc->enabled = buffer_get_int(&b);
392 enc->block_size = buffer_get_int(&b);
393 enc->key = buffer_get_string(&b, &enc->key_len);
394 enc->iv = buffer_get_string(&b, &len);
395 if (len != enc->block_size)
396 fatal("%s: bad ivlen: expected %d != %d", __FUNCTION__,
397 enc->block_size, len);
398
399 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
400 fatal("%s: bad cipher name %s or pointer %p", __FUNCTION__,
401 enc->name, enc->cipher);
402
403 /* Mac structure */
404 mac->name = buffer_get_string(&b, NULL);
405 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
406 fatal("%s: can not init mac %s", __FUNCTION__, mac->name);
407 mac->enabled = buffer_get_int(&b);
408 mac->key = buffer_get_string(&b, &len);
409 if (len > mac->key_len)
410 fatal("%s: bad mac key lenght: %d > %d", __FUNCTION__, len,
411 mac->key_len);
412 mac->key_len = len;
413
414 /* Comp structure */
415 comp->type = buffer_get_int(&b);
416 comp->enabled = buffer_get_int(&b);
417 comp->name = buffer_get_string(&b, NULL);
418
419 len = buffer_len(&b);
420 if (len != 0)
421 error("newkeys_from_blob: remaining bytes in blob %d", len);
422 buffer_free(&b);
423 return (newkey);
424}
425
426int
427mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
428{
429 Buffer b;
430 int len;
431 u_char *buf;
432 Enc *enc;
433 Mac *mac;
434 Comp *comp;
435 Newkeys *newkey = newkeys[mode];
436
437 debug3("%s: converting %p", __FUNCTION__, newkey);
438
439 if (newkey == NULL) {
440 error("%s: newkey == NULL", __FUNCTION__);
441 return 0;
442 }
443 enc = &newkey->enc;
444 mac = &newkey->mac;
445 comp = &newkey->comp;
446
447 buffer_init(&b);
448 /* Enc structure */
449 buffer_put_cstring(&b, enc->name);
450 /* The cipher struct is constant and shared, you export pointer */
451 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
452 buffer_put_int(&b, enc->enabled);
453 buffer_put_int(&b, enc->block_size);
454 buffer_put_string(&b, enc->key, enc->key_len);
455 packet_get_keyiv(mode, enc->iv, enc->block_size);
456 buffer_put_string(&b, enc->iv, enc->block_size);
457
458 /* Mac structure */
459 buffer_put_cstring(&b, mac->name);
460 buffer_put_int(&b, mac->enabled);
461 buffer_put_string(&b, mac->key, mac->key_len);
462
463 /* Comp structure */
464 buffer_put_int(&b, comp->type);
465 buffer_put_int(&b, comp->enabled);
466 buffer_put_cstring(&b, comp->name);
467
468 len = buffer_len(&b);
469 buf = xmalloc(len);
470 memcpy(buf, buffer_ptr(&b), len);
471 memset(buffer_ptr(&b), 0, len);
472 buffer_free(&b);
473 if (lenp != NULL)
474 *lenp = len;
475 if (blobp != NULL)
476 *blobp = buf;
477 return len;
478}
479
480static void
481mm_send_kex(Buffer *m, Kex *kex)
482{
483 buffer_put_string(m, kex->session_id, kex->session_id_len);
484 buffer_put_int(m, kex->we_need);
485 buffer_put_int(m, kex->hostkey_type);
486 buffer_put_int(m, kex->kex_type);
487 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
488 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
489 buffer_put_int(m, kex->flags);
490 buffer_put_cstring(m, kex->client_version_string);
491 buffer_put_cstring(m, kex->server_version_string);
492}
493
494void
495mm_send_keystate(struct monitor *monitor)
496{
497 Buffer m;
498 u_char *blob, *p;
499 u_int bloblen, plen;
500
501 buffer_init(&m);
502
503 if (!compat20) {
504 u_char iv[24];
505 int ivlen;
506
507 buffer_put_int(&m, packet_get_protocol_flags());
508
509 buffer_put_int(&m, packet_get_ssh1_cipher());
510
511 debug3("%s: Sending ssh1 IV", __FUNCTION__);
512 ivlen = packet_get_keyiv_len(MODE_OUT);
513 packet_get_keyiv(MODE_OUT, iv, ivlen);
514 buffer_put_string(&m, iv, ivlen);
515 ivlen = packet_get_keyiv_len(MODE_OUT);
516 packet_get_keyiv(MODE_IN, iv, ivlen);
517 buffer_put_string(&m, iv, ivlen);
518 goto skip;
519 } else {
520 /* Kex for rekeying */
521 mm_send_kex(&m, *monitor->m_pkex);
522 }
523
524 debug3("%s: Sending new keys: %p %p",
525 __FUNCTION__, newkeys[MODE_OUT], newkeys[MODE_IN]);
526
527 /* Keys from Kex */
528 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
529 fatal("%s: conversion of newkeys failed", __FUNCTION__);
530
531 buffer_put_string(&m, blob, bloblen);
532 xfree(blob);
533
534 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
535 fatal("%s: conversion of newkeys failed", __FUNCTION__);
536
537 buffer_put_string(&m, blob, bloblen);
538 xfree(blob);
539
540 buffer_put_int(&m, packet_get_seqnr(MODE_OUT));
541 buffer_put_int(&m, packet_get_seqnr(MODE_IN));
542
543 debug3("%s: New keys have been sent", __FUNCTION__);
544 skip:
545 /* More key context */
546 plen = packet_get_keycontext(MODE_OUT, NULL);
547 p = xmalloc(plen+1);
548 packet_get_keycontext(MODE_OUT, p);
549 buffer_put_string(&m, p, plen);
550 xfree(p);
551
552 plen = packet_get_keycontext(MODE_IN, NULL);
553 p = xmalloc(plen+1);
554 packet_get_keycontext(MODE_IN, p);
555 buffer_put_string(&m, p, plen);
556 xfree(p);
557
558 /* Compression state */
559 debug3("%s: Sending compression state", __FUNCTION__);
560 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
561 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
562
563 /* Network I/O buffers */
564 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
565 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
566
567 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
568 debug3("%s: Finished sending state", __FUNCTION__);
569
570 buffer_free(&m);
571}
572
573int
574mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
575{
576 Buffer m;
577 u_char *p;
578 int success = 0;
579
580 buffer_init(&m);
581 mm_request_send(monitor->m_recvfd, MONITOR_REQ_PTY, &m);
582
583 debug3("%s: waiting for MONITOR_ANS_PTY", __FUNCTION__);
584 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_PTY, &m);
585
586 success = buffer_get_int(&m);
587 if (success == 0) {
588 debug3("%s: pty alloc failed", __FUNCTION__);
589 buffer_free(&m);
590 return (0);
591 }
592 p = buffer_get_string(&m, NULL);
593 buffer_free(&m);
594
595 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
596 xfree(p);
597
598 *ptyfd = mm_receive_fd(monitor->m_recvfd);
599 *ttyfd = mm_receive_fd(monitor->m_recvfd);
600
601 /* Success */
602 return (1);
603}
604
605void
606mm_session_pty_cleanup2(void *session)
607{
608 Session *s = session;
609 Buffer m;
610
611 if (s->ttyfd == -1)
612 return;
613 buffer_init(&m);
614 buffer_put_cstring(&m, s->tty);
615 mm_request_send(monitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
616 buffer_free(&m);
617
618 /* closed dup'ed master */
619 if (close(s->ptymaster) < 0)
620 error("close(s->ptymaster): %s", strerror(errno));
621
622 /* unlink pty from session */
623 s->ttyfd = -1;
624}
625
Damien Miller79418552002-04-23 20:28:48 +1000626#ifdef USE_PAM
627void
628mm_start_pam(char *user)
629{
630 Buffer m;
631
632 debug3("%s entering", __FUNCTION__);
633
634 buffer_init(&m);
635 buffer_put_cstring(&m, user);
636
637 mm_request_send(monitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
638
639 buffer_free(&m);
640}
641#endif /* USE_PAM */
642
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643/* Request process termination */
644
645void
646mm_terminate(void)
647{
648 Buffer m;
649
650 buffer_init(&m);
651 mm_request_send(monitor->m_recvfd, MONITOR_REQ_TERM, &m);
652 buffer_free(&m);
653}
654
655int
656mm_ssh1_session_key(BIGNUM *num)
657{
658 int rsafail;
659 Buffer m;
660
661 buffer_init(&m);
662 buffer_put_bignum2(&m, num);
663 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
664
665 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
666
667 rsafail = buffer_get_int(&m);
668 buffer_get_bignum2(&m, num);
669
670 buffer_free(&m);
671
672 return (rsafail);
673}
674
675static void
676mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
677 char ***prompts, u_int **echo_on)
678{
679 *name = xstrdup("");
680 *infotxt = xstrdup("");
681 *numprompts = 1;
682 *prompts = xmalloc(*numprompts * sizeof(char*));
683 *echo_on = xmalloc(*numprompts * sizeof(u_int));
684 (*echo_on)[0] = 0;
685}
686
687int
688mm_bsdauth_query(void *ctx, char **name, char **infotxt,
689 u_int *numprompts, char ***prompts, u_int **echo_on)
690{
691 Buffer m;
692 int res;
693 char *challenge;
694
695 debug3("%s: entering", __FUNCTION__);
696
697 buffer_init(&m);
698 mm_request_send(monitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
699
700 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
701 &m);
702 res = buffer_get_int(&m);
703 if (res == -1) {
704 debug3("%s: no challenge", __FUNCTION__);
705 buffer_free(&m);
706 return (-1);
707 }
708
709 /* Get the challenge, and format the response */
710 challenge = buffer_get_string(&m, NULL);
711 buffer_free(&m);
712
713 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
714 (*prompts)[0] = challenge;
715
716 debug3("%s: received challenge: %s", __FUNCTION__, challenge);
717
718 return (0);
719}
720
721int
722mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
723{
724 Buffer m;
725 int authok;
726
727 debug3("%s: entering", __FUNCTION__);
728 if (numresponses != 1)
729 return (-1);
730
731 buffer_init(&m);
732 buffer_put_cstring(&m, responses[0]);
733 mm_request_send(monitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
734
735 mm_request_receive_expect(monitor->m_recvfd,
736 MONITOR_ANS_BSDAUTHRESPOND, &m);
737
738 authok = buffer_get_int(&m);
739 buffer_free(&m);
740
741 return ((authok == 0) ? -1 : 0);
742}
743
744int
745mm_skey_query(void *ctx, char **name, char **infotxt,
746 u_int *numprompts, char ***prompts, u_int **echo_on)
747{
748 Buffer m;
749 int len, res;
750 char *p, *challenge;
751
752 debug3("%s: entering", __FUNCTION__);
753
754 buffer_init(&m);
755 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
756
757 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
758 &m);
759 res = buffer_get_int(&m);
760 if (res == -1) {
761 debug3("%s: no challenge", __FUNCTION__);
762 buffer_free(&m);
763 return (-1);
764 }
765
766 /* Get the challenge, and format the response */
767 challenge = buffer_get_string(&m, NULL);
768 buffer_free(&m);
769
770 debug3("%s: received challenge: %s", __FUNCTION__, challenge);
771
772 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
773
774 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
775 p = xmalloc(len);
776 strlcpy(p, challenge, len);
777 strlcat(p, SKEY_PROMPT, len);
778 (*prompts)[0] = p;
779 xfree(challenge);
780
781 return (0);
782}
783
784int
785mm_skey_respond(void *ctx, u_int numresponses, char **responses)
786{
787 Buffer m;
788 int authok;
789
790 debug3("%s: entering", __FUNCTION__);
791 if (numresponses != 1)
792 return (-1);
793
794 buffer_init(&m);
795 buffer_put_cstring(&m, responses[0]);
796 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
797
798 mm_request_receive_expect(monitor->m_recvfd,
799 MONITOR_ANS_SKEYRESPOND, &m);
800
801 authok = buffer_get_int(&m);
802 buffer_free(&m);
803
804 return ((authok == 0) ? -1 : 0);
805}
806
807void
808mm_ssh1_session_id(u_char session_id[16])
809{
810 Buffer m;
811 int i;
812
813 debug3("%s entering", __FUNCTION__);
814
815 buffer_init(&m);
816 for (i = 0; i < 16; i++)
817 buffer_put_char(&m, session_id[i]);
818
819 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SESSID, &m);
820 buffer_free(&m);
821}
822
823int
824mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
825{
826 Buffer m;
827 Key *key;
828 u_char *blob;
829 u_int blen;
830 int allowed = 0;
831
832 debug3("%s entering", __FUNCTION__);
833
834 buffer_init(&m);
835 buffer_put_bignum2(&m, client_n);
836
837 mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
838 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
839
840 allowed = buffer_get_int(&m);
841
842 if (allowed && rkey != NULL) {
843 blob = buffer_get_string(&m, &blen);
844 if ((key = key_from_blob(blob, blen)) == NULL)
845 fatal("%s: key_from_blob failed", __FUNCTION__);
846 *rkey = key;
847 xfree(blob);
848 }
849 mm_send_debug(&m);
850 buffer_free(&m);
851
852 return (allowed);
853}
854
855BIGNUM *
856mm_auth_rsa_generate_challenge(Key *key)
857{
858 Buffer m;
859 BIGNUM *challenge;
860 u_char *blob;
861 u_int blen;
862
863 debug3("%s entering", __FUNCTION__);
864
865 if ((challenge = BN_new()) == NULL)
866 fatal("%s: BN_new failed", __FUNCTION__);
867
868 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
869 if (key_to_blob(key, &blob, &blen) == 0)
870 fatal("%s: key_to_blob failed", __FUNCTION__);
871 key->type = KEY_RSA1;
872
873 buffer_init(&m);
874 buffer_put_string(&m, blob, blen);
875 xfree(blob);
876
877 mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
878 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
879
880 buffer_get_bignum2(&m, challenge);
881 buffer_free(&m);
882
883 return (challenge);
884}
885
886int
887mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
888{
889 Buffer m;
890 u_char *blob;
891 u_int blen;
892 int success = 0;
893
894 debug3("%s entering", __FUNCTION__);
895
896 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
897 if (key_to_blob(key, &blob, &blen) == 0)
898 fatal("%s: key_to_blob failed", __FUNCTION__);
899 key->type = KEY_RSA1;
900
901 buffer_init(&m);
902 buffer_put_string(&m, blob, blen);
903 buffer_put_string(&m, response, 16);
904 xfree(blob);
905
906 mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
907 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
908
909 success = buffer_get_int(&m);
910 buffer_free(&m);
911
912 return (success);
913}