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