xref: /openbsd/usr.bin/ssh/monitor_wrap.c (revision 71f11376)
1 /* $OpenBSD: monitor_wrap.c,v 1.130 2024/05/17 00:30:24 djm Exp $ */
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 
28 #include <sys/types.h>
29 #include <sys/uio.h>
30 #include <sys/queue.h>
31 
32 #include <errno.h>
33 #include <pwd.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 
40 #ifdef WITH_OPENSSL
41 #include <openssl/bn.h>
42 #include <openssl/dh.h>
43 #endif
44 
45 #include "xmalloc.h"
46 #include "ssh.h"
47 #ifdef WITH_OPENSSL
48 #include "dh.h"
49 #endif
50 #include "sshbuf.h"
51 #include "sshkey.h"
52 #include "cipher.h"
53 #include "kex.h"
54 #include "hostfile.h"
55 #include "auth.h"
56 #include "auth-options.h"
57 #include "packet.h"
58 #include "mac.h"
59 #include "log.h"
60 #include "monitor.h"
61 #ifdef GSSAPI
62 #include "ssh-gss.h"
63 #endif
64 #include "atomicio.h"
65 #include "monitor_fdpass.h"
66 #include "misc.h"
67 
68 #include "channels.h"
69 #include "session.h"
70 #include "servconf.h"
71 #include "monitor_wrap.h"
72 
73 #include "ssherr.h"
74 
75 /* Imports */
76 extern struct monitor *pmonitor;
77 extern struct sshbuf *loginmsg;
78 extern ServerOptions options;
79 
80 void
mm_log_handler(LogLevel level,int forced,const char * msg,void * ctx)81 mm_log_handler(LogLevel level, int forced, const char *msg, void *ctx)
82 {
83 	struct sshbuf *log_msg;
84 	struct monitor *mon = (struct monitor *)ctx;
85 	int r;
86 	size_t len;
87 
88 	if (mon->m_log_sendfd == -1)
89 		fatal_f("no log channel");
90 
91 	if ((log_msg = sshbuf_new()) == NULL)
92 		fatal_f("sshbuf_new failed");
93 
94 	if ((r = sshbuf_put_u32(log_msg, 0)) != 0 || /* length; filled below */
95 	    (r = sshbuf_put_u32(log_msg, level)) != 0 ||
96 	    (r = sshbuf_put_u32(log_msg, forced)) != 0 ||
97 	    (r = sshbuf_put_cstring(log_msg, msg)) != 0)
98 		fatal_fr(r, "assemble");
99 	if ((len = sshbuf_len(log_msg)) < 4 || len > 0xffffffff)
100 		fatal_f("bad length %zu", len);
101 	POKE_U32(sshbuf_mutable_ptr(log_msg), len - 4);
102 	if (atomicio(vwrite, mon->m_log_sendfd,
103 	    sshbuf_mutable_ptr(log_msg), len) != len)
104 		fatal_f("write: %s", strerror(errno));
105 	sshbuf_free(log_msg);
106 }
107 
108 int
mm_is_monitor(void)109 mm_is_monitor(void)
110 {
111 	/*
112 	 * m_pid is only set in the privileged part, and
113 	 * points to the unprivileged child.
114 	 */
115 	return (pmonitor && pmonitor->m_pid > 0);
116 }
117 
118 void
mm_request_send(int sock,enum monitor_reqtype type,struct sshbuf * m)119 mm_request_send(int sock, enum monitor_reqtype type, struct sshbuf *m)
120 {
121 	size_t mlen = sshbuf_len(m);
122 	u_char buf[5];
123 
124 	debug3_f("entering, type %d", type);
125 
126 	if (mlen >= 0xffffffff)
127 		fatal_f("bad length %zu", mlen);
128 	POKE_U32(buf, mlen + 1);
129 	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
130 	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
131 		fatal_f("write: %s", strerror(errno));
132 	if (atomicio(vwrite, sock, sshbuf_mutable_ptr(m), mlen) != mlen)
133 		fatal_f("write: %s", strerror(errno));
134 }
135 
136 void
mm_request_receive(int sock,struct sshbuf * m)137 mm_request_receive(int sock, struct sshbuf *m)
138 {
139 	u_char buf[4], *p = NULL;
140 	u_int msg_len;
141 	int r;
142 
143 	debug3_f("entering");
144 
145 	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
146 		if (errno == EPIPE) {
147 			debug3_f("monitor fd closed");
148 			cleanup_exit(255);
149 		}
150 		fatal_f("read: %s", strerror(errno));
151 	}
152 	msg_len = PEEK_U32(buf);
153 	if (msg_len > 256 * 1024)
154 		fatal_f("read: bad msg_len %d", msg_len);
155 	sshbuf_reset(m);
156 	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
157 		fatal_fr(r, "reserve");
158 	if (atomicio(read, sock, p, msg_len) != msg_len)
159 		fatal_f("read: %s", strerror(errno));
160 }
161 
162 void
mm_request_receive_expect(int sock,enum monitor_reqtype type,struct sshbuf * m)163 mm_request_receive_expect(int sock, enum monitor_reqtype type, struct sshbuf *m)
164 {
165 	u_char rtype;
166 	int r;
167 
168 	debug3_f("entering, type %d", type);
169 
170 	mm_request_receive(sock, m);
171 	if ((r = sshbuf_get_u8(m, &rtype)) != 0)
172 		fatal_fr(r, "parse");
173 	if (rtype != type)
174 		fatal_f("read: rtype %d != type %d", rtype, type);
175 }
176 
177 #ifdef WITH_OPENSSL
178 DH *
mm_choose_dh(int min,int nbits,int max)179 mm_choose_dh(int min, int nbits, int max)
180 {
181 	BIGNUM *p, *g;
182 	int r;
183 	u_char success = 0;
184 	struct sshbuf *m;
185 
186 	if ((m = sshbuf_new()) == NULL)
187 		fatal_f("sshbuf_new failed");
188 	if ((r = sshbuf_put_u32(m, min)) != 0 ||
189 	    (r = sshbuf_put_u32(m, nbits)) != 0 ||
190 	    (r = sshbuf_put_u32(m, max)) != 0)
191 		fatal_fr(r, "assemble");
192 
193 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, m);
194 
195 	debug3_f("waiting for MONITOR_ANS_MODULI");
196 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, m);
197 
198 	if ((r = sshbuf_get_u8(m, &success)) != 0)
199 		fatal_fr(r, "parse success");
200 	if (success == 0)
201 		fatal_f("MONITOR_ANS_MODULI failed");
202 
203 	if ((r = sshbuf_get_bignum2(m, &p)) != 0 ||
204 	    (r = sshbuf_get_bignum2(m, &g)) != 0)
205 		fatal_fr(r, "parse group");
206 
207 	debug3_f("remaining %zu", sshbuf_len(m));
208 	sshbuf_free(m);
209 
210 	return (dh_new_group(g, p));
211 }
212 #endif
213 
214 int
mm_sshkey_sign(struct ssh * ssh,struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,const char * hostkey_alg,const char * sk_provider,const char * sk_pin,u_int compat)215 mm_sshkey_sign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
216     const u_char *data, size_t datalen, const char *hostkey_alg,
217     const char *sk_provider, const char *sk_pin, u_int compat)
218 {
219 	struct kex *kex = *pmonitor->m_pkex;
220 	struct sshbuf *m;
221 	u_int ndx = kex->host_key_index(key, 0, ssh);
222 	int r;
223 
224 	debug3_f("entering");
225 	if ((m = sshbuf_new()) == NULL)
226 		fatal_f("sshbuf_new failed");
227 	if ((r = sshbuf_put_u32(m, ndx)) != 0 ||
228 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
229 	    (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
230 	    (r = sshbuf_put_u32(m, compat)) != 0)
231 		fatal_fr(r, "assemble");
232 
233 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
234 
235 	debug3_f("waiting for MONITOR_ANS_SIGN");
236 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
237 	if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
238 		fatal_fr(r, "parse");
239 	sshbuf_free(m);
240 
241 	return (0);
242 }
243 
244 void
mm_decode_activate_server_options(struct ssh * ssh,struct sshbuf * m)245 mm_decode_activate_server_options(struct ssh *ssh, struct sshbuf *m)
246 {
247 	const u_char *p;
248 	size_t len;
249 	u_int i;
250 	ServerOptions *newopts;
251 	int r;
252 
253 	if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
254 		fatal_fr(r, "parse opts");
255 	if (len != sizeof(*newopts))
256 		fatal_f("option block size mismatch");
257 	newopts = xcalloc(sizeof(*newopts), 1);
258 	memcpy(newopts, p, sizeof(*newopts));
259 
260 #define M_CP_STROPT(x) do { \
261 		if (newopts->x != NULL && \
262 		    (r = sshbuf_get_cstring(m, &newopts->x, NULL)) != 0) \
263 			fatal_fr(r, "parse %s", #x); \
264 	} while (0)
265 #define M_CP_STRARRAYOPT(x, nx) do { \
266 		newopts->x = newopts->nx == 0 ? \
267 		    NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
268 		for (i = 0; i < newopts->nx; i++) { \
269 			if ((r = sshbuf_get_cstring(m, \
270 			    &newopts->x[i], NULL)) != 0) \
271 				fatal_fr(r, "parse %s", #x); \
272 		} \
273 	} while (0)
274 	/* See comment in servconf.h */
275 	COPY_MATCH_STRING_OPTS();
276 #undef M_CP_STROPT
277 #undef M_CP_STRARRAYOPT
278 
279 	copy_set_server_options(&options, newopts, 1);
280 	log_change_level(options.log_level);
281 	log_verbose_reset();
282 	for (i = 0; i < options.num_log_verbose; i++)
283 		log_verbose_add(options.log_verbose[i]);
284 	free(newopts);
285 }
286 
287 #define GETPW(b, id) \
288 	do { \
289 		if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0) \
290 			fatal_fr(r, "parse pw %s", #id); \
291 		if (len != sizeof(pw->id)) \
292 			fatal_fr(r, "bad length for %s", #id); \
293 		memcpy(&pw->id, p, len); \
294 	} while (0)
295 
296 struct passwd *
mm_getpwnamallow(struct ssh * ssh,const char * username)297 mm_getpwnamallow(struct ssh *ssh, const char *username)
298 {
299 	struct sshbuf *m;
300 	struct passwd *pw;
301 	size_t len;
302 	int r;
303 	u_char ok;
304 	const u_char *p;
305 
306 	debug3_f("entering");
307 
308 	if ((m = sshbuf_new()) == NULL)
309 		fatal_f("sshbuf_new failed");
310 	if ((r = sshbuf_put_cstring(m, username)) != 0)
311 		fatal_fr(r, "assemble");
312 
313 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
314 
315 	debug3_f("waiting for MONITOR_ANS_PWNAM");
316 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
317 
318 	if ((r = sshbuf_get_u8(m, &ok)) != 0)
319 		fatal_fr(r, "parse success");
320 	if (ok == 0) {
321 		pw = NULL;
322 		goto out;
323 	}
324 
325 	pw = xcalloc(sizeof(*pw), 1);
326 	GETPW(m, pw_uid);
327 	GETPW(m, pw_gid);
328 	GETPW(m, pw_change);
329 	GETPW(m, pw_expire);
330 	if ((r = sshbuf_get_cstring(m, &pw->pw_name, NULL)) != 0 ||
331 	    (r = sshbuf_get_cstring(m, &pw->pw_passwd, NULL)) != 0 ||
332 	    (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 ||
333 	    (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 ||
334 	    (r = sshbuf_get_cstring(m, &pw->pw_dir, NULL)) != 0 ||
335 	    (r = sshbuf_get_cstring(m, &pw->pw_shell, NULL)) != 0)
336 		fatal_fr(r, "parse pw");
337 
338 out:
339 	/* copy options block as a Match directive may have changed some */
340 	mm_decode_activate_server_options(ssh, m);
341 	server_process_permitopen(ssh);
342 	server_process_channel_timeouts(ssh);
343 	kex_set_server_sig_algs(ssh, options.pubkey_accepted_algos);
344 	sshbuf_free(m);
345 
346 	return (pw);
347 }
348 
349 char *
mm_auth2_read_banner(void)350 mm_auth2_read_banner(void)
351 {
352 	struct sshbuf *m;
353 	char *banner;
354 	int r;
355 
356 	debug3_f("entering");
357 
358 	if ((m = sshbuf_new()) == NULL)
359 		fatal_f("sshbuf_new failed");
360 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
361 	sshbuf_reset(m);
362 
363 	mm_request_receive_expect(pmonitor->m_recvfd,
364 	    MONITOR_ANS_AUTH2_READ_BANNER, m);
365 	if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
366 		fatal_fr(r, "parse");
367 	sshbuf_free(m);
368 
369 	/* treat empty banner as missing banner */
370 	if (strlen(banner) == 0) {
371 		free(banner);
372 		banner = NULL;
373 	}
374 	return (banner);
375 }
376 
377 /* Inform the privileged process about service and style */
378 
379 void
mm_inform_authserv(char * service,char * style)380 mm_inform_authserv(char *service, char *style)
381 {
382 	struct sshbuf *m;
383 	int r;
384 
385 	debug3_f("entering");
386 
387 	if ((m = sshbuf_new()) == NULL)
388 		fatal_f("sshbuf_new failed");
389 	if ((r = sshbuf_put_cstring(m, service)) != 0 ||
390 	    (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
391 		fatal_fr(r, "assemble");
392 
393 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
394 
395 	sshbuf_free(m);
396 }
397 
398 /* Do the password authentication */
399 int
mm_auth_password(struct ssh * ssh,char * password)400 mm_auth_password(struct ssh *ssh, char *password)
401 {
402 	struct sshbuf *m;
403 	int r, authenticated = 0;
404 
405 	debug3_f("entering");
406 
407 	if ((m = sshbuf_new()) == NULL)
408 		fatal_f("sshbuf_new failed");
409 	if ((r = sshbuf_put_cstring(m, password)) != 0)
410 		fatal_fr(r, "assemble");
411 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
412 
413 	debug3_f("waiting for MONITOR_ANS_AUTHPASSWORD");
414 	mm_request_receive_expect(pmonitor->m_recvfd,
415 	    MONITOR_ANS_AUTHPASSWORD, m);
416 
417 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
418 		fatal_fr(r, "parse");
419 
420 	sshbuf_free(m);
421 
422 	debug3_f("user %sauthenticated", authenticated ? "" : "not ");
423 	return (authenticated);
424 }
425 
426 int
mm_user_key_allowed(struct ssh * ssh,struct passwd * pw,struct sshkey * key,int pubkey_auth_attempt,struct sshauthopt ** authoptp)427 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
428     int pubkey_auth_attempt, struct sshauthopt **authoptp)
429 {
430 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
431 	    pubkey_auth_attempt, authoptp));
432 }
433 
434 int
mm_hostbased_key_allowed(struct ssh * ssh,struct passwd * pw,const char * user,const char * host,struct sshkey * key)435 mm_hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
436     const char *user, const char *host, struct sshkey *key)
437 {
438 	return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
439 }
440 
441 int
mm_key_allowed(enum mm_keytype type,const char * user,const char * host,struct sshkey * key,int pubkey_auth_attempt,struct sshauthopt ** authoptp)442 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
443     struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
444 {
445 	struct sshbuf *m;
446 	int r, allowed = 0;
447 	struct sshauthopt *opts = NULL;
448 
449 	debug3_f("entering");
450 
451 	if (authoptp != NULL)
452 		*authoptp = NULL;
453 
454 	if ((m = sshbuf_new()) == NULL)
455 		fatal_f("sshbuf_new failed");
456 	if ((r = sshbuf_put_u32(m, type)) != 0 ||
457 	    (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
458 	    (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
459 	    (r = sshkey_puts(key, m)) != 0 ||
460 	    (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
461 		fatal_fr(r, "assemble");
462 
463 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
464 
465 	debug3_f("waiting for MONITOR_ANS_KEYALLOWED");
466 	mm_request_receive_expect(pmonitor->m_recvfd,
467 	    MONITOR_ANS_KEYALLOWED, m);
468 
469 	if ((r = sshbuf_get_u32(m, &allowed)) != 0)
470 		fatal_fr(r, "parse");
471 	if (allowed && type == MM_USERKEY &&
472 	    (r = sshauthopt_deserialise(m, &opts)) != 0)
473 		fatal_fr(r, "sshauthopt_deserialise");
474 	sshbuf_free(m);
475 
476 	if (authoptp != NULL) {
477 		*authoptp = opts;
478 		opts = NULL;
479 	}
480 	sshauthopt_free(opts);
481 
482 	return allowed;
483 }
484 
485 /*
486  * This key verify needs to send the key type along, because the
487  * privileged parent makes the decision if the key is allowed
488  * for authentication.
489  */
490 
491 int
mm_sshkey_verify(const struct sshkey * key,const u_char * sig,size_t siglen,const u_char * data,size_t datalen,const char * sigalg,u_int compat,struct sshkey_sig_details ** sig_detailsp)492 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
493     const u_char *data, size_t datalen, const char *sigalg, u_int compat,
494     struct sshkey_sig_details **sig_detailsp)
495 {
496 	struct sshbuf *m;
497 	u_int encoded_ret = 0;
498 	int r;
499 	u_char sig_details_present, flags;
500 	u_int counter;
501 
502 	debug3_f("entering");
503 
504 	if (sig_detailsp != NULL)
505 		*sig_detailsp = NULL;
506 	if ((m = sshbuf_new()) == NULL)
507 		fatal_f("sshbuf_new failed");
508 	if ((r = sshkey_puts(key, m)) != 0 ||
509 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
510 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
511 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
512 		fatal_fr(r, "assemble");
513 
514 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
515 
516 	debug3_f("waiting for MONITOR_ANS_KEYVERIFY");
517 	mm_request_receive_expect(pmonitor->m_recvfd,
518 	    MONITOR_ANS_KEYVERIFY, m);
519 
520 	if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0 ||
521 	    (r = sshbuf_get_u8(m, &sig_details_present)) != 0)
522 		fatal_fr(r, "parse");
523 	if (sig_details_present && encoded_ret == 0) {
524 		if ((r = sshbuf_get_u32(m, &counter)) != 0 ||
525 		    (r = sshbuf_get_u8(m, &flags)) != 0)
526 			fatal_fr(r, "parse sig_details");
527 		if (sig_detailsp != NULL) {
528 			*sig_detailsp = xcalloc(1, sizeof(**sig_detailsp));
529 			(*sig_detailsp)->sk_counter = counter;
530 			(*sig_detailsp)->sk_flags = flags;
531 		}
532 	}
533 
534 	sshbuf_free(m);
535 
536 	if (encoded_ret != 0)
537 		return SSH_ERR_SIGNATURE_INVALID;
538 	return 0;
539 }
540 
541 void
mm_send_keystate(struct ssh * ssh,struct monitor * monitor)542 mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
543 {
544 	struct sshbuf *m;
545 	int r;
546 
547 	if ((m = sshbuf_new()) == NULL)
548 		fatal_f("sshbuf_new failed");
549 	if ((r = ssh_packet_get_state(ssh, m)) != 0)
550 		fatal_fr(r, "ssh_packet_get_state");
551 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
552 	debug3_f("Finished sending state");
553 	sshbuf_free(m);
554 }
555 
556 int
mm_pty_allocate(int * ptyfd,int * ttyfd,char * namebuf,size_t namebuflen)557 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
558 {
559 	struct sshbuf *m;
560 	char *p, *msg;
561 	int success = 0, tmp1 = -1, tmp2 = -1, r;
562 
563 	/* Kludge: ensure there are fds free to receive the pty/tty */
564 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
565 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
566 		error_f("cannot allocate fds for pty");
567 		if (tmp1 >= 0)
568 			close(tmp1);
569 		return 0;
570 	}
571 	close(tmp1);
572 	close(tmp2);
573 
574 	if ((m = sshbuf_new()) == NULL)
575 		fatal_f("sshbuf_new failed");
576 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
577 
578 	debug3_f("waiting for MONITOR_ANS_PTY");
579 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
580 
581 	if ((r = sshbuf_get_u32(m, &success)) != 0)
582 		fatal_fr(r, "parse success");
583 	if (success == 0) {
584 		debug3_f("pty alloc failed");
585 		sshbuf_free(m);
586 		return (0);
587 	}
588 	if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
589 	    (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
590 		fatal_fr(r, "parse");
591 	sshbuf_free(m);
592 
593 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
594 	free(p);
595 
596 	if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
597 		fatal_fr(r, "put loginmsg");
598 	free(msg);
599 
600 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
601 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
602 		fatal_f("receive fds failed");
603 
604 	/* Success */
605 	return (1);
606 }
607 
608 void
mm_session_pty_cleanup2(Session * s)609 mm_session_pty_cleanup2(Session *s)
610 {
611 	struct sshbuf *m;
612 	int r;
613 
614 	if (s->ttyfd == -1)
615 		return;
616 	if ((m = sshbuf_new()) == NULL)
617 		fatal_f("sshbuf_new failed");
618 	if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
619 		fatal_fr(r, "assmble");
620 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
621 	sshbuf_free(m);
622 
623 	/* closed dup'ed master */
624 	if (s->ptymaster != -1 && close(s->ptymaster) == -1)
625 		error("close(s->ptymaster/%d): %s",
626 		    s->ptymaster, strerror(errno));
627 
628 	/* unlink pty from session */
629 	s->ttyfd = -1;
630 }
631 
632 /* Request process termination */
633 
634 void
mm_terminate(void)635 mm_terminate(void)
636 {
637 	struct sshbuf *m;
638 
639 	if ((m = sshbuf_new()) == NULL)
640 		fatal_f("sshbuf_new failed");
641 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
642 	sshbuf_free(m);
643 }
644 
645 static void
mm_chall_setup(char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)646 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
647     char ***prompts, u_int **echo_on)
648 {
649 	*name = xstrdup("");
650 	*infotxt = xstrdup("");
651 	*numprompts = 1;
652 	*prompts = xcalloc(*numprompts, sizeof(char *));
653 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
654 	(*echo_on)[0] = 0;
655 }
656 
657 int
mm_bsdauth_query(void * ctx,char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)658 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
659    u_int *numprompts, char ***prompts, u_int **echo_on)
660 {
661 	struct sshbuf *m;
662 	u_int success;
663 	char *challenge;
664 	int r;
665 
666 	debug3_f("entering");
667 
668 	if ((m = sshbuf_new()) == NULL)
669 		fatal_f("sshbuf_new failed");
670 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
671 
672 	mm_request_receive_expect(pmonitor->m_recvfd,
673 	    MONITOR_ANS_BSDAUTHQUERY, m);
674 	if ((r = sshbuf_get_u32(m, &success)) != 0)
675 		fatal_fr(r, "parse success");
676 	if (success == 0) {
677 		debug3_f("no challenge");
678 		sshbuf_free(m);
679 		return (-1);
680 	}
681 
682 	/* Get the challenge, and format the response */
683 	if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
684 		fatal_fr(r, "parse challenge");
685 	sshbuf_free(m);
686 
687 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
688 	(*prompts)[0] = challenge;
689 
690 	debug3_f("received challenge: %s", challenge);
691 
692 	return (0);
693 }
694 
695 int
mm_bsdauth_respond(void * ctx,u_int numresponses,char ** responses)696 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
697 {
698 	struct sshbuf *m;
699 	int r, authok;
700 
701 	debug3_f("entering");
702 	if (numresponses != 1)
703 		return (-1);
704 
705 	if ((m = sshbuf_new()) == NULL)
706 		fatal_f("sshbuf_new failed");
707 	if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
708 		fatal_fr(r, "assemble");
709 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
710 
711 	mm_request_receive_expect(pmonitor->m_recvfd,
712 	    MONITOR_ANS_BSDAUTHRESPOND, m);
713 
714 	if ((r = sshbuf_get_u32(m, &authok)) != 0)
715 		fatal_fr(r, "parse");
716 	sshbuf_free(m);
717 
718 	return ((authok == 0) ? -1 : 0);
719 }
720 
721 #ifdef GSSAPI
722 OM_uint32
mm_ssh_gssapi_server_ctx(Gssctxt ** ctx,gss_OID goid)723 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
724 {
725 	struct sshbuf *m;
726 	OM_uint32 major;
727 	int r;
728 
729 	/* Client doesn't get to see the context */
730 	*ctx = NULL;
731 
732 	if ((m = sshbuf_new()) == NULL)
733 		fatal_f("sshbuf_new failed");
734 	if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
735 		fatal_fr(r, "assemble");
736 
737 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
738 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
739 
740 	if ((r = sshbuf_get_u32(m, &major)) != 0)
741 		fatal_fr(r, "parse");
742 
743 	sshbuf_free(m);
744 	return (major);
745 }
746 
747 OM_uint32
mm_ssh_gssapi_accept_ctx(Gssctxt * ctx,gss_buffer_desc * in,gss_buffer_desc * out,OM_uint32 * flagsp)748 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
749     gss_buffer_desc *out, OM_uint32 *flagsp)
750 {
751 	struct sshbuf *m;
752 	OM_uint32 major;
753 	u_int flags;
754 	int r;
755 
756 	if ((m = sshbuf_new()) == NULL)
757 		fatal_f("sshbuf_new failed");
758 	if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
759 		fatal_fr(r, "assemble");
760 
761 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
762 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
763 
764 	if ((r = sshbuf_get_u32(m, &major)) != 0 ||
765 	    (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
766 		fatal_fr(r, "parse");
767 	if (flagsp != NULL) {
768 		if ((r = sshbuf_get_u32(m, &flags)) != 0)
769 			fatal_fr(r, "parse flags");
770 		*flagsp = flags;
771 	}
772 
773 	sshbuf_free(m);
774 
775 	return (major);
776 }
777 
778 OM_uint32
mm_ssh_gssapi_checkmic(Gssctxt * ctx,gss_buffer_t gssbuf,gss_buffer_t gssmic)779 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
780 {
781 	struct sshbuf *m;
782 	OM_uint32 major;
783 	int r;
784 
785 	if ((m = sshbuf_new()) == NULL)
786 		fatal_f("sshbuf_new failed");
787 	if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
788 	    (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
789 		fatal_fr(r, "assemble");
790 
791 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
792 	mm_request_receive_expect(pmonitor->m_recvfd,
793 	    MONITOR_ANS_GSSCHECKMIC, m);
794 
795 	if ((r = sshbuf_get_u32(m, &major)) != 0)
796 		fatal_fr(r, "parse");
797 	sshbuf_free(m);
798 	return(major);
799 }
800 
801 int
mm_ssh_gssapi_userok(char * user)802 mm_ssh_gssapi_userok(char *user)
803 {
804 	struct sshbuf *m;
805 	int r, authenticated = 0;
806 
807 	if ((m = sshbuf_new()) == NULL)
808 		fatal_f("sshbuf_new failed");
809 
810 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
811 	mm_request_receive_expect(pmonitor->m_recvfd,
812 	    MONITOR_ANS_GSSUSEROK, m);
813 
814 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
815 		fatal_fr(r, "parse");
816 
817 	sshbuf_free(m);
818 	debug3_f("user %sauthenticated", authenticated ? "" : "not ");
819 	return (authenticated);
820 }
821 #endif /* GSSAPI */
822 
823 /*
824  * Inform channels layer of permitopen options for a single forwarding
825  * direction (local/remote).
826  */
827 static void
server_process_permitopen_list(struct ssh * ssh,int listen,char ** opens,u_int num_opens)828 server_process_permitopen_list(struct ssh *ssh, int listen,
829     char **opens, u_int num_opens)
830 {
831 	u_int i;
832 	int port;
833 	char *host, *arg, *oarg;
834 	int where = listen ? FORWARD_REMOTE : FORWARD_LOCAL;
835 	const char *what = listen ? "permitlisten" : "permitopen";
836 
837 	channel_clear_permission(ssh, FORWARD_ADM, where);
838 	if (num_opens == 0)
839 		return; /* permit any */
840 
841 	/* handle keywords: "any" / "none" */
842 	if (num_opens == 1 && strcmp(opens[0], "any") == 0)
843 		return;
844 	if (num_opens == 1 && strcmp(opens[0], "none") == 0) {
845 		channel_disable_admin(ssh, where);
846 		return;
847 	}
848 	/* Otherwise treat it as a list of permitted host:port */
849 	for (i = 0; i < num_opens; i++) {
850 		oarg = arg = xstrdup(opens[i]);
851 		host = hpdelim(&arg);
852 		if (host == NULL)
853 			fatal_f("missing host in %s", what);
854 		host = cleanhostname(host);
855 		if (arg == NULL || ((port = permitopen_port(arg)) < 0))
856 			fatal_f("bad port number in %s", what);
857 		/* Send it to channels layer */
858 		channel_add_permission(ssh, FORWARD_ADM,
859 		    where, host, port);
860 		free(oarg);
861 	}
862 }
863 
864 /*
865  * Inform channels layer of permitopen options from configuration.
866  */
867 void
server_process_permitopen(struct ssh * ssh)868 server_process_permitopen(struct ssh *ssh)
869 {
870 	server_process_permitopen_list(ssh, 0,
871 	    options.permitted_opens, options.num_permitted_opens);
872 	server_process_permitopen_list(ssh, 1,
873 	    options.permitted_listens, options.num_permitted_listens);
874 }
875 
876 void
server_process_channel_timeouts(struct ssh * ssh)877 server_process_channel_timeouts(struct ssh *ssh)
878 {
879 	u_int i, secs;
880 	char *type;
881 
882 	debug3_f("setting %u timeouts", options.num_channel_timeouts);
883 	channel_clear_timeouts(ssh);
884 	for (i = 0; i < options.num_channel_timeouts; i++) {
885 		if (parse_pattern_interval(options.channel_timeouts[i],
886 		    &type, &secs) != 0) {
887 			fatal_f("internal error: bad timeout %s",
888 			    options.channel_timeouts[i]);
889 		}
890 		channel_add_timeout(ssh, type, secs);
891 		free(type);
892 	}
893 }
894 
895 struct connection_info *
server_get_connection_info(struct ssh * ssh,int populate,int use_dns)896 server_get_connection_info(struct ssh *ssh, int populate, int use_dns)
897 {
898 	static struct connection_info ci;
899 
900 	if (ssh == NULL || !populate)
901 		return &ci;
902 	ci.host = use_dns ? ssh_remote_hostname(ssh) : ssh_remote_ipaddr(ssh);
903 	ci.address = ssh_remote_ipaddr(ssh);
904 	ci.laddress = ssh_local_ipaddr(ssh);
905 	ci.lport = ssh_local_port(ssh);
906 	ci.rdomain = ssh_packet_rdomain_in(ssh);
907 	return &ci;
908 }
909 
910