xref: /freebsd/crypto/openssh/monitor_wrap.c (revision 2574974648c68c738aec3ff96644d888d7913a37)
1 /* $OpenBSD: monitor_wrap.c,v 1.146 2026/03/02 02:40:15 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 "includes.h"
29 
30 #include <sys/types.h>
31 #include <sys/uio.h>
32 #include <sys/queue.h>
33 #include <sys/wait.h>
34 
35 #include <errno.h>
36 #include <pwd.h>
37 #include <signal.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #ifdef WITH_OPENSSL
44 #include <openssl/bn.h>
45 #include <openssl/dh.h>
46 #include <openssl/evp.h>
47 #endif
48 
49 #include "xmalloc.h"
50 #include "ssh.h"
51 #ifdef WITH_OPENSSL
52 #include "dh.h"
53 #endif
54 #include "sshbuf.h"
55 #include "sshkey.h"
56 #include "cipher.h"
57 #include "kex.h"
58 #include "hostfile.h"
59 #include "auth.h"
60 #include "auth-options.h"
61 #include "packet.h"
62 #include "mac.h"
63 #include "log.h"
64 #include "auth-pam.h"
65 #include "monitor.h"
66 #ifdef GSSAPI
67 #include "ssh-gss.h"
68 #endif
69 #include "atomicio.h"
70 #include "monitor_fdpass.h"
71 #include "misc.h"
72 
73 #include "channels.h"
74 #include "session.h"
75 #include "servconf.h"
76 #include "monitor_wrap.h"
77 #include "srclimit.h"
78 
79 #include "ssherr.h"
80 
81 /* Imports */
82 extern struct monitor *pmonitor;
83 extern struct sshbuf *loginmsg;
84 extern ServerOptions options;
85 
86 void
mm_log_handler(LogLevel level,int forced,const char * msg,void * ctx)87 mm_log_handler(LogLevel level, int forced, const char *msg, void *ctx)
88 {
89 	struct sshbuf *log_msg;
90 	struct monitor *mon = (struct monitor *)ctx;
91 	int r;
92 	size_t len;
93 
94 	if (mon->m_log_sendfd == -1)
95 		fatal_f("no log channel");
96 
97 	if ((log_msg = sshbuf_new()) == NULL)
98 		fatal_f("sshbuf_new failed");
99 
100 	if ((r = sshbuf_put_u32(log_msg, 0)) != 0 || /* length; filled below */
101 	    (r = sshbuf_put_u32(log_msg, level)) != 0 ||
102 	    (r = sshbuf_put_u32(log_msg, forced)) != 0 ||
103 	    (r = sshbuf_put_cstring(log_msg, msg)) != 0)
104 		fatal_fr(r, "assemble");
105 	if ((len = sshbuf_len(log_msg)) < 4 || len > 0xffffffff)
106 		fatal_f("bad length %zu", len);
107 	POKE_U32(sshbuf_mutable_ptr(log_msg), len - 4);
108 	if (atomicio(vwrite, mon->m_log_sendfd,
109 	    sshbuf_mutable_ptr(log_msg), len) != len) {
110 		if (errno == EPIPE) {
111 			debug_f("write: %s", strerror(errno));
112 			cleanup_exit(255);
113 		}
114 		fatal_f("write: %s", strerror(errno));
115 	}
116 	sshbuf_free(log_msg);
117 }
118 
119 static void
mm_reap(void)120 mm_reap(void)
121 {
122 	int status = -1;
123 
124 	if (!mm_is_monitor())
125 		return;
126 	while (waitpid(pmonitor->m_pid, &status, 0) == -1) {
127 		if (errno == EINTR)
128 			continue;
129 		pmonitor->m_pid = -1;
130 		fatal_f("waitpid: %s", strerror(errno));
131 	}
132 	if (WIFEXITED(status)) {
133 		if (WEXITSTATUS(status) != 0) {
134 			debug_f("child exited with status %d",
135 			    WEXITSTATUS(status));
136 			cleanup_exit(255);
137 		}
138 	} else if (WIFSIGNALED(status)) {
139 		error_f("child terminated by signal %d",
140 		    WTERMSIG(status));
141 		cleanup_exit(signal_is_crash(WTERMSIG(status)) ?
142 		    EXIT_CHILD_CRASH : 255);
143 	} else {
144 		error_f("child terminated abnormally (status=0x%x)",
145 		    status);
146 		cleanup_exit(EXIT_CHILD_CRASH);
147 	}
148 }
149 
150 void
mm_request_send(int sock,enum monitor_reqtype type,struct sshbuf * m)151 mm_request_send(int sock, enum monitor_reqtype type, struct sshbuf *m)
152 {
153 	size_t mlen = sshbuf_len(m);
154 	u_char buf[5];
155 
156 	debug3_f("entering, type %d", type);
157 
158 	if (mlen >= MONITOR_MAX_MSGLEN)
159 		fatal_f("bad length %zu", mlen);
160 	POKE_U32(buf, mlen + 1);
161 	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
162 	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf) ||
163 	    atomicio(vwrite, sock, sshbuf_mutable_ptr(m), mlen) != mlen) {
164 		if (errno == EPIPE) {
165 			debug3_f("monitor fd closed");
166 			mm_reap();
167 			cleanup_exit(255);
168 		}
169 		fatal_f("write: %s", strerror(errno));
170 	}
171 }
172 
173 void
mm_request_receive(int sock,struct sshbuf * m)174 mm_request_receive(int sock, struct sshbuf *m)
175 {
176 	u_char buf[4], *p = NULL;
177 	u_int msg_len;
178 	int oerrno, r;
179 
180 	debug3_f("entering");
181 
182 	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
183 		if (errno == EPIPE) {
184 			debug3_f("monitor fd closed");
185 			mm_reap();
186 			cleanup_exit(255);
187 		}
188 		fatal_f("read: %s", strerror(errno));
189 	}
190 	msg_len = PEEK_U32(buf);
191 	if (msg_len > MONITOR_MAX_MSGLEN)
192 		fatal_f("read: bad msg_len %d", msg_len);
193 	sshbuf_reset(m);
194 	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
195 		fatal_fr(r, "reserve");
196 	if (atomicio(read, sock, p, msg_len) != msg_len) {
197 		oerrno = errno;
198 		error_f("read: %s", strerror(errno));
199 		if (oerrno == EPIPE)
200 			mm_reap();
201 		cleanup_exit(255);
202 	}
203 }
204 
205 void
mm_request_receive_expect(int sock,enum monitor_reqtype type,struct sshbuf * m)206 mm_request_receive_expect(int sock, enum monitor_reqtype type, struct sshbuf *m)
207 {
208 	u_char rtype;
209 	int r;
210 
211 	debug3_f("entering, type %d", type);
212 
213 	mm_request_receive(sock, m);
214 	if ((r = sshbuf_get_u8(m, &rtype)) != 0)
215 		fatal_fr(r, "parse");
216 	if (rtype != type)
217 		fatal_f("read: rtype %d != type %d", rtype, type);
218 }
219 
220 #ifdef WITH_OPENSSL
221 DH *
mm_choose_dh(int min,int nbits,int max)222 mm_choose_dh(int min, int nbits, int max)
223 {
224 	BIGNUM *p, *g;
225 	int r;
226 	u_char success = 0;
227 	struct sshbuf *m;
228 
229 	if ((m = sshbuf_new()) == NULL)
230 		fatal_f("sshbuf_new failed");
231 	if ((r = sshbuf_put_u32(m, min)) != 0 ||
232 	    (r = sshbuf_put_u32(m, nbits)) != 0 ||
233 	    (r = sshbuf_put_u32(m, max)) != 0)
234 		fatal_fr(r, "assemble");
235 
236 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, m);
237 
238 	debug3_f("waiting for MONITOR_ANS_MODULI");
239 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, m);
240 
241 	if ((r = sshbuf_get_u8(m, &success)) != 0)
242 		fatal_fr(r, "parse success");
243 	if (success == 0)
244 		fatal_f("MONITOR_ANS_MODULI failed");
245 
246 	if ((r = sshbuf_get_bignum2(m, &p)) != 0 ||
247 	    (r = sshbuf_get_bignum2(m, &g)) != 0)
248 		fatal_fr(r, "parse group");
249 
250 	debug3_f("remaining %zu", sshbuf_len(m));
251 	sshbuf_free(m);
252 
253 	return (dh_new_group(g, p));
254 }
255 #endif
256 
257 void
mm_sshkey_setcompat(struct ssh * ssh)258 mm_sshkey_setcompat(struct ssh *ssh)
259 {
260 	struct sshbuf *m;
261 	int r;
262 
263 	debug3_f("entering");
264 	if ((m = sshbuf_new()) == NULL)
265 		fatal_f("sshbuf_new failed");
266 	if ((r = sshbuf_put_u32(m, ssh->compat)) != 0)
267 		fatal_fr(r, "assemble");
268 
269 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SETCOMPAT, m);
270 }
271 
272 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)273 mm_sshkey_sign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
274     const u_char *data, size_t datalen, const char *hostkey_alg,
275     const char *sk_provider, const char *sk_pin, u_int compat)
276 {
277 	struct sshbuf *m;
278 	int r;
279 
280 	debug3_f("entering");
281 	if ((m = sshbuf_new()) == NULL)
282 		fatal_f("sshbuf_new failed");
283 	if ((r = sshkey_puts(key, m)) != 0 ||
284 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
285 	    (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
286 	    (r = sshbuf_put_u32(m, compat)) != 0)
287 		fatal_fr(r, "assemble");
288 
289 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
290 
291 	debug3_f("waiting for MONITOR_ANS_SIGN");
292 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
293 	if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
294 		fatal_fr(r, "parse");
295 	sshbuf_free(m);
296 	debug3_f("%s signature len=%zu", hostkey_alg ? hostkey_alg : "(null)",
297 	    *lenp);
298 
299 	return (0);
300 }
301 
302 void
mm_decode_activate_server_options(struct ssh * ssh,struct sshbuf * m)303 mm_decode_activate_server_options(struct ssh *ssh, struct sshbuf *m)
304 {
305 	const u_char *p;
306 	size_t len;
307 	u_int i;
308 	ServerOptions *newopts;
309 	int r;
310 
311 	if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
312 		fatal_fr(r, "parse opts");
313 	if (len != sizeof(*newopts))
314 		fatal_f("option block size mismatch");
315 	newopts = xcalloc(sizeof(*newopts), 1);
316 	memcpy(newopts, p, sizeof(*newopts));
317 
318 #define M_CP_STROPT(x) do { \
319 		if (newopts->x != NULL && \
320 		    (r = sshbuf_get_cstring(m, &newopts->x, NULL)) != 0) \
321 			fatal_fr(r, "parse %s", #x); \
322 	} while (0)
323 #define M_CP_STRARRAYOPT(x, nx, clobber) do { \
324 		newopts->x = newopts->nx == 0 ? \
325 		    NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
326 		for (i = 0; i < newopts->nx; i++) { \
327 			if ((r = sshbuf_get_cstring(m, \
328 			    &newopts->x[i], NULL)) != 0) \
329 				fatal_fr(r, "parse %s", #x); \
330 		} \
331 	} while (0)
332 	/* See comment in servconf.h */
333 	COPY_MATCH_STRING_OPTS();
334 #undef M_CP_STROPT
335 #undef M_CP_STRARRAYOPT
336 
337 	copy_set_server_options(&options, newopts, 1);
338 	log_change_level(options.log_level);
339 	log_verbose_reset();
340 	for (i = 0; i < options.num_log_verbose; i++)
341 		log_verbose_add(options.log_verbose[i]);
342 
343 	/* use the macro hell to clean up too */
344 #define M_CP_STROPT(x) free(newopts->x)
345 #define M_CP_STRARRAYOPT(x, nx, clobber) do { \
346 		for (i = 0; i < newopts->nx; i++) \
347 			free(newopts->x[i]); \
348 		free(newopts->x); \
349 	} while (0)
350 	COPY_MATCH_STRING_OPTS();
351 #undef M_CP_STROPT
352 #undef M_CP_STRARRAYOPT
353 	free(newopts);
354 }
355 
356 #define GETPW(b, id) \
357 	do { \
358 		if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0) \
359 			fatal_fr(r, "parse pw %s", #id); \
360 		if (len != sizeof(pw->id)) \
361 			fatal_fr(r, "bad length for %s", #id); \
362 		memcpy(&pw->id, p, len); \
363 	} while (0)
364 
365 struct passwd *
mm_getpwnamallow(struct ssh * ssh,const char * username)366 mm_getpwnamallow(struct ssh *ssh, const char *username)
367 {
368 	struct sshbuf *m;
369 	struct passwd *pw;
370 	size_t len;
371 	int r;
372 	u_char ok;
373 	const u_char *p;
374 
375 	debug3_f("entering");
376 
377 	if ((m = sshbuf_new()) == NULL)
378 		fatal_f("sshbuf_new failed");
379 	if ((r = sshbuf_put_cstring(m, username)) != 0)
380 		fatal_fr(r, "assemble");
381 
382 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
383 
384 	debug3_f("waiting for MONITOR_ANS_PWNAM");
385 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
386 
387 	if ((r = sshbuf_get_u8(m, &ok)) != 0)
388 		fatal_fr(r, "parse success");
389 	if (ok == 0) {
390 		pw = NULL;
391 		goto out;
392 	}
393 
394 	/* XXX don't like passing struct passwd like this */
395 	pw = xcalloc(sizeof(*pw), 1);
396 	GETPW(m, pw_uid);
397 	GETPW(m, pw_gid);
398 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
399 	GETPW(m, pw_change);
400 #endif
401 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
402 	GETPW(m, pw_expire);
403 #endif
404 	if ((r = sshbuf_get_cstring(m, &pw->pw_name, NULL)) != 0 ||
405 	    (r = sshbuf_get_cstring(m, &pw->pw_passwd, NULL)) != 0 ||
406 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
407 	    (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 ||
408 #endif
409 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
410 	    (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 ||
411 #endif
412 	    (r = sshbuf_get_cstring(m, &pw->pw_dir, NULL)) != 0 ||
413 	    (r = sshbuf_get_cstring(m, &pw->pw_shell, NULL)) != 0)
414 		fatal_fr(r, "parse pw");
415 
416 out:
417 	/* copy options block as a Match directive may have changed some */
418 	mm_decode_activate_server_options(ssh, m);
419 	server_process_permitopen(ssh);
420 	server_process_channel_timeouts(ssh);
421 	kex_set_server_sig_algs(ssh, options.pubkey_accepted_algos);
422 	sshbuf_free(m);
423 
424 	return (pw);
425 }
426 
427 char *
mm_auth2_read_banner(void)428 mm_auth2_read_banner(void)
429 {
430 	struct sshbuf *m;
431 	char *banner;
432 	int r;
433 
434 	debug3_f("entering");
435 
436 	if ((m = sshbuf_new()) == NULL)
437 		fatal_f("sshbuf_new failed");
438 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
439 	sshbuf_reset(m);
440 
441 	mm_request_receive_expect(pmonitor->m_recvfd,
442 	    MONITOR_ANS_AUTH2_READ_BANNER, m);
443 	if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
444 		fatal_fr(r, "parse");
445 	sshbuf_free(m);
446 
447 	/* treat empty banner as missing banner */
448 	if (strlen(banner) == 0) {
449 		free(banner);
450 		banner = NULL;
451 	}
452 	return (banner);
453 }
454 
455 /* Inform the privileged process about service and style */
456 
457 void
mm_inform_authserv(char * service,char * style)458 mm_inform_authserv(char *service, char *style)
459 {
460 	struct sshbuf *m;
461 	int r;
462 
463 	debug3_f("entering");
464 
465 	if ((m = sshbuf_new()) == NULL)
466 		fatal_f("sshbuf_new failed");
467 	if ((r = sshbuf_put_cstring(m, service)) != 0 ||
468 	    (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
469 		fatal_fr(r, "assemble");
470 
471 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
472 
473 	sshbuf_free(m);
474 }
475 
476 /* Do the password authentication */
477 int
mm_auth_password(struct ssh * ssh,char * password)478 mm_auth_password(struct ssh *ssh, char *password)
479 {
480 	struct sshbuf *m;
481 	int r, authenticated = 0;
482 #ifdef USE_PAM
483 	u_int maxtries = 0;
484 #endif
485 
486 	debug3_f("entering");
487 
488 	if ((m = sshbuf_new()) == NULL)
489 		fatal_f("sshbuf_new failed");
490 	if ((r = sshbuf_put_cstring(m, password)) != 0)
491 		fatal_fr(r, "assemble");
492 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
493 
494 	debug3_f("waiting for MONITOR_ANS_AUTHPASSWORD");
495 	mm_request_receive_expect(pmonitor->m_recvfd,
496 	    MONITOR_ANS_AUTHPASSWORD, m);
497 
498 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
499 		fatal_fr(r, "parse");
500 #ifdef USE_PAM
501 	if ((r = sshbuf_get_u32(m, &maxtries)) != 0)
502 		fatal_fr(r, "parse PAM");
503 	if (maxtries > INT_MAX)
504 		fatal_fr(r, "bad maxtries");
505 	sshpam_set_maxtries_reached(maxtries);
506 #endif
507 
508 	sshbuf_free(m);
509 
510 	debug3_f("user %sauthenticated", authenticated ? "" : "not ");
511 	return (authenticated);
512 }
513 
514 int
mm_user_key_allowed(struct ssh * ssh,struct passwd * pw,struct sshkey * key,int pubkey_auth_attempt,struct sshauthopt ** authoptp)515 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
516     int pubkey_auth_attempt, struct sshauthopt **authoptp)
517 {
518 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
519 	    pubkey_auth_attempt, authoptp));
520 }
521 
522 int
mm_hostbased_key_allowed(struct ssh * ssh,struct passwd * pw,const char * user,const char * host,struct sshkey * key)523 mm_hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
524     const char *user, const char *host, struct sshkey *key)
525 {
526 	return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
527 }
528 
529 int
mm_key_allowed(enum mm_keytype type,const char * user,const char * host,struct sshkey * key,int pubkey_auth_attempt,struct sshauthopt ** authoptp)530 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
531     struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
532 {
533 	struct sshbuf *m;
534 	int r, allowed = 0;
535 	struct sshauthopt *opts = NULL;
536 
537 	debug3_f("entering");
538 
539 	if (authoptp != NULL)
540 		*authoptp = NULL;
541 
542 	if ((m = sshbuf_new()) == NULL)
543 		fatal_f("sshbuf_new failed");
544 	if ((r = sshbuf_put_u32(m, type)) != 0 ||
545 	    (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
546 	    (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
547 	    (r = sshkey_puts(key, m)) != 0 ||
548 	    (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
549 		fatal_fr(r, "assemble");
550 
551 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
552 
553 	debug3_f("waiting for MONITOR_ANS_KEYALLOWED");
554 	mm_request_receive_expect(pmonitor->m_recvfd,
555 	    MONITOR_ANS_KEYALLOWED, m);
556 
557 	if ((r = sshbuf_get_u32(m, &allowed)) != 0)
558 		fatal_fr(r, "parse");
559 	if (allowed && type == MM_USERKEY &&
560 	    (r = sshauthopt_deserialise(m, &opts)) != 0)
561 		fatal_fr(r, "sshauthopt_deserialise");
562 	sshbuf_free(m);
563 
564 	if (authoptp != NULL) {
565 		*authoptp = opts;
566 		opts = NULL;
567 	}
568 	sshauthopt_free(opts);
569 
570 	return allowed;
571 }
572 
573 /*
574  * This key verify needs to send the key type along, because the
575  * privileged parent makes the decision if the key is allowed
576  * for authentication.
577  */
578 
579 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)580 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
581     const u_char *data, size_t datalen, const char *sigalg, u_int compat,
582     struct sshkey_sig_details **sig_detailsp)
583 {
584 	struct sshbuf *m;
585 	u_int encoded_ret = 0;
586 	int r;
587 	u_char sig_details_present, flags;
588 	u_int counter;
589 
590 	debug3_f("entering");
591 
592 	if (sig_detailsp != NULL)
593 		*sig_detailsp = NULL;
594 	if ((m = sshbuf_new()) == NULL)
595 		fatal_f("sshbuf_new failed");
596 	if ((r = sshkey_puts(key, m)) != 0 ||
597 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
598 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
599 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
600 		fatal_fr(r, "assemble");
601 
602 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
603 
604 	debug3_f("waiting for MONITOR_ANS_KEYVERIFY");
605 	mm_request_receive_expect(pmonitor->m_recvfd,
606 	    MONITOR_ANS_KEYVERIFY, m);
607 
608 	if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0 ||
609 	    (r = sshbuf_get_u8(m, &sig_details_present)) != 0)
610 		fatal_fr(r, "parse");
611 	if (sig_details_present && encoded_ret == 0) {
612 		if ((r = sshbuf_get_u32(m, &counter)) != 0 ||
613 		    (r = sshbuf_get_u8(m, &flags)) != 0)
614 			fatal_fr(r, "parse sig_details");
615 		if (sig_detailsp != NULL) {
616 			*sig_detailsp = xcalloc(1, sizeof(**sig_detailsp));
617 			(*sig_detailsp)->sk_counter = counter;
618 			(*sig_detailsp)->sk_flags = flags;
619 		}
620 	}
621 
622 	sshbuf_free(m);
623 
624 	if (encoded_ret != 0)
625 		return SSH_ERR_SIGNATURE_INVALID;
626 	return 0;
627 }
628 
629 void
mm_send_keystate(struct ssh * ssh,struct monitor * monitor)630 mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
631 {
632 	struct sshbuf *m;
633 	int r;
634 
635 	if ((m = sshbuf_new()) == NULL)
636 		fatal_f("sshbuf_new failed");
637 	if ((r = ssh_packet_get_state(ssh, m)) != 0)
638 		fatal_fr(r, "ssh_packet_get_state");
639 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
640 	debug3_f("Finished sending state");
641 	sshbuf_free(m);
642 }
643 
644 int
mm_pty_allocate(int * ptyfd,int * ttyfd,char * namebuf,size_t namebuflen)645 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
646 {
647 	struct sshbuf *m;
648 	char *p, *msg;
649 	int success = 0, tmp1 = -1, tmp2 = -1, r;
650 
651 	/* Kludge: ensure there are fds free to receive the pty/tty */
652 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
653 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
654 		error_f("cannot allocate fds for pty");
655 		if (tmp1 >= 0)
656 			close(tmp1);
657 		return 0;
658 	}
659 	close(tmp1);
660 	close(tmp2);
661 
662 	if ((m = sshbuf_new()) == NULL)
663 		fatal_f("sshbuf_new failed");
664 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
665 
666 	debug3_f("waiting for MONITOR_ANS_PTY");
667 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
668 
669 	if ((r = sshbuf_get_u32(m, &success)) != 0)
670 		fatal_fr(r, "parse success");
671 	if (success == 0) {
672 		debug3_f("pty alloc failed");
673 		sshbuf_free(m);
674 		return (0);
675 	}
676 	if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
677 	    (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
678 		fatal_fr(r, "parse");
679 	sshbuf_free(m);
680 
681 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
682 	free(p);
683 
684 	if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
685 		fatal_fr(r, "put loginmsg");
686 	free(msg);
687 
688 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
689 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
690 		fatal_f("receive fds failed");
691 
692 	/* Success */
693 	return (1);
694 }
695 
696 void
mm_session_pty_cleanup2(Session * s)697 mm_session_pty_cleanup2(Session *s)
698 {
699 	struct sshbuf *m;
700 	int r;
701 
702 	if (s->ttyfd == -1)
703 		return;
704 	if ((m = sshbuf_new()) == NULL)
705 		fatal_f("sshbuf_new failed");
706 	if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
707 		fatal_fr(r, "assmble");
708 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
709 	sshbuf_free(m);
710 
711 	/* closed dup'ed master */
712 	if (s->ptymaster != -1 && close(s->ptymaster) == -1)
713 		error("close(s->ptymaster/%d): %s",
714 		    s->ptymaster, strerror(errno));
715 
716 	/* unlink pty from session */
717 	s->ttyfd = -1;
718 }
719 
720 #ifdef USE_PAM
721 void
mm_start_pam(struct ssh * ssh)722 mm_start_pam(struct ssh *ssh)
723 {
724 	struct sshbuf *m;
725 
726 	debug3_f("entering");
727 	if (!options.use_pam)
728 		fatal_f("UsePAM=no, but ended up in %s anyway", __func__);
729 	if ((m = sshbuf_new()) == NULL)
730 		fatal_f("sshbuf_new failed");
731 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m);
732 
733 	sshbuf_free(m);
734 }
735 
736 u_int
mm_do_pam_account(void)737 mm_do_pam_account(void)
738 {
739 	struct sshbuf *m;
740 	u_int ret;
741 	char *msg;
742 	size_t msglen;
743 	int r;
744 
745 	debug3_f("entering");
746 	if (!options.use_pam)
747 		fatal_f("UsePAM=no, but ended up in %s anyway", __func__);
748 
749 	if ((m = sshbuf_new()) == NULL)
750 		fatal_f("sshbuf_new failed");
751 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, m);
752 
753 	mm_request_receive_expect(pmonitor->m_recvfd,
754 	    MONITOR_ANS_PAM_ACCOUNT, m);
755 	if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
756 	    (r = sshbuf_get_cstring(m, &msg, &msglen)) != 0 ||
757 	    (r = sshbuf_put(loginmsg, msg, msglen)) != 0)
758 		fatal_fr(r, "buffer error");
759 
760 	free(msg);
761 	sshbuf_free(m);
762 
763 	debug3_f("returning %d", ret);
764 
765 	return (ret);
766 }
767 
768 void *
mm_sshpam_init_ctx(Authctxt * authctxt)769 mm_sshpam_init_ctx(Authctxt *authctxt)
770 {
771 	struct sshbuf *m;
772 	int r, success;
773 
774 	debug3_f("entering");
775 	if ((m = sshbuf_new()) == NULL)
776 		fatal_f("sshbuf_new failed");
777 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, m);
778 	debug3_f("waiting for MONITOR_ANS_PAM_INIT_CTX");
779 	mm_request_receive_expect(pmonitor->m_recvfd,
780 	    MONITOR_ANS_PAM_INIT_CTX, m);
781 	if ((r = sshbuf_get_u32(m, &success)) != 0)
782 		fatal_fr(r, "buffer error");
783 	if (success == 0) {
784 		debug3_f("pam_init_ctx failed");
785 		sshbuf_free(m);
786 		return (NULL);
787 	}
788 	sshbuf_free(m);
789 	return (authctxt);
790 }
791 
792 int
mm_sshpam_query(void * ctx,char ** name,char ** info,u_int * num,char *** prompts,u_int ** echo_on)793 mm_sshpam_query(void *ctx, char **name, char **info,
794     u_int *num, char ***prompts, u_int **echo_on)
795 {
796 	struct sshbuf *m;
797 	u_int i, n;
798 	int r, ret;
799 
800 	debug3_f("entering");
801 	if ((m = sshbuf_new()) == NULL)
802 		fatal_f("sshbuf_new failed");
803 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, m);
804 	debug3_f("waiting for MONITOR_ANS_PAM_QUERY");
805 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, m);
806 	if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
807 	    (r = sshbuf_get_cstring(m, name, NULL)) != 0 ||
808 	    (r = sshbuf_get_cstring(m, info, NULL)) != 0 ||
809 	    (r = sshbuf_get_u32(m, &n)) != 0 ||
810 	    (r = sshbuf_get_u32(m, num)) != 0)
811 		fatal_fr(r, "buffer error");
812 	debug3_f("pam_query returned %d", ret);
813 	sshpam_set_maxtries_reached(n);
814 	if (*num > PAM_MAX_NUM_MSG)
815 		fatal("%s: received %u PAM messages, expected <= %u",
816 		    __func__, *num, PAM_MAX_NUM_MSG);
817 	*prompts = xcalloc((*num + 1), sizeof(char *));
818 	*echo_on = xcalloc((*num + 1), sizeof(u_int));
819 	for (i = 0; i < *num; ++i) {
820 		if ((r = sshbuf_get_cstring(m, &((*prompts)[i]), NULL)) != 0 ||
821 		    (r = sshbuf_get_u32(m, &((*echo_on)[i]))) != 0)
822 			fatal_fr(r, "buffer error");
823 	}
824 	sshbuf_free(m);
825 	return (ret);
826 }
827 
828 int
mm_sshpam_respond(void * ctx,u_int num,char ** resp)829 mm_sshpam_respond(void *ctx, u_int num, char **resp)
830 {
831 	struct sshbuf *m;
832 	u_int n, i;
833 	int r, ret;
834 
835 	debug3_f("entering");
836 	if ((m = sshbuf_new()) == NULL)
837 		fatal_f("sshbuf_new failed");
838 	if ((r = sshbuf_put_u32(m, num)) != 0)
839 		fatal_fr(r, "buffer error");
840 	for (i = 0; i < num; ++i) {
841 		if ((r = sshbuf_put_cstring(m, resp[i])) != 0)
842 			fatal_fr(r, "buffer error");
843 	}
844 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, m);
845 	debug3_f("waiting for MONITOR_ANS_PAM_RESPOND");
846 	mm_request_receive_expect(pmonitor->m_recvfd,
847 	    MONITOR_ANS_PAM_RESPOND, m);
848 	if ((r = sshbuf_get_u32(m, &n)) != 0)
849 		fatal_fr(r, "buffer error");
850 	ret = (int)n; /* XXX */
851 	debug3_f("pam_respond returned %d", ret);
852 	sshbuf_free(m);
853 	return (ret);
854 }
855 
856 void
mm_sshpam_free_ctx(void * ctxtp)857 mm_sshpam_free_ctx(void *ctxtp)
858 {
859 	struct sshbuf *m;
860 
861 	debug3_f("entering");
862 	if ((m = sshbuf_new()) == NULL)
863 		fatal_f("sshbuf_new failed");
864 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, m);
865 	debug3_f("waiting for MONITOR_ANS_PAM_FREE_CTX");
866 	mm_request_receive_expect(pmonitor->m_recvfd,
867 	    MONITOR_ANS_PAM_FREE_CTX, m);
868 	sshbuf_free(m);
869 }
870 #endif /* USE_PAM */
871 
872 /* Request process termination */
873 
874 void
mm_terminate(void)875 mm_terminate(void)
876 {
877 	struct sshbuf *m;
878 
879 	if ((m = sshbuf_new()) == NULL)
880 		fatal_f("sshbuf_new failed");
881 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
882 	sshbuf_free(m);
883 }
884 
885 /* Request state information */
886 
887 void
mm_get_state(struct ssh * ssh,struct include_list * includes,struct sshbuf * conf,struct sshbuf ** confdatap,uint64_t * timing_secretp,struct sshbuf ** hostkeysp,struct sshbuf ** keystatep,u_char ** pw_namep,struct sshbuf ** authinfop,struct sshbuf ** auth_optsp)888 mm_get_state(struct ssh *ssh, struct include_list *includes,
889     struct sshbuf *conf, struct sshbuf **confdatap,
890     uint64_t *timing_secretp,
891     struct sshbuf **hostkeysp, struct sshbuf **keystatep,
892     u_char **pw_namep,
893     struct sshbuf **authinfop, struct sshbuf **auth_optsp)
894 {
895 	struct sshbuf *m, *inc;
896 	u_char *cp;
897 	size_t len;
898 	int r;
899 	struct include_item *item;
900 
901 	debug3_f("entering");
902 
903 	if ((m = sshbuf_new()) == NULL || (inc = sshbuf_new()) == NULL)
904 		fatal_f("sshbuf_new failed");
905 
906 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_STATE, m);
907 
908 	debug3_f("waiting for MONITOR_ANS_STATE");
909 	mm_request_receive_expect(pmonitor->m_recvfd,
910 	    MONITOR_ANS_STATE, m);
911 
912 	if ((r = sshbuf_get_string(m, &cp, &len)) != 0 ||
913 	    (r = sshbuf_get_u64(m, timing_secretp)) != 0 ||
914 	    (r = sshbuf_froms(m, hostkeysp)) != 0 ||
915 	    (r = sshbuf_get_stringb(m, ssh->kex->server_version)) != 0 ||
916 	    (r = sshbuf_get_stringb(m, ssh->kex->client_version)) != 0 ||
917 	    (r = sshbuf_get_stringb(m, inc)) != 0)
918 		fatal_fr(r, "parse config");
919 
920 	/* postauth */
921 	if (confdatap) {
922 		if ((r = sshbuf_froms(m, confdatap)) != 0 ||
923 		    (r = sshbuf_froms(m, keystatep)) != 0 ||
924 		    (r = sshbuf_get_string(m, pw_namep, NULL)) != 0 ||
925 		    (r = sshbuf_froms(m, authinfop)) != 0 ||
926 		    (r = sshbuf_froms(m, auth_optsp)) != 0)
927 			fatal_fr(r, "parse config postauth");
928 	}
929 
930 	if (conf != NULL && (r = sshbuf_put(conf, cp, len)))
931 		fatal_fr(r, "sshbuf_put");
932 
933 	while (sshbuf_len(inc) != 0) {
934 		item = xcalloc(1, sizeof(*item));
935 		if ((item->contents = sshbuf_new()) == NULL)
936 			fatal_f("sshbuf_new failed");
937 		if ((r = sshbuf_get_cstring(inc, &item->selector, NULL)) != 0 ||
938 		    (r = sshbuf_get_cstring(inc, &item->filename, NULL)) != 0 ||
939 		    (r = sshbuf_get_stringb(inc, item->contents)) != 0)
940 			fatal_fr(r, "parse includes");
941 		TAILQ_INSERT_TAIL(includes, item, entry);
942 	}
943 
944 	free(cp);
945 	sshbuf_free(m);
946 	sshbuf_free(inc);
947 
948 	debug3_f("done");
949 }
950 
951 static void
mm_chall_setup(char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)952 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
953     char ***prompts, u_int **echo_on)
954 {
955 	*name = xstrdup("");
956 	*infotxt = xstrdup("");
957 	*numprompts = 1;
958 	*prompts = xcalloc(*numprompts, sizeof(char *));
959 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
960 	(*echo_on)[0] = 0;
961 }
962 
963 int
mm_bsdauth_query(void * ctx,char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)964 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
965    u_int *numprompts, char ***prompts, u_int **echo_on)
966 {
967 	struct sshbuf *m;
968 	u_int success;
969 	char *challenge;
970 	int r;
971 
972 	debug3_f("entering");
973 
974 	if ((m = sshbuf_new()) == NULL)
975 		fatal_f("sshbuf_new failed");
976 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
977 
978 	mm_request_receive_expect(pmonitor->m_recvfd,
979 	    MONITOR_ANS_BSDAUTHQUERY, m);
980 	if ((r = sshbuf_get_u32(m, &success)) != 0)
981 		fatal_fr(r, "parse success");
982 	if (success == 0) {
983 		debug3_f("no challenge");
984 		sshbuf_free(m);
985 		return (-1);
986 	}
987 
988 	/* Get the challenge, and format the response */
989 	if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
990 		fatal_fr(r, "parse challenge");
991 	sshbuf_free(m);
992 
993 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
994 	(*prompts)[0] = challenge;
995 
996 	debug3_f("received challenge: %s", challenge);
997 
998 	return (0);
999 }
1000 
1001 int
mm_bsdauth_respond(void * ctx,u_int numresponses,char ** responses)1002 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
1003 {
1004 	struct sshbuf *m;
1005 	int r, authok;
1006 
1007 	debug3_f("entering");
1008 	if (numresponses != 1)
1009 		return (-1);
1010 
1011 	if ((m = sshbuf_new()) == NULL)
1012 		fatal_f("sshbuf_new failed");
1013 	if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
1014 		fatal_fr(r, "assemble");
1015 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
1016 
1017 	mm_request_receive_expect(pmonitor->m_recvfd,
1018 	    MONITOR_ANS_BSDAUTHRESPOND, m);
1019 
1020 	if ((r = sshbuf_get_u32(m, &authok)) != 0)
1021 		fatal_fr(r, "parse");
1022 	sshbuf_free(m);
1023 
1024 	return ((authok == 0) ? -1 : 0);
1025 }
1026 
1027 #ifdef SSH_AUDIT_EVENTS
1028 void
mm_audit_event(struct ssh * ssh,ssh_audit_event_t event)1029 mm_audit_event(struct ssh *ssh, ssh_audit_event_t event)
1030 {
1031 	struct sshbuf *m;
1032 	int r;
1033 
1034 	debug3_f("entering");
1035 
1036 	if ((m = sshbuf_new()) == NULL)
1037 		fatal_f("sshbuf_new failed");
1038 	if ((r = sshbuf_put_u32(m, event)) != 0)
1039 		fatal_fr(r, "buffer error");
1040 
1041 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, m);
1042 	sshbuf_free(m);
1043 }
1044 
1045 void
mm_audit_run_command(const char * command)1046 mm_audit_run_command(const char *command)
1047 {
1048 	struct sshbuf *m;
1049 	int r;
1050 
1051 	debug3_f("entering command %s", command);
1052 
1053 	if ((m = sshbuf_new()) == NULL)
1054 		fatal_f("sshbuf_new failed");
1055 	if ((r = sshbuf_put_cstring(m, command)) != 0)
1056 		fatal_fr(r, "buffer error");
1057 
1058 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
1059 	sshbuf_free(m);
1060 }
1061 #endif /* SSH_AUDIT_EVENTS */
1062 
1063 #ifdef GSSAPI
1064 OM_uint32
mm_ssh_gssapi_server_ctx(Gssctxt ** ctx,gss_OID goid)1065 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1066 {
1067 	struct sshbuf *m;
1068 	OM_uint32 major;
1069 	int r;
1070 
1071 	/* Client doesn't get to see the context */
1072 	*ctx = NULL;
1073 
1074 	if ((m = sshbuf_new()) == NULL)
1075 		fatal_f("sshbuf_new failed");
1076 	if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
1077 		fatal_fr(r, "assemble");
1078 
1079 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
1080 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
1081 
1082 	if ((r = sshbuf_get_u32(m, &major)) != 0)
1083 		fatal_fr(r, "parse");
1084 
1085 	sshbuf_free(m);
1086 	return (major);
1087 }
1088 
1089 OM_uint32
mm_ssh_gssapi_accept_ctx(Gssctxt * ctx,gss_buffer_desc * in,gss_buffer_desc * out,OM_uint32 * flagsp)1090 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1091     gss_buffer_desc *out, OM_uint32 *flagsp)
1092 {
1093 	struct sshbuf *m;
1094 	OM_uint32 major;
1095 	u_int flags;
1096 	int r;
1097 
1098 	if ((m = sshbuf_new()) == NULL)
1099 		fatal_f("sshbuf_new failed");
1100 	if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
1101 		fatal_fr(r, "assemble");
1102 
1103 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
1104 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
1105 
1106 	if ((r = sshbuf_get_u32(m, &major)) != 0 ||
1107 	    (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
1108 		fatal_fr(r, "parse");
1109 	if (flagsp != NULL) {
1110 		if ((r = sshbuf_get_u32(m, &flags)) != 0)
1111 			fatal_fr(r, "parse flags");
1112 		*flagsp = flags;
1113 	}
1114 
1115 	sshbuf_free(m);
1116 
1117 	return (major);
1118 }
1119 
1120 OM_uint32
mm_ssh_gssapi_checkmic(Gssctxt * ctx,gss_buffer_t gssbuf,gss_buffer_t gssmic)1121 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1122 {
1123 	struct sshbuf *m;
1124 	OM_uint32 major;
1125 	int r;
1126 
1127 	if ((m = sshbuf_new()) == NULL)
1128 		fatal_f("sshbuf_new failed");
1129 	if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
1130 	    (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
1131 		fatal_fr(r, "assemble");
1132 
1133 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
1134 	mm_request_receive_expect(pmonitor->m_recvfd,
1135 	    MONITOR_ANS_GSSCHECKMIC, m);
1136 
1137 	if ((r = sshbuf_get_u32(m, &major)) != 0)
1138 		fatal_fr(r, "parse");
1139 	sshbuf_free(m);
1140 	return(major);
1141 }
1142 
1143 int
mm_ssh_gssapi_userok(char * user)1144 mm_ssh_gssapi_userok(char *user)
1145 {
1146 	struct sshbuf *m;
1147 	int r, authenticated = 0;
1148 
1149 	if ((m = sshbuf_new()) == NULL)
1150 		fatal_f("sshbuf_new failed");
1151 
1152 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
1153 	mm_request_receive_expect(pmonitor->m_recvfd,
1154 	    MONITOR_ANS_GSSUSEROK, m);
1155 
1156 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
1157 		fatal_fr(r, "parse");
1158 
1159 	sshbuf_free(m);
1160 	debug3_f("user %sauthenticated", authenticated ? "" : "not ");
1161 	return (authenticated);
1162 }
1163 #endif /* GSSAPI */
1164 
1165 /*
1166  * Inform channels layer of permitopen options for a single forwarding
1167  * direction (local/remote).
1168  */
1169 static void
server_process_permitopen_list(struct ssh * ssh,int listen,char ** opens,u_int num_opens)1170 server_process_permitopen_list(struct ssh *ssh, int listen,
1171     char **opens, u_int num_opens)
1172 {
1173 	u_int i;
1174 	int port;
1175 	char *host, *arg, *oarg;
1176 	int where = listen ? FORWARD_REMOTE : FORWARD_LOCAL;
1177 	const char *what = listen ? "permitlisten" : "permitopen";
1178 
1179 	channel_clear_permission(ssh, FORWARD_ADM, where);
1180 	if (num_opens == 0)
1181 		return; /* permit any */
1182 
1183 	/* handle keywords: "any" / "none" */
1184 	if (num_opens == 1 && strcmp(opens[0], "any") == 0)
1185 		return;
1186 	if (num_opens == 1 && strcmp(opens[0], "none") == 0) {
1187 		channel_disable_admin(ssh, where);
1188 		return;
1189 	}
1190 	/* Otherwise treat it as a list of permitted host:port */
1191 	for (i = 0; i < num_opens; i++) {
1192 		oarg = arg = xstrdup(opens[i]);
1193 		host = hpdelim(&arg);
1194 		if (host == NULL)
1195 			fatal_f("missing host in %s", what);
1196 		host = cleanhostname(host);
1197 		if (arg == NULL || ((port = permitopen_port(arg)) < 0))
1198 			fatal_f("bad port number in %s", what);
1199 		/* Send it to channels layer */
1200 		channel_add_permission(ssh, FORWARD_ADM,
1201 		    where, host, port);
1202 		free(oarg);
1203 	}
1204 }
1205 
1206 /*
1207  * Inform channels layer of permitopen options from configuration.
1208  */
1209 void
server_process_permitopen(struct ssh * ssh)1210 server_process_permitopen(struct ssh *ssh)
1211 {
1212 	server_process_permitopen_list(ssh, 0,
1213 	    options.permitted_opens, options.num_permitted_opens);
1214 	server_process_permitopen_list(ssh, 1,
1215 	    options.permitted_listens, options.num_permitted_listens);
1216 }
1217 
1218 void
server_process_channel_timeouts(struct ssh * ssh)1219 server_process_channel_timeouts(struct ssh *ssh)
1220 {
1221 	u_int i, secs;
1222 	char *type;
1223 
1224 	debug3_f("setting %u timeouts", options.num_channel_timeouts);
1225 	channel_clear_timeouts(ssh);
1226 	for (i = 0; i < options.num_channel_timeouts; i++) {
1227 		if (parse_pattern_interval(options.channel_timeouts[i],
1228 		    &type, &secs) != 0) {
1229 			fatal_f("internal error: bad timeout %s",
1230 			    options.channel_timeouts[i]);
1231 		}
1232 		channel_add_timeout(ssh, type, secs);
1233 		free(type);
1234 	}
1235 }
1236 
1237 struct connection_info *
server_get_connection_info(struct ssh * ssh,int populate,int use_dns)1238 server_get_connection_info(struct ssh *ssh, int populate, int use_dns)
1239 {
1240 	static struct connection_info ci;
1241 
1242 	if (ssh == NULL || !populate)
1243 		return &ci;
1244 	ci.host = use_dns ? ssh_remote_hostname(ssh) : ssh_remote_ipaddr(ssh);
1245 	ci.address = ssh_remote_ipaddr(ssh);
1246 	ci.laddress = ssh_local_ipaddr(ssh);
1247 	ci.lport = ssh_local_port(ssh);
1248 	ci.rdomain = ssh_packet_rdomain_in(ssh);
1249 	return &ci;
1250 }
1251 
1252