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