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