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