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