Annotation of src/usr.bin/ssh/monitor_wrap.c, Revision 1.61
1.61 ! djm 1: /* $OpenBSD: monitor_wrap.c,v 1.60 2007/10/29 04:08:08 dtucker 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;
564: u_int64_t blocks;
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.25 markus 613: packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
614: buffer_put_int(&m, seqnr);
615: buffer_put_int64(&m, blocks);
616: buffer_put_int(&m, packets);
1.26 markus 617: packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
1.25 markus 618: buffer_put_int(&m, seqnr);
619: buffer_put_int64(&m, blocks);
620: buffer_put_int(&m, packets);
1.1 provos 621:
1.8 markus 622: debug3("%s: New keys have been sent", __func__);
1.1 provos 623: skip:
624: /* More key context */
625: plen = packet_get_keycontext(MODE_OUT, NULL);
626: p = xmalloc(plen+1);
627: packet_get_keycontext(MODE_OUT, p);
628: buffer_put_string(&m, p, plen);
629: xfree(p);
630:
631: plen = packet_get_keycontext(MODE_IN, NULL);
632: p = xmalloc(plen+1);
633: packet_get_keycontext(MODE_IN, p);
634: buffer_put_string(&m, p, plen);
635: xfree(p);
636:
637: /* Compression state */
1.8 markus 638: debug3("%s: Sending compression state", __func__);
1.1 provos 639: buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
640: buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
641:
642: /* Network I/O buffers */
643: buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
644: buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
645:
1.36 avsm 646: mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
1.8 markus 647: debug3("%s: Finished sending state", __func__);
1.1 provos 648:
649: buffer_free(&m);
650: }
651:
652: int
1.42 deraadt 653: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
1.1 provos 654: {
655: Buffer m;
1.39 dtucker 656: char *p, *msg;
1.1 provos 657: int success = 0;
658:
659: buffer_init(&m);
1.7 mouring 660: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1 provos 661:
1.8 markus 662: debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7 mouring 663: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1 provos 664:
665: success = buffer_get_int(&m);
666: if (success == 0) {
1.8 markus 667: debug3("%s: pty alloc failed", __func__);
1.1 provos 668: buffer_free(&m);
669: return (0);
670: }
671: p = buffer_get_string(&m, NULL);
1.39 dtucker 672: msg = buffer_get_string(&m, NULL);
1.1 provos 673: buffer_free(&m);
674:
675: strlcpy(namebuf, p, namebuflen); /* Possible truncation */
676: xfree(p);
1.39 dtucker 677:
678: buffer_append(&loginmsg, msg, strlen(msg));
679: xfree(msg);
1.1 provos 680:
1.58 djm 681: if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
682: (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
683: fatal("%s: receive fds failed", __func__);
1.1 provos 684:
685: /* Success */
686: return (1);
687: }
688:
689: void
1.32 markus 690: mm_session_pty_cleanup2(Session *s)
1.1 provos 691: {
692: Buffer m;
693:
694: if (s->ttyfd == -1)
695: return;
696: buffer_init(&m);
697: buffer_put_cstring(&m, s->tty);
1.7 mouring 698: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1 provos 699: buffer_free(&m);
1.3 markus 700:
1.1 provos 701: /* closed dup'ed master */
702: if (close(s->ptymaster) < 0)
703: error("close(s->ptymaster): %s", strerror(errno));
704:
705: /* unlink pty from session */
706: s->ttyfd = -1;
707: }
708:
709: /* Request process termination */
710:
711: void
712: mm_terminate(void)
713: {
714: Buffer m;
715:
716: buffer_init(&m);
1.7 mouring 717: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1 provos 718: buffer_free(&m);
719: }
720:
721: int
722: mm_ssh1_session_key(BIGNUM *num)
723: {
724: int rsafail;
725: Buffer m;
726:
727: buffer_init(&m);
728: buffer_put_bignum2(&m, num);
1.7 mouring 729: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1.1 provos 730:
1.7 mouring 731: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1.1 provos 732:
733: rsafail = buffer_get_int(&m);
734: buffer_get_bignum2(&m, num);
735:
736: buffer_free(&m);
737:
738: return (rsafail);
739: }
740:
1.2 markus 741: static void
1.1 provos 742: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
743: char ***prompts, u_int **echo_on)
744: {
1.10 deraadt 745: *name = xstrdup("");
746: *infotxt = xstrdup("");
1.1 provos 747: *numprompts = 1;
1.43 djm 748: *prompts = xcalloc(*numprompts, sizeof(char *));
749: *echo_on = xcalloc(*numprompts, sizeof(u_int));
1.1 provos 750: (*echo_on)[0] = 0;
751: }
752:
753: int
754: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
755: u_int *numprompts, char ***prompts, u_int **echo_on)
756: {
757: Buffer m;
1.21 markus 758: u_int success;
1.1 provos 759: char *challenge;
760:
1.8 markus 761: debug3("%s: entering", __func__);
1.1 provos 762:
763: buffer_init(&m);
1.7 mouring 764: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1 provos 765:
1.7 mouring 766: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1 provos 767: &m);
1.21 markus 768: success = buffer_get_int(&m);
769: if (success == 0) {
1.8 markus 770: debug3("%s: no challenge", __func__);
1.1 provos 771: buffer_free(&m);
772: return (-1);
773: }
774:
775: /* Get the challenge, and format the response */
776: challenge = buffer_get_string(&m, NULL);
777: buffer_free(&m);
778:
779: mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
780: (*prompts)[0] = challenge;
781:
1.8 markus 782: debug3("%s: received challenge: %s", __func__, challenge);
1.1 provos 783:
784: return (0);
785: }
786:
787: int
788: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
789: {
790: Buffer m;
791: int authok;
792:
1.8 markus 793: debug3("%s: entering", __func__);
1.1 provos 794: if (numresponses != 1)
795: return (-1);
796:
797: buffer_init(&m);
798: buffer_put_cstring(&m, responses[0]);
1.7 mouring 799: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1 provos 800:
1.7 mouring 801: mm_request_receive_expect(pmonitor->m_recvfd,
1.1 provos 802: MONITOR_ANS_BSDAUTHRESPOND, &m);
803:
804: authok = buffer_get_int(&m);
805: buffer_free(&m);
806:
807: return ((authok == 0) ? -1 : 0);
808: }
809:
810:
811: void
812: mm_ssh1_session_id(u_char session_id[16])
813: {
814: Buffer m;
815: int i;
816:
1.8 markus 817: debug3("%s entering", __func__);
1.1 provos 818:
819: buffer_init(&m);
820: for (i = 0; i < 16; i++)
821: buffer_put_char(&m, session_id[i]);
822:
1.7 mouring 823: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1.1 provos 824: buffer_free(&m);
825: }
826:
827: int
828: mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
829: {
830: Buffer m;
831: Key *key;
832: u_char *blob;
833: u_int blen;
1.22 markus 834: int allowed = 0, have_forced = 0;
1.1 provos 835:
1.8 markus 836: debug3("%s entering", __func__);
1.1 provos 837:
838: buffer_init(&m);
839: buffer_put_bignum2(&m, client_n);
840:
1.7 mouring 841: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
842: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1.1 provos 843:
844: allowed = buffer_get_int(&m);
1.22 markus 845:
846: /* fake forced command */
847: auth_clear_options();
848: have_forced = buffer_get_int(&m);
849: forced_command = have_forced ? xstrdup("true") : NULL;
1.1 provos 850:
851: if (allowed && rkey != NULL) {
852: blob = buffer_get_string(&m, &blen);
853: if ((key = key_from_blob(blob, blen)) == NULL)
1.8 markus 854: fatal("%s: key_from_blob failed", __func__);
1.1 provos 855: *rkey = key;
856: xfree(blob);
857: }
858: mm_send_debug(&m);
859: buffer_free(&m);
860:
861: return (allowed);
862: }
863:
864: BIGNUM *
865: mm_auth_rsa_generate_challenge(Key *key)
866: {
867: Buffer m;
868: BIGNUM *challenge;
869: u_char *blob;
870: u_int blen;
871:
1.8 markus 872: debug3("%s entering", __func__);
1.1 provos 873:
874: if ((challenge = BN_new()) == NULL)
1.8 markus 875: fatal("%s: BN_new failed", __func__);
1.1 provos 876:
877: key->type = KEY_RSA; /* XXX cheat for key_to_blob */
878: if (key_to_blob(key, &blob, &blen) == 0)
1.8 markus 879: fatal("%s: key_to_blob failed", __func__);
1.1 provos 880: key->type = KEY_RSA1;
881:
882: buffer_init(&m);
883: buffer_put_string(&m, blob, blen);
884: xfree(blob);
885:
1.7 mouring 886: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
887: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1.1 provos 888:
889: buffer_get_bignum2(&m, challenge);
890: buffer_free(&m);
891:
892: return (challenge);
893: }
894:
895: int
896: mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
897: {
898: Buffer m;
899: u_char *blob;
900: u_int blen;
901: int success = 0;
902:
1.8 markus 903: debug3("%s entering", __func__);
1.1 provos 904:
905: key->type = KEY_RSA; /* XXX cheat for key_to_blob */
906: if (key_to_blob(key, &blob, &blen) == 0)
1.8 markus 907: fatal("%s: key_to_blob failed", __func__);
1.1 provos 908: key->type = KEY_RSA1;
909:
910: buffer_init(&m);
911: buffer_put_string(&m, blob, blen);
912: buffer_put_string(&m, response, 16);
913: xfree(blob);
914:
1.7 mouring 915: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
916: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1.1 provos 917:
918: success = buffer_get_int(&m);
919: buffer_free(&m);
920:
921: return (success);
922: }
1.29 markus 923:
924: #ifdef GSSAPI
925: OM_uint32
1.36 avsm 926: mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1.29 markus 927: {
928: Buffer m;
929: OM_uint32 major;
930:
931: /* Client doesn't get to see the context */
932: *ctx = NULL;
933:
934: buffer_init(&m);
1.36 avsm 935: buffer_put_string(&m, goid->elements, goid->length);
1.29 markus 936:
937: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
938: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
939:
940: major = buffer_get_int(&m);
941:
942: buffer_free(&m);
943: return (major);
944: }
945:
946: OM_uint32
947: mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
948: gss_buffer_desc *out, OM_uint32 *flags)
949: {
950: Buffer m;
951: OM_uint32 major;
1.30 deraadt 952: u_int len;
1.29 markus 953:
954: buffer_init(&m);
955: buffer_put_string(&m, in->value, in->length);
956:
957: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
958: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
959:
960: major = buffer_get_int(&m);
1.30 deraadt 961: out->value = buffer_get_string(&m, &len);
962: out->length = len;
1.29 markus 963: if (flags)
964: *flags = buffer_get_int(&m);
965:
966: buffer_free(&m);
967:
968: return (major);
1.35 markus 969: }
970:
971: OM_uint32
972: mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
973: {
974: Buffer m;
975: OM_uint32 major;
976:
977: buffer_init(&m);
978: buffer_put_string(&m, gssbuf->value, gssbuf->length);
979: buffer_put_string(&m, gssmic->value, gssmic->length);
980:
981: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
982: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
983: &m);
984:
985: major = buffer_get_int(&m);
986: buffer_free(&m);
987: return(major);
1.29 markus 988: }
989:
990: int
991: mm_ssh_gssapi_userok(char *user)
992: {
993: Buffer m;
994: int authenticated = 0;
995:
996: buffer_init(&m);
997:
998: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
999: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1000: &m);
1001:
1002: authenticated = buffer_get_int(&m);
1003:
1004: buffer_free(&m);
1005: debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1006: return (authenticated);
1007: }
1008: #endif /* GSSAPI */