Annotation of src/usr.bin/ssh/monitor_wrap.c, Revision 1.59
1.59 ! djm 1: /* $OpenBSD: monitor_wrap.c,v 1.58 2007/09/04 03:21:03 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) {
215: buffer_free(&m);
216: return (NULL);
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:
228: /* copy options block as a Match directive may have changed some */
229: newopts = buffer_get_string(&m, &len);
230: if (len != sizeof(*newopts))
231: fatal("%s: option block size mismatch", __func__);
232: if (newopts->banner != NULL)
233: newopts->banner = buffer_get_string(&m, NULL);
234: copy_set_server_options(&options, newopts, 1);
235: xfree(newopts);
236:
1.1 provos 237: buffer_free(&m);
238:
239: return (pw);
1.6 djm 240: }
241:
1.33 markus 242: char *
243: mm_auth2_read_banner(void)
1.6 djm 244: {
245: Buffer m;
246: char *banner;
247:
1.8 markus 248: debug3("%s entering", __func__);
1.6 djm 249:
250: buffer_init(&m);
1.7 mouring 251: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
1.6 djm 252: buffer_clear(&m);
253:
1.33 markus 254: mm_request_receive_expect(pmonitor->m_recvfd,
255: MONITOR_ANS_AUTH2_READ_BANNER, &m);
1.6 djm 256: banner = buffer_get_string(&m, NULL);
257: buffer_free(&m);
1.10 deraadt 258:
1.33 markus 259: /* treat empty banner as missing banner */
260: if (strlen(banner) == 0) {
261: xfree(banner);
262: banner = NULL;
263: }
1.6 djm 264: return (banner);
1.1 provos 265: }
266:
267: /* Inform the privileged process about service and style */
268:
269: void
270: mm_inform_authserv(char *service, char *style)
271: {
272: Buffer m;
273:
1.8 markus 274: debug3("%s entering", __func__);
1.1 provos 275:
276: buffer_init(&m);
277: buffer_put_cstring(&m, service);
278: buffer_put_cstring(&m, style ? style : "");
279:
1.7 mouring 280: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
1.1 provos 281:
282: buffer_free(&m);
283: }
284:
285: /* Do the password authentication */
286: int
287: mm_auth_password(Authctxt *authctxt, char *password)
288: {
289: Buffer m;
290: int authenticated = 0;
291:
1.8 markus 292: debug3("%s entering", __func__);
1.1 provos 293:
294: buffer_init(&m);
295: buffer_put_cstring(&m, password);
1.7 mouring 296: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
1.1 provos 297:
1.8 markus 298: debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
1.7 mouring 299: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
1.1 provos 300:
301: authenticated = buffer_get_int(&m);
302:
303: buffer_free(&m);
304:
1.3 markus 305: debug3("%s: user %sauthenticated",
1.8 markus 306: __func__, authenticated ? "" : "not ");
1.1 provos 307: return (authenticated);
308: }
309:
310: int
311: mm_user_key_allowed(struct passwd *pw, Key *key)
312: {
313: return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
314: }
315:
316: int
317: mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
318: Key *key)
319: {
320: return (mm_key_allowed(MM_HOSTKEY, user, host, key));
321: }
322:
323: int
324: mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
325: char *host, Key *key)
326: {
327: int ret;
328:
329: key->type = KEY_RSA; /* XXX hack for key_to_blob */
330: ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
331: key->type = KEY_RSA1;
332: return (ret);
333: }
334:
1.2 markus 335: static void
1.1 provos 336: mm_send_debug(Buffer *m)
337: {
338: char *msg;
339:
340: while (buffer_len(m)) {
341: msg = buffer_get_string(m, NULL);
1.8 markus 342: debug3("%s: Sending debug: %s", __func__, msg);
1.1 provos 343: packet_send_debug("%s", msg);
344: xfree(msg);
345: }
346: }
347:
348: int
349: mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
350: {
351: Buffer m;
352: u_char *blob;
353: u_int len;
1.22 markus 354: int allowed = 0, have_forced = 0;
1.1 provos 355:
1.8 markus 356: debug3("%s entering", __func__);
1.1 provos 357:
358: /* Convert the key to a blob and the pass it over */
359: if (!key_to_blob(key, &blob, &len))
360: return (0);
361:
362: buffer_init(&m);
363: buffer_put_int(&m, type);
364: buffer_put_cstring(&m, user ? user : "");
365: buffer_put_cstring(&m, host ? host : "");
366: buffer_put_string(&m, blob, len);
367: xfree(blob);
368:
1.7 mouring 369: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
1.1 provos 370:
1.8 markus 371: debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
1.7 mouring 372: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
1.1 provos 373:
374: allowed = buffer_get_int(&m);
375:
1.22 markus 376: /* fake forced command */
377: auth_clear_options();
378: have_forced = buffer_get_int(&m);
379: forced_command = have_forced ? xstrdup("true") : NULL;
380:
1.1 provos 381: /* Send potential debug messages */
382: mm_send_debug(&m);
383:
384: buffer_free(&m);
385:
386: return (allowed);
387: }
388:
1.3 markus 389: /*
1.1 provos 390: * This key verify needs to send the key type along, because the
391: * privileged parent makes the decision if the key is allowed
392: * for authentication.
393: */
394:
395: int
396: mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
397: {
398: Buffer m;
399: u_char *blob;
400: u_int len;
401: int verified = 0;
402:
1.8 markus 403: debug3("%s entering", __func__);
1.1 provos 404:
405: /* Convert the key to a blob and the pass it over */
406: if (!key_to_blob(key, &blob, &len))
407: return (0);
408:
409: buffer_init(&m);
410: buffer_put_string(&m, blob, len);
411: buffer_put_string(&m, sig, siglen);
412: buffer_put_string(&m, data, datalen);
413: xfree(blob);
414:
1.7 mouring 415: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
1.1 provos 416:
1.8 markus 417: debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
1.7 mouring 418: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
1.1 provos 419:
420: verified = buffer_get_int(&m);
421:
422: buffer_free(&m);
423:
424: return (verified);
425: }
426:
427: /* Export key state after authentication */
428: Newkeys *
429: mm_newkeys_from_blob(u_char *blob, int blen)
430: {
431: Buffer b;
432: u_int len;
433: Newkeys *newkey = NULL;
434: Enc *enc;
435: Mac *mac;
436: Comp *comp;
437:
1.8 markus 438: debug3("%s: %p(%d)", __func__, blob, blen);
1.1 provos 439: #ifdef DEBUG_PK
440: dump_base64(stderr, blob, blen);
441: #endif
442: buffer_init(&b);
443: buffer_append(&b, blob, blen);
444:
445: newkey = xmalloc(sizeof(*newkey));
446: enc = &newkey->enc;
447: mac = &newkey->mac;
448: comp = &newkey->comp;
449:
450: /* Enc structure */
451: enc->name = buffer_get_string(&b, NULL);
452: buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
453: enc->enabled = buffer_get_int(&b);
454: enc->block_size = buffer_get_int(&b);
455: enc->key = buffer_get_string(&b, &enc->key_len);
456: enc->iv = buffer_get_string(&b, &len);
457: if (len != enc->block_size)
1.12 deraadt 458: fatal("%s: bad ivlen: expected %u != %u", __func__,
1.1 provos 459: enc->block_size, len);
460:
461: if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
1.8 markus 462: fatal("%s: bad cipher name %s or pointer %p", __func__,
1.1 provos 463: enc->name, enc->cipher);
464:
465: /* Mac structure */
466: mac->name = buffer_get_string(&b, NULL);
1.56 djm 467: if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
1.57 pvalchev 468: fatal("%s: can not setup mac %s", __func__, mac->name);
1.1 provos 469: mac->enabled = buffer_get_int(&b);
470: mac->key = buffer_get_string(&b, &len);
471: if (len > mac->key_len)
1.12 deraadt 472: fatal("%s: bad mac key length: %u > %d", __func__, len,
1.1 provos 473: mac->key_len);
474: mac->key_len = len;
475:
476: /* Comp structure */
477: comp->type = buffer_get_int(&b);
478: comp->enabled = buffer_get_int(&b);
479: comp->name = buffer_get_string(&b, NULL);
480:
481: len = buffer_len(&b);
482: if (len != 0)
1.12 deraadt 483: error("newkeys_from_blob: remaining bytes in blob %u", len);
1.1 provos 484: buffer_free(&b);
485: return (newkey);
486: }
487:
488: int
489: mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
490: {
491: Buffer b;
492: int len;
493: Enc *enc;
494: Mac *mac;
495: Comp *comp;
496: Newkeys *newkey = newkeys[mode];
497:
1.8 markus 498: debug3("%s: converting %p", __func__, newkey);
1.1 provos 499:
500: if (newkey == NULL) {
1.8 markus 501: error("%s: newkey == NULL", __func__);
1.1 provos 502: return 0;
503: }
504: enc = &newkey->enc;
505: mac = &newkey->mac;
506: comp = &newkey->comp;
507:
508: buffer_init(&b);
509: /* Enc structure */
510: buffer_put_cstring(&b, enc->name);
511: /* The cipher struct is constant and shared, you export pointer */
512: buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
513: buffer_put_int(&b, enc->enabled);
514: buffer_put_int(&b, enc->block_size);
515: buffer_put_string(&b, enc->key, enc->key_len);
516: packet_get_keyiv(mode, enc->iv, enc->block_size);
517: buffer_put_string(&b, enc->iv, enc->block_size);
518:
519: /* Mac structure */
520: buffer_put_cstring(&b, mac->name);
521: buffer_put_int(&b, mac->enabled);
522: buffer_put_string(&b, mac->key, mac->key_len);
523:
524: /* Comp structure */
525: buffer_put_int(&b, comp->type);
526: buffer_put_int(&b, comp->enabled);
527: buffer_put_cstring(&b, comp->name);
528:
529: len = buffer_len(&b);
1.16 markus 530: if (lenp != NULL)
531: *lenp = len;
532: if (blobp != NULL) {
533: *blobp = xmalloc(len);
534: memcpy(*blobp, buffer_ptr(&b), len);
535: }
1.1 provos 536: memset(buffer_ptr(&b), 0, len);
537: buffer_free(&b);
538: return len;
539: }
540:
1.2 markus 541: static void
1.1 provos 542: mm_send_kex(Buffer *m, Kex *kex)
543: {
544: buffer_put_string(m, kex->session_id, kex->session_id_len);
545: buffer_put_int(m, kex->we_need);
546: buffer_put_int(m, kex->hostkey_type);
547: buffer_put_int(m, kex->kex_type);
548: buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
549: buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
550: buffer_put_int(m, kex->flags);
551: buffer_put_cstring(m, kex->client_version_string);
552: buffer_put_cstring(m, kex->server_version_string);
553: }
554:
555: void
1.36 avsm 556: mm_send_keystate(struct monitor *monitor)
1.1 provos 557: {
558: Buffer m;
559: u_char *blob, *p;
560: u_int bloblen, plen;
1.25 markus 561: u_int32_t seqnr, packets;
562: u_int64_t blocks;
1.1 provos 563:
564: buffer_init(&m);
565:
566: if (!compat20) {
567: u_char iv[24];
1.11 markus 568: u_char *key;
569: u_int ivlen, keylen;
1.1 provos 570:
571: buffer_put_int(&m, packet_get_protocol_flags());
572:
573: buffer_put_int(&m, packet_get_ssh1_cipher());
574:
1.11 markus 575: debug3("%s: Sending ssh1 KEY+IV", __func__);
576: keylen = packet_get_encryption_key(NULL);
577: key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
578: keylen = packet_get_encryption_key(key);
579: buffer_put_string(&m, key, keylen);
580: memset(key, 0, keylen);
581: xfree(key);
582:
1.1 provos 583: ivlen = packet_get_keyiv_len(MODE_OUT);
584: packet_get_keyiv(MODE_OUT, iv, ivlen);
585: buffer_put_string(&m, iv, ivlen);
586: ivlen = packet_get_keyiv_len(MODE_OUT);
587: packet_get_keyiv(MODE_IN, iv, ivlen);
588: buffer_put_string(&m, iv, ivlen);
589: goto skip;
590: } else {
591: /* Kex for rekeying */
1.36 avsm 592: mm_send_kex(&m, *monitor->m_pkex);
1.1 provos 593: }
594:
595: debug3("%s: Sending new keys: %p %p",
1.8 markus 596: __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
1.1 provos 597:
598: /* Keys from Kex */
599: if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
1.8 markus 600: fatal("%s: conversion of newkeys failed", __func__);
1.1 provos 601:
602: buffer_put_string(&m, blob, bloblen);
603: xfree(blob);
604:
605: if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
1.8 markus 606: fatal("%s: conversion of newkeys failed", __func__);
1.1 provos 607:
608: buffer_put_string(&m, blob, bloblen);
609: xfree(blob);
610:
1.25 markus 611: packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
612: buffer_put_int(&m, seqnr);
613: buffer_put_int64(&m, blocks);
614: buffer_put_int(&m, packets);
1.26 markus 615: packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
1.25 markus 616: buffer_put_int(&m, seqnr);
617: buffer_put_int64(&m, blocks);
618: buffer_put_int(&m, packets);
1.1 provos 619:
1.8 markus 620: debug3("%s: New keys have been sent", __func__);
1.1 provos 621: skip:
622: /* More key context */
623: plen = packet_get_keycontext(MODE_OUT, NULL);
624: p = xmalloc(plen+1);
625: packet_get_keycontext(MODE_OUT, p);
626: buffer_put_string(&m, p, plen);
627: xfree(p);
628:
629: plen = packet_get_keycontext(MODE_IN, NULL);
630: p = xmalloc(plen+1);
631: packet_get_keycontext(MODE_IN, p);
632: buffer_put_string(&m, p, plen);
633: xfree(p);
634:
635: /* Compression state */
1.8 markus 636: debug3("%s: Sending compression state", __func__);
1.1 provos 637: buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
638: buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
639:
640: /* Network I/O buffers */
641: buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
642: buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
643:
1.36 avsm 644: mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
1.8 markus 645: debug3("%s: Finished sending state", __func__);
1.1 provos 646:
647: buffer_free(&m);
648: }
649:
650: int
1.42 deraadt 651: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
1.1 provos 652: {
653: Buffer m;
1.39 dtucker 654: char *p, *msg;
1.1 provos 655: int success = 0;
656:
657: buffer_init(&m);
1.7 mouring 658: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1 provos 659:
1.8 markus 660: debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7 mouring 661: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1 provos 662:
663: success = buffer_get_int(&m);
664: if (success == 0) {
1.8 markus 665: debug3("%s: pty alloc failed", __func__);
1.1 provos 666: buffer_free(&m);
667: return (0);
668: }
669: p = buffer_get_string(&m, NULL);
1.39 dtucker 670: msg = buffer_get_string(&m, NULL);
1.1 provos 671: buffer_free(&m);
672:
673: strlcpy(namebuf, p, namebuflen); /* Possible truncation */
674: xfree(p);
1.39 dtucker 675:
676: buffer_append(&loginmsg, msg, strlen(msg));
677: xfree(msg);
1.1 provos 678:
1.58 djm 679: if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
680: (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
681: fatal("%s: receive fds failed", __func__);
1.1 provos 682:
683: /* Success */
684: return (1);
685: }
686:
687: void
1.32 markus 688: mm_session_pty_cleanup2(Session *s)
1.1 provos 689: {
690: Buffer m;
691:
692: if (s->ttyfd == -1)
693: return;
694: buffer_init(&m);
695: buffer_put_cstring(&m, s->tty);
1.7 mouring 696: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1 provos 697: buffer_free(&m);
1.3 markus 698:
1.1 provos 699: /* closed dup'ed master */
700: if (close(s->ptymaster) < 0)
701: error("close(s->ptymaster): %s", strerror(errno));
702:
703: /* unlink pty from session */
704: s->ttyfd = -1;
705: }
706:
707: /* Request process termination */
708:
709: void
710: mm_terminate(void)
711: {
712: Buffer m;
713:
714: buffer_init(&m);
1.7 mouring 715: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1 provos 716: buffer_free(&m);
717: }
718:
719: int
720: mm_ssh1_session_key(BIGNUM *num)
721: {
722: int rsafail;
723: Buffer m;
724:
725: buffer_init(&m);
726: buffer_put_bignum2(&m, num);
1.7 mouring 727: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1.1 provos 728:
1.7 mouring 729: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1.1 provos 730:
731: rsafail = buffer_get_int(&m);
732: buffer_get_bignum2(&m, num);
733:
734: buffer_free(&m);
735:
736: return (rsafail);
737: }
738:
1.2 markus 739: static void
1.1 provos 740: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
741: char ***prompts, u_int **echo_on)
742: {
1.10 deraadt 743: *name = xstrdup("");
744: *infotxt = xstrdup("");
1.1 provos 745: *numprompts = 1;
1.43 djm 746: *prompts = xcalloc(*numprompts, sizeof(char *));
747: *echo_on = xcalloc(*numprompts, sizeof(u_int));
1.1 provos 748: (*echo_on)[0] = 0;
749: }
750:
751: int
752: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
753: u_int *numprompts, char ***prompts, u_int **echo_on)
754: {
755: Buffer m;
1.21 markus 756: u_int success;
1.1 provos 757: char *challenge;
758:
1.8 markus 759: debug3("%s: entering", __func__);
1.1 provos 760:
761: buffer_init(&m);
1.7 mouring 762: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1 provos 763:
1.7 mouring 764: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1 provos 765: &m);
1.21 markus 766: success = buffer_get_int(&m);
767: if (success == 0) {
1.8 markus 768: debug3("%s: no challenge", __func__);
1.1 provos 769: buffer_free(&m);
770: return (-1);
771: }
772:
773: /* Get the challenge, and format the response */
774: challenge = buffer_get_string(&m, NULL);
775: buffer_free(&m);
776:
777: mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
778: (*prompts)[0] = challenge;
779:
1.8 markus 780: debug3("%s: received challenge: %s", __func__, challenge);
1.1 provos 781:
782: return (0);
783: }
784:
785: int
786: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
787: {
788: Buffer m;
789: int authok;
790:
1.8 markus 791: debug3("%s: entering", __func__);
1.1 provos 792: if (numresponses != 1)
793: return (-1);
794:
795: buffer_init(&m);
796: buffer_put_cstring(&m, responses[0]);
1.7 mouring 797: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1 provos 798:
1.7 mouring 799: mm_request_receive_expect(pmonitor->m_recvfd,
1.1 provos 800: MONITOR_ANS_BSDAUTHRESPOND, &m);
801:
802: authok = buffer_get_int(&m);
803: buffer_free(&m);
804:
805: return ((authok == 0) ? -1 : 0);
806: }
807:
808:
809: void
810: mm_ssh1_session_id(u_char session_id[16])
811: {
812: Buffer m;
813: int i;
814:
1.8 markus 815: debug3("%s entering", __func__);
1.1 provos 816:
817: buffer_init(&m);
818: for (i = 0; i < 16; i++)
819: buffer_put_char(&m, session_id[i]);
820:
1.7 mouring 821: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1.1 provos 822: buffer_free(&m);
823: }
824:
825: int
826: mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
827: {
828: Buffer m;
829: Key *key;
830: u_char *blob;
831: u_int blen;
1.22 markus 832: int allowed = 0, have_forced = 0;
1.1 provos 833:
1.8 markus 834: debug3("%s entering", __func__);
1.1 provos 835:
836: buffer_init(&m);
837: buffer_put_bignum2(&m, client_n);
838:
1.7 mouring 839: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
840: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1.1 provos 841:
842: allowed = buffer_get_int(&m);
1.22 markus 843:
844: /* fake forced command */
845: auth_clear_options();
846: have_forced = buffer_get_int(&m);
847: forced_command = have_forced ? xstrdup("true") : NULL;
1.1 provos 848:
849: if (allowed && rkey != NULL) {
850: blob = buffer_get_string(&m, &blen);
851: if ((key = key_from_blob(blob, blen)) == NULL)
1.8 markus 852: fatal("%s: key_from_blob failed", __func__);
1.1 provos 853: *rkey = key;
854: xfree(blob);
855: }
856: mm_send_debug(&m);
857: buffer_free(&m);
858:
859: return (allowed);
860: }
861:
862: BIGNUM *
863: mm_auth_rsa_generate_challenge(Key *key)
864: {
865: Buffer m;
866: BIGNUM *challenge;
867: u_char *blob;
868: u_int blen;
869:
1.8 markus 870: debug3("%s entering", __func__);
1.1 provos 871:
872: if ((challenge = BN_new()) == NULL)
1.8 markus 873: fatal("%s: BN_new failed", __func__);
1.1 provos 874:
875: key->type = KEY_RSA; /* XXX cheat for key_to_blob */
876: if (key_to_blob(key, &blob, &blen) == 0)
1.8 markus 877: fatal("%s: key_to_blob failed", __func__);
1.1 provos 878: key->type = KEY_RSA1;
879:
880: buffer_init(&m);
881: buffer_put_string(&m, blob, blen);
882: xfree(blob);
883:
1.7 mouring 884: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
885: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1.1 provos 886:
887: buffer_get_bignum2(&m, challenge);
888: buffer_free(&m);
889:
890: return (challenge);
891: }
892:
893: int
894: mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
895: {
896: Buffer m;
897: u_char *blob;
898: u_int blen;
899: int success = 0;
900:
1.8 markus 901: debug3("%s entering", __func__);
1.1 provos 902:
903: key->type = KEY_RSA; /* XXX cheat for key_to_blob */
904: if (key_to_blob(key, &blob, &blen) == 0)
1.8 markus 905: fatal("%s: key_to_blob failed", __func__);
1.1 provos 906: key->type = KEY_RSA1;
907:
908: buffer_init(&m);
909: buffer_put_string(&m, blob, blen);
910: buffer_put_string(&m, response, 16);
911: xfree(blob);
912:
1.7 mouring 913: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
914: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1.1 provos 915:
916: success = buffer_get_int(&m);
917: buffer_free(&m);
918:
919: return (success);
920: }
1.29 markus 921:
922: #ifdef GSSAPI
923: OM_uint32
1.36 avsm 924: mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1.29 markus 925: {
926: Buffer m;
927: OM_uint32 major;
928:
929: /* Client doesn't get to see the context */
930: *ctx = NULL;
931:
932: buffer_init(&m);
1.36 avsm 933: buffer_put_string(&m, goid->elements, goid->length);
1.29 markus 934:
935: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
936: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
937:
938: major = buffer_get_int(&m);
939:
940: buffer_free(&m);
941: return (major);
942: }
943:
944: OM_uint32
945: mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
946: gss_buffer_desc *out, OM_uint32 *flags)
947: {
948: Buffer m;
949: OM_uint32 major;
1.30 deraadt 950: u_int len;
1.29 markus 951:
952: buffer_init(&m);
953: buffer_put_string(&m, in->value, in->length);
954:
955: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
956: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
957:
958: major = buffer_get_int(&m);
1.30 deraadt 959: out->value = buffer_get_string(&m, &len);
960: out->length = len;
1.29 markus 961: if (flags)
962: *flags = buffer_get_int(&m);
963:
964: buffer_free(&m);
965:
966: return (major);
1.35 markus 967: }
968:
969: OM_uint32
970: mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
971: {
972: Buffer m;
973: OM_uint32 major;
974:
975: buffer_init(&m);
976: buffer_put_string(&m, gssbuf->value, gssbuf->length);
977: buffer_put_string(&m, gssmic->value, gssmic->length);
978:
979: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
980: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
981: &m);
982:
983: major = buffer_get_int(&m);
984: buffer_free(&m);
985: return(major);
1.29 markus 986: }
987:
988: int
989: mm_ssh_gssapi_userok(char *user)
990: {
991: Buffer m;
992: int authenticated = 0;
993:
994: buffer_init(&m);
995:
996: mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
997: mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
998: &m);
999:
1000: authenticated = buffer_get_int(&m);
1001:
1002: buffer_free(&m);
1003: debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1004: return (authenticated);
1005: }
1006: #endif /* GSSAPI */