1 /* $OpenBSD: monitor_wrap.c,v 1.107 2018/07/20 03:46:34 djm Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <[email protected]>
4 * Copyright 2002 Markus Friedl <[email protected]>
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 ((p = BN_new()) == NULL)
206 fatal("%s: BN_new failed", __func__);
207 if ((g = BN_new()) == NULL)
208 fatal("%s: BN_new failed", __func__);
209 if ((r = sshbuf_get_bignum2(m, p)) != 0 ||
210 (r = sshbuf_get_bignum2(m, g)) != 0)
211 fatal("%s: buffer error: %s", __func__, ssh_err(r));
212
213 debug3("%s: remaining %zu", __func__, sshbuf_len(m));
214 sshbuf_free(m);
215
216 return (dh_new_group(g, p));
217 }
218 #endif
219
220 int
mm_sshkey_sign(struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,const char * hostkey_alg,u_int compat)221 mm_sshkey_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
222 const u_char *data, size_t datalen, const char *hostkey_alg, u_int compat)
223 {
224 struct kex *kex = *pmonitor->m_pkex;
225 struct sshbuf *m;
226 u_int ndx = kex->host_key_index(key, 0, active_state);
227 int r;
228
229 debug3("%s entering", __func__);
230
231 if ((m = sshbuf_new()) == NULL)
232 fatal("%s: sshbuf_new failed", __func__);
233 if ((r = sshbuf_put_u32(m, ndx)) != 0 ||
234 (r = sshbuf_put_string(m, data, datalen)) != 0 ||
235 (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
236 (r = sshbuf_put_u32(m, compat)) != 0)
237 fatal("%s: buffer error: %s", __func__, ssh_err(r));
238
239 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
240
241 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
242 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
243 if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
244 fatal("%s: buffer error: %s", __func__, ssh_err(r));
245 sshbuf_free(m);
246
247 return (0);
248 }
249
250 #ifdef HAVE_LOGIN_CAP
251 login_cap_t *
mm_login_getpwclass(const struct passwd * pwent)252 mm_login_getpwclass(const struct passwd *pwent)
253 {
254 int r;
255 struct sshbuf *m;
256 char rc;
257 login_cap_t *lc;
258
259 debug3("%s entering", __func__);
260
261 if ((m = sshbuf_new()) == NULL)
262 fatal("%s: sshbuf_new failed", __func__);
263 if ((r = sshbuf_put_passwd(m, pwent)) != 0)
264 fatal("%s: buffer error: %s", __func__, ssh_err(r));
265
266 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GETPWCLASS, m);
267
268 debug3("%s: waiting for MONITOR_ANS_GETPWCLASS", __func__);
269 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GETPWCLASS, m);
270
271 if ((r = sshbuf_get_u8(m, &rc)) != 0)
272 fatal("%s: buffer error: %s", __func__, ssh_err(r));
273
274 if (rc == 0) {
275 lc = NULL;
276 goto out;
277 }
278
279 lc = xmalloc(sizeof(*lc));
280 if ((r = sshbuf_get_cstring(m, &lc->lc_class, NULL)) != 0 ||
281 (r = sshbuf_get_cstring(m, &lc->lc_cap, NULL)) != 0 ||
282 (r = sshbuf_get_cstring(m, &lc->lc_style, NULL)) != 0)
283 fatal("%s: buffer error: %s", __func__, ssh_err(r));
284
285 out:
286 sshbuf_free(m);
287
288 return (lc);
289 }
290 #endif
291
292 #ifdef HAVE_LOGIN_CAP
293 void
mm_login_close(login_cap_t * lc)294 mm_login_close(login_cap_t *lc)
295 {
296 if (lc == NULL)
297 return;
298 free(lc->lc_style);
299 free(lc->lc_class);
300 free(lc->lc_cap);
301 free(lc);
302 }
303 #endif
304
305 struct passwd *
mm_getpwnamallow(const char * username)306 mm_getpwnamallow(const char *username)
307 {
308 struct ssh *ssh = active_state; /* XXX */
309 struct sshbuf *m;
310 struct passwd *pw;
311 size_t len;
312 u_int i;
313 ServerOptions *newopts;
314 int r;
315 u_char ok;
316 const u_char *p;
317
318 debug3("%s entering", __func__);
319
320 if ((m = sshbuf_new()) == NULL)
321 fatal("%s: sshbuf_new failed", __func__);
322 if ((r = sshbuf_put_cstring(m, username)) != 0)
323 fatal("%s: buffer error: %s", __func__, ssh_err(r));
324
325 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
326
327 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
328 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
329
330 if ((r = sshbuf_get_u8(m, &ok)) != 0)
331 fatal("%s: buffer error: %s", __func__, ssh_err(r));
332 if (ok == 0) {
333 pw = NULL;
334 goto out;
335 }
336
337 pw = sshbuf_get_passwd(m);
338 if (pw == NULL)
339 fatal("%s: receive get struct passwd failed", __func__);
340
341 out:
342 /* copy options block as a Match directive may have changed some */
343 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
344 fatal("%s: buffer error: %s", __func__, ssh_err(r));
345 if (len != sizeof(*newopts))
346 fatal("%s: option block size mismatch", __func__);
347 newopts = xcalloc(sizeof(*newopts), 1);
348 memcpy(newopts, p, sizeof(*newopts));
349
350 #define M_CP_STROPT(x) do { \
351 if (newopts->x != NULL) { \
352 if ((r = sshbuf_get_cstring(m, \
353 &newopts->x, NULL)) != 0) \
354 fatal("%s: buffer error: %s", \
355 __func__, ssh_err(r)); \
356 } \
357 } while (0)
358 #define M_CP_STRARRAYOPT(x, nx) do { \
359 newopts->x = newopts->nx == 0 ? \
360 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
361 for (i = 0; i < newopts->nx; i++) { \
362 if ((r = sshbuf_get_cstring(m, \
363 &newopts->x[i], NULL)) != 0) \
364 fatal("%s: buffer error: %s", \
365 __func__, ssh_err(r)); \
366 } \
367 } while (0)
368 /* See comment in servconf.h */
369 COPY_MATCH_STRING_OPTS();
370 #undef M_CP_STROPT
371 #undef M_CP_STRARRAYOPT
372
373 copy_set_server_options(&options, newopts, 1);
374 log_change_level(options.log_level);
375 process_permitopen(ssh, &options);
376 free(newopts);
377
378 sshbuf_free(m);
379
380 return (pw);
381 }
382
383 char *
mm_auth2_read_banner(void)384 mm_auth2_read_banner(void)
385 {
386 struct sshbuf *m;
387 char *banner;
388 int r;
389
390 debug3("%s entering", __func__);
391
392 if ((m = sshbuf_new()) == NULL)
393 fatal("%s: sshbuf_new failed", __func__);
394 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
395 sshbuf_reset(m);
396
397 mm_request_receive_expect(pmonitor->m_recvfd,
398 MONITOR_ANS_AUTH2_READ_BANNER, m);
399 if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
400 fatal("%s: buffer error: %s", __func__, ssh_err(r));
401 sshbuf_free(m);
402
403 /* treat empty banner as missing banner */
404 if (strlen(banner) == 0) {
405 free(banner);
406 banner = NULL;
407 }
408 return (banner);
409 }
410
411 /* Inform the privileged process about service and style */
412
413 void
mm_inform_authserv(char * service,char * style)414 mm_inform_authserv(char *service, char *style)
415 {
416 struct sshbuf *m;
417 int r;
418
419 debug3("%s entering", __func__);
420
421 if ((m = sshbuf_new()) == NULL)
422 fatal("%s: sshbuf_new failed", __func__);
423 if ((r = sshbuf_put_cstring(m, service)) != 0 ||
424 (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
425 fatal("%s: buffer error: %s", __func__, ssh_err(r));
426
427 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
428
429 sshbuf_free(m);
430 }
431
432 /* Do the password authentication */
433 int
mm_auth_password(struct ssh * ssh,char * password)434 mm_auth_password(struct ssh *ssh, char *password)
435 {
436 struct sshbuf *m;
437 int r, authenticated = 0;
438 #ifdef USE_PAM
439 u_int maxtries = 0;
440 #endif
441
442 debug3("%s entering", __func__);
443
444 if ((m = sshbuf_new()) == NULL)
445 fatal("%s: sshbuf_new failed", __func__);
446 if ((r = sshbuf_put_cstring(m, password)) != 0)
447 fatal("%s: buffer error: %s", __func__, ssh_err(r));
448 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
449
450 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
451 mm_request_receive_expect(pmonitor->m_recvfd,
452 MONITOR_ANS_AUTHPASSWORD, m);
453
454 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
455 fatal("%s: buffer error: %s", __func__, ssh_err(r));
456 #ifdef USE_PAM
457 if ((r = sshbuf_get_u32(m, &maxtries)) != 0)
458 fatal("%s: buffer error: %s", __func__, ssh_err(r));
459 if (maxtries > INT_MAX)
460 fatal("%s: bad maxtries %u", __func__, maxtries);
461 sshpam_set_maxtries_reached(maxtries);
462 #endif
463
464 sshbuf_free(m);
465
466 debug3("%s: user %sauthenticated",
467 __func__, authenticated ? "" : "not ");
468 return (authenticated);
469 }
470
471 int
mm_user_key_allowed(struct ssh * ssh,struct passwd * pw,struct sshkey * key,int pubkey_auth_attempt,struct sshauthopt ** authoptp)472 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
473 int pubkey_auth_attempt, struct sshauthopt **authoptp)
474 {
475 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
476 pubkey_auth_attempt, authoptp));
477 }
478
479 int
mm_hostbased_key_allowed(struct passwd * pw,const char * user,const char * host,struct sshkey * key)480 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
481 struct sshkey *key)
482 {
483 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
484 }
485
486 int
mm_key_allowed(enum mm_keytype type,const char * user,const char * host,struct sshkey * key,int pubkey_auth_attempt,struct sshauthopt ** authoptp)487 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
488 struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
489 {
490 struct sshbuf *m;
491 int r, allowed = 0;
492 struct sshauthopt *opts = NULL;
493
494 debug3("%s entering", __func__);
495
496 if (authoptp != NULL)
497 *authoptp = NULL;
498
499 if ((m = sshbuf_new()) == NULL)
500 fatal("%s: sshbuf_new failed", __func__);
501 if ((r = sshbuf_put_u32(m, type)) != 0 ||
502 (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
503 (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
504 (r = sshkey_puts(key, m)) != 0 ||
505 (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
506 fatal("%s: buffer error: %s", __func__, ssh_err(r));
507
508 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
509
510 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
511 mm_request_receive_expect(pmonitor->m_recvfd,
512 MONITOR_ANS_KEYALLOWED, m);
513
514 if ((r = sshbuf_get_u32(m, &allowed)) != 0)
515 fatal("%s: buffer error: %s", __func__, ssh_err(r));
516 if (allowed && type == MM_USERKEY) {
517 if ((r = sshauthopt_deserialise(m, &opts)) != 0)
518 fatal("%s: sshauthopt_deserialise: %s",
519 __func__, ssh_err(r));
520 }
521 sshbuf_free(m);
522
523 if (authoptp != NULL) {
524 *authoptp = opts;
525 opts = NULL;
526 }
527 sshauthopt_free(opts);
528
529 return allowed;
530 }
531
532 /*
533 * This key verify needs to send the key type along, because the
534 * privileged parent makes the decision if the key is allowed
535 * for authentication.
536 */
537
538 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)539 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
540 const u_char *data, size_t datalen, const char *sigalg, u_int compat)
541 {
542 struct sshbuf *m;
543 u_int encoded_ret = 0;
544 int r;
545
546 debug3("%s entering", __func__);
547
548
549 if ((m = sshbuf_new()) == NULL)
550 fatal("%s: sshbuf_new failed", __func__);
551 if ((r = sshkey_puts(key, m)) != 0 ||
552 (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
553 (r = sshbuf_put_string(m, data, datalen)) != 0 ||
554 (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
555 fatal("%s: buffer error: %s", __func__, ssh_err(r));
556
557 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
558
559 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
560 mm_request_receive_expect(pmonitor->m_recvfd,
561 MONITOR_ANS_KEYVERIFY, m);
562
563 if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0)
564 fatal("%s: buffer error: %s", __func__, ssh_err(r));
565
566 sshbuf_free(m);
567
568 if (encoded_ret != 0)
569 return SSH_ERR_SIGNATURE_INVALID;
570 return 0;
571 }
572
573 void
mm_send_keystate(struct monitor * monitor)574 mm_send_keystate(struct monitor *monitor)
575 {
576 struct ssh *ssh = active_state; /* XXX */
577 struct sshbuf *m;
578 int r;
579
580 if ((m = sshbuf_new()) == NULL)
581 fatal("%s: sshbuf_new failed", __func__);
582 if ((r = ssh_packet_get_state(ssh, m)) != 0)
583 fatal("%s: get_state failed: %s",
584 __func__, ssh_err(r));
585 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
586 debug3("%s: Finished sending state", __func__);
587 sshbuf_free(m);
588 }
589
590 int
mm_pty_allocate(int * ptyfd,int * ttyfd,char * namebuf,size_t namebuflen)591 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
592 {
593 struct sshbuf *m;
594 char *p, *msg;
595 int success = 0, tmp1 = -1, tmp2 = -1, r;
596
597 /* Kludge: ensure there are fds free to receive the pty/tty */
598 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
599 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
600 error("%s: cannot allocate fds for pty", __func__);
601 if (tmp1 > 0)
602 close(tmp1);
603 if (tmp2 > 0)
604 close(tmp2);
605 return 0;
606 }
607 close(tmp1);
608 close(tmp2);
609
610 if ((m = sshbuf_new()) == NULL)
611 fatal("%s: sshbuf_new failed", __func__);
612 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
613
614 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
615 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
616
617 if ((r = sshbuf_get_u32(m, &success)) != 0)
618 fatal("%s: buffer error: %s", __func__, ssh_err(r));
619 if (success == 0) {
620 debug3("%s: pty alloc failed", __func__);
621 sshbuf_free(m);
622 return (0);
623 }
624 if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
625 (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
626 fatal("%s: buffer error: %s", __func__, ssh_err(r));
627 sshbuf_free(m);
628
629 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
630 free(p);
631
632 if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
633 fatal("%s: buffer error: %s", __func__, ssh_err(r));
634 free(msg);
635
636 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
637 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
638 fatal("%s: receive fds failed", __func__);
639
640 /* Success */
641 return (1);
642 }
643
644 void
mm_session_pty_cleanup2(Session * s)645 mm_session_pty_cleanup2(Session *s)
646 {
647 struct sshbuf *m;
648 int r;
649
650 if (s->ttyfd == -1)
651 return;
652 if ((m = sshbuf_new()) == NULL)
653 fatal("%s: sshbuf_new failed", __func__);
654 if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
655 fatal("%s: buffer error: %s", __func__, ssh_err(r));
656 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
657 sshbuf_free(m);
658
659 /* closed dup'ed master */
660 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
661 error("close(s->ptymaster/%d): %s",
662 s->ptymaster, strerror(errno));
663
664 /* unlink pty from session */
665 s->ttyfd = -1;
666 }
667
668 #ifdef USE_PAM
669 void
mm_start_pam(Authctxt * authctxt)670 mm_start_pam(Authctxt *authctxt)
671 {
672 struct sshbuf *m;
673
674 debug3("%s entering", __func__);
675 if (!options.use_pam)
676 fatal("UsePAM=no, but ended up in %s anyway", __func__);
677 if ((m = sshbuf_new()) == NULL)
678 fatal("%s: sshbuf_new failed", __func__);
679 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m);
680
681 sshbuf_free(m);
682 }
683
684 u_int
mm_do_pam_account(void)685 mm_do_pam_account(void)
686 {
687 struct sshbuf *m;
688 u_int ret;
689 char *msg;
690 size_t msglen;
691 int r;
692
693 debug3("%s entering", __func__);
694 if (!options.use_pam)
695 fatal("UsePAM=no, but ended up in %s anyway", __func__);
696
697 if ((m = sshbuf_new()) == NULL)
698 fatal("%s: sshbuf_new failed", __func__);
699 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, m);
700
701 mm_request_receive_expect(pmonitor->m_recvfd,
702 MONITOR_ANS_PAM_ACCOUNT, m);
703 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
704 (r = sshbuf_get_cstring(m, &msg, &msglen)) != 0 ||
705 (r = sshbuf_put(loginmsg, msg, msglen)) != 0)
706 fatal("%s: buffer error: %s", __func__, ssh_err(r));
707
708 free(msg);
709 sshbuf_free(m);
710
711 debug3("%s returning %d", __func__, ret);
712
713 return (ret);
714 }
715
716 void *
mm_sshpam_init_ctx(Authctxt * authctxt)717 mm_sshpam_init_ctx(Authctxt *authctxt)
718 {
719 struct sshbuf *m;
720 int r, success;
721
722 debug3("%s", __func__);
723 if ((m = sshbuf_new()) == NULL)
724 fatal("%s: sshbuf_new failed", __func__);
725 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, m);
726 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
727 mm_request_receive_expect(pmonitor->m_recvfd,
728 MONITOR_ANS_PAM_INIT_CTX, m);
729 if ((r = sshbuf_get_u32(m, &success)) != 0)
730 fatal("%s: buffer error: %s", __func__, ssh_err(r));
731 if (success == 0) {
732 debug3("%s: pam_init_ctx failed", __func__);
733 sshbuf_free(m);
734 return (NULL);
735 }
736 sshbuf_free(m);
737 return (authctxt);
738 }
739
740 int
mm_sshpam_query(void * ctx,char ** name,char ** info,u_int * num,char *** prompts,u_int ** echo_on)741 mm_sshpam_query(void *ctx, char **name, char **info,
742 u_int *num, char ***prompts, u_int **echo_on)
743 {
744 struct sshbuf *m;
745 u_int i, n;
746 int r, ret;
747
748 debug3("%s", __func__);
749 if ((m = sshbuf_new()) == NULL)
750 fatal("%s: sshbuf_new failed", __func__);
751 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, m);
752 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
753 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, m);
754 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
755 (r = sshbuf_get_cstring(m, name, NULL)) != 0 ||
756 (r = sshbuf_get_cstring(m, info, NULL)) != 0 ||
757 (r = sshbuf_get_u32(m, &n)) != 0 ||
758 (r = sshbuf_get_u32(m, num)) != 0)
759 fatal("%s: buffer error: %s", __func__, ssh_err(r));
760 debug3("%s: pam_query returned %d", __func__, ret);
761 sshpam_set_maxtries_reached(n);
762 if (*num > PAM_MAX_NUM_MSG)
763 fatal("%s: received %u PAM messages, expected <= %u",
764 __func__, *num, PAM_MAX_NUM_MSG);
765 *prompts = xcalloc((*num + 1), sizeof(char *));
766 *echo_on = xcalloc((*num + 1), sizeof(u_int));
767 for (i = 0; i < *num; ++i) {
768 if ((r = sshbuf_get_cstring(m, &((*prompts)[i]), NULL)) != 0 ||
769 (r = sshbuf_get_u32(m, &((*echo_on)[i]))) != 0)
770 fatal("%s: buffer error: %s", __func__, ssh_err(r));
771 }
772 sshbuf_free(m);
773 return (ret);
774 }
775
776 int
mm_sshpam_respond(void * ctx,u_int num,char ** resp)777 mm_sshpam_respond(void *ctx, u_int num, char **resp)
778 {
779 struct sshbuf *m;
780 u_int n, i;
781 int r, ret;
782
783 debug3("%s", __func__);
784 if ((m = sshbuf_new()) == NULL)
785 fatal("%s: sshbuf_new failed", __func__);
786 if ((r = sshbuf_put_u32(m, num)) != 0)
787 fatal("%s: buffer error: %s", __func__, ssh_err(r));
788 for (i = 0; i < num; ++i) {
789 if ((r = sshbuf_put_cstring(m, resp[i])) != 0)
790 fatal("%s: buffer error: %s", __func__, ssh_err(r));
791 }
792 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, m);
793 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
794 mm_request_receive_expect(pmonitor->m_recvfd,
795 MONITOR_ANS_PAM_RESPOND, m);
796 if ((r = sshbuf_get_u32(m, &n)) != 0)
797 fatal("%s: buffer error: %s", __func__, ssh_err(r));
798 ret = (int)n; /* XXX */
799 debug3("%s: pam_respond returned %d", __func__, ret);
800 sshbuf_free(m);
801 return (ret);
802 }
803
804 void
mm_sshpam_free_ctx(void * ctxtp)805 mm_sshpam_free_ctx(void *ctxtp)
806 {
807 struct sshbuf *m;
808
809 debug3("%s", __func__);
810 if ((m = sshbuf_new()) == NULL)
811 fatal("%s: sshbuf_new failed", __func__);
812 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, m);
813 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
814 mm_request_receive_expect(pmonitor->m_recvfd,
815 MONITOR_ANS_PAM_FREE_CTX, m);
816 sshbuf_free(m);
817 }
818 #endif /* USE_PAM */
819
820 /* Request process termination */
821
822 void
mm_terminate(void)823 mm_terminate(void)
824 {
825 struct sshbuf *m;
826
827 if ((m = sshbuf_new()) == NULL)
828 fatal("%s: sshbuf_new failed", __func__);
829 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
830 sshbuf_free(m);
831 }
832
833 static void
mm_chall_setup(char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)834 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
835 char ***prompts, u_int **echo_on)
836 {
837 *name = xstrdup("");
838 *infotxt = xstrdup("");
839 *numprompts = 1;
840 *prompts = xcalloc(*numprompts, sizeof(char *));
841 *echo_on = xcalloc(*numprompts, sizeof(u_int));
842 (*echo_on)[0] = 0;
843 }
844
845 int
mm_bsdauth_query(void * ctx,char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)846 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
847 u_int *numprompts, char ***prompts, u_int **echo_on)
848 {
849 struct sshbuf *m;
850 u_int success;
851 char *challenge;
852 int r;
853
854 debug3("%s: entering", __func__);
855
856 if ((m = sshbuf_new()) == NULL)
857 fatal("%s: sshbuf_new failed", __func__);
858 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
859
860 mm_request_receive_expect(pmonitor->m_recvfd,
861 MONITOR_ANS_BSDAUTHQUERY, m);
862 if ((r = sshbuf_get_u32(m, &success)) != 0)
863 fatal("%s: buffer error: %s", __func__, ssh_err(r));
864 if (success == 0) {
865 debug3("%s: no challenge", __func__);
866 sshbuf_free(m);
867 return (-1);
868 }
869
870 /* Get the challenge, and format the response */
871 if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
872 fatal("%s: buffer error: %s", __func__, ssh_err(r));
873 sshbuf_free(m);
874
875 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
876 (*prompts)[0] = challenge;
877
878 debug3("%s: received challenge: %s", __func__, challenge);
879
880 return (0);
881 }
882
883 int
mm_bsdauth_respond(void * ctx,u_int numresponses,char ** responses)884 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
885 {
886 struct sshbuf *m;
887 int r, authok;
888
889 debug3("%s: entering", __func__);
890 if (numresponses != 1)
891 return (-1);
892
893 if ((m = sshbuf_new()) == NULL)
894 fatal("%s: sshbuf_new failed", __func__);
895 if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
896 fatal("%s: buffer error: %s", __func__, ssh_err(r));
897 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
898
899 mm_request_receive_expect(pmonitor->m_recvfd,
900 MONITOR_ANS_BSDAUTHRESPOND, m);
901
902 if ((r = sshbuf_get_u32(m, &authok)) != 0)
903 fatal("%s: buffer error: %s", __func__, ssh_err(r));
904 sshbuf_free(m);
905
906 return ((authok == 0) ? -1 : 0);
907 }
908
909 #ifdef SSH_AUDIT_EVENTS
910 void
mm_audit_event(ssh_audit_event_t event)911 mm_audit_event(ssh_audit_event_t event)
912 {
913 struct sshbuf *m;
914 int r;
915
916 debug3("%s entering", __func__);
917
918 if ((m = sshbuf_new()) == NULL)
919 fatal("%s: sshbuf_new failed", __func__);
920 if ((r = sshbuf_put_u32(m, event)) != 0)
921 fatal("%s: buffer error: %s", __func__, ssh_err(r));
922
923 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, m);
924 sshbuf_free(m);
925 }
926
927 void
mm_audit_run_command(const char * command)928 mm_audit_run_command(const char *command)
929 {
930 struct sshbuf *m;
931 int r;
932
933 debug3("%s entering command %s", __func__, command);
934
935 if ((m = sshbuf_new()) == NULL)
936 fatal("%s: sshbuf_new failed", __func__);
937 if ((r = sshbuf_put_cstring(m, command)) != 0)
938 fatal("%s: buffer error: %s", __func__, ssh_err(r));
939
940 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
941 sshbuf_free(m);
942 }
943 #endif /* SSH_AUDIT_EVENTS */
944
945 #ifdef GSSAPI
946 OM_uint32
mm_ssh_gssapi_server_ctx(Gssctxt ** ctx,gss_OID goid)947 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
948 {
949 struct sshbuf *m;
950 OM_uint32 major;
951 int r;
952
953 /* Client doesn't get to see the context */
954 *ctx = NULL;
955
956 if ((m = sshbuf_new()) == NULL)
957 fatal("%s: sshbuf_new failed", __func__);
958 if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
959 fatal("%s: buffer error: %s", __func__, ssh_err(r));
960
961 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
962 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
963
964 if ((r = sshbuf_get_u32(m, &major)) != 0)
965 fatal("%s: buffer error: %s", __func__, ssh_err(r));
966
967 sshbuf_free(m);
968 return (major);
969 }
970
971 OM_uint32
mm_ssh_gssapi_accept_ctx(Gssctxt * ctx,gss_buffer_desc * in,gss_buffer_desc * out,OM_uint32 * flagsp)972 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
973 gss_buffer_desc *out, OM_uint32 *flagsp)
974 {
975 struct sshbuf *m;
976 OM_uint32 major;
977 u_int flags;
978 int r;
979
980 if ((m = sshbuf_new()) == NULL)
981 fatal("%s: sshbuf_new failed", __func__);
982 if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
983 fatal("%s: buffer error: %s", __func__, ssh_err(r));
984
985 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
986 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
987
988 if ((r = sshbuf_get_u32(m, &major)) != 0 ||
989 (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
990 fatal("%s: buffer error: %s", __func__, ssh_err(r));
991 if (flagsp != NULL) {
992 if ((r = sshbuf_get_u32(m, &flags)) != 0)
993 fatal("%s: buffer error: %s", __func__, ssh_err(r));
994 *flagsp = flags;
995 }
996
997 sshbuf_free(m);
998
999 return (major);
1000 }
1001
1002 OM_uint32
mm_ssh_gssapi_checkmic(Gssctxt * ctx,gss_buffer_t gssbuf,gss_buffer_t gssmic)1003 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1004 {
1005 struct sshbuf *m;
1006 OM_uint32 major;
1007 int r;
1008
1009 if ((m = sshbuf_new()) == NULL)
1010 fatal("%s: sshbuf_new failed", __func__);
1011 if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
1012 (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
1013 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1014
1015 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
1016 mm_request_receive_expect(pmonitor->m_recvfd,
1017 MONITOR_ANS_GSSCHECKMIC, m);
1018
1019 if ((r = sshbuf_get_u32(m, &major)) != 0)
1020 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1021 sshbuf_free(m);
1022 return(major);
1023 }
1024
1025 int
mm_ssh_gssapi_userok(char * user)1026 mm_ssh_gssapi_userok(char *user)
1027 {
1028 struct sshbuf *m;
1029 int r, authenticated = 0;
1030
1031 if ((m = sshbuf_new()) == NULL)
1032 fatal("%s: sshbuf_new failed", __func__);
1033
1034 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
1035 mm_request_receive_expect(pmonitor->m_recvfd,
1036 MONITOR_ANS_GSSUSEROK, m);
1037
1038 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
1039 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1040
1041 sshbuf_free(m);
1042 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1043 return (authenticated);
1044 }
1045 #endif /* GSSAPI */
1046