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