2 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <openssl/dh.h>
37 #include "auth-options.h"
43 #include "monitor_mm.h"
44 #include "monitor_wrap.h"
45 #include "monitor_fdpass.h"
52 extern Newkeys *current_keys[];
53 extern z_stream incoming_stream;
54 extern z_stream outgoing_stream;
58 /* State exported from the child */
69 /* Prototype for authentication functions */
71 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
72 int user_key_allowed(struct passwd *, Key *);
73 Key *get_hostkey_by_index(int);
75 void session_pty_cleanup(void *);
77 static Authctxt *authctxt;
80 enum monitor_reqtype type;
82 int (*f)(int, Buffer *);
85 #define MON_PROTOONE 0x0001 /* Used in protocol 1 */
86 #define MON_PROTOTWO 0x0002 /* Used in protocol 2 */
87 #define MON_AUTH 0x0004 /* Authentication Request */
89 #define MON_BOTH (MON_PROTOONE|MON_PROTOTWO)
91 #define MON_PERMIT 0x1000 /* Request is permitted */
93 struct mon_table mon_dispatch_proto20[] = {
94 {MONITOR_REQ_MODULI, MON_PROTOTWO, mm_answer_moduli},
95 {MONITOR_REQ_SIGN, MON_PROTOTWO, mm_answer_sign},
96 {MONITOR_REQ_PWNAM, MON_BOTH, mm_answer_pwnamallow},
97 {MONITOR_REQ_AUTHSERV, MON_BOTH, mm_answer_authserv},
98 {MONITOR_REQ_AUTHPASSWORD, MON_BOTH | MON_AUTH, mm_answer_authpassword},
99 {MONITOR_REQ_KEYALLOWED, MON_BOTH | MON_AUTH, mm_answer_keyallowed},
100 {MONITOR_REQ_KEYVERIFY, MON_BOTH | MON_AUTH, mm_answer_keyverify},
104 struct mon_table mon_dispatch_postauth20[] = {
105 {MONITOR_REQ_MODULI, MON_PROTOTWO, mm_answer_moduli},
106 {MONITOR_REQ_SIGN, MON_PROTOTWO, mm_answer_sign},
107 {MONITOR_REQ_PTY, MON_BOTH, mm_answer_pty},
108 {MONITOR_REQ_TERM, MON_BOTH, mm_answer_term},
112 struct mon_table mon_dispatch_proto15[] = {
116 struct mon_table *mon_dispatch;
118 /* Specifies if a certain message is allowed at the moment */
121 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
123 while (ent->f != NULL) {
124 if (ent->type == type) {
125 ent->flags &= ~MON_PERMIT;
126 ent->flags |= permit ? MON_PERMIT : 0;
134 monitor_permit_authentications(int permit)
136 struct mon_table *ent = mon_dispatch;
138 while (ent->f != NULL) {
139 if (ent->flags & MON_AUTH) {
140 ent->flags &= ~MON_PERMIT;
141 ent->flags |= permit ? MON_PERMIT : 0;
147 #define FD_CLOSEONEXEC(x) do { \
148 if (fcntl(x, F_SETFD, 1) == -1) \
149 fatal("fcntl(%d, F_SETFD)", x); \
153 monitor_socketpair(int *pair)
155 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, pair) == -1)
156 fatal("%s: socketpair", __FUNCTION__);
157 FD_CLOSEONEXEC(pair[0]);
158 FD_CLOSEONEXEC(pair[1]);
162 monitor_child_preauth(int socket)
164 debug3("preauth child monitor started");
167 mon_dispatch = mon_dispatch_proto20;
169 /* Permit requests for moduli and signatures */
170 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
171 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
173 mon_dispatch = mon_dispatch_proto15;
175 authctxt = authctxt_new();
177 /* The first few requests do not require asynchronous access */
179 if (monitor_read(socket, mon_dispatch))
183 debug("%s: %s has been authenticated by privileged process",
184 __FUNCTION__, authctxt->user);
187 mm_get_keystate(socket);
196 monitor_child_postauth(int socket)
199 mon_dispatch = mon_dispatch_postauth20;
201 /* Permit requests for moduli and signatures */
202 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
203 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
204 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
207 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
209 mon_dispatch = mon_dispatch_proto15;
212 if (monitor_read(socket, mon_dispatch))
218 monitor_read(int socket, struct mon_table *ent)
226 mm_request_receive(socket, &m);
227 type = buffer_get_char(&m);
229 debug3("%s: checking request %d", __FUNCTION__, type);
231 while (ent->f != NULL) {
232 if (ent->type == type)
237 if (ent->f != NULL) {
238 if (!(ent->flags & MON_PERMIT))
239 fatal("%s: unpermitted request %d", __FUNCTION__,
241 ret = (*ent->f)(socket, &m);
246 fatal("%s: unsupported request: %d\n", __FUNCTION__, type);
253 mm_answer_moduli(int socket, Buffer *m)
258 /* Turn off requests for moduli */
259 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 0);
261 min = buffer_get_int(m);
262 want = buffer_get_int(m);
263 max = buffer_get_int(m);
265 debug3("%s: got parameters: %d %d %d",
266 __FUNCTION__, min, want, max);
267 /* We need to check here, too, in case the child got corrupted */
268 if (max < min || want < min || max < want)
269 fatal("%s: bad parameters: %d %d %d",
270 __FUNCTION__, min, want, max);
274 dh = choose_dh(min, want, max);
276 buffer_put_char(m, 0);
279 /* Send first bignum */
280 buffer_put_char(m, 1);
281 buffer_put_bignum2(m, dh->p);
282 buffer_put_bignum2(m, dh->g);
286 mm_request_send(socket, MONITOR_ANS_MODULI, m);
291 mm_answer_sign(int socket, Buffer *m)
296 u_int siglen, datlen;
299 debug3("%s", __FUNCTION__);
301 keyid = buffer_get_int(m);
302 p = buffer_get_string(m, &datlen);
304 if ((key = get_hostkey_by_index(keyid)) == NULL)
305 fatal("%s: no hostkey from index %d", __FUNCTION__, keyid);
306 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
307 fatal("%s: key_sign failed", __FUNCTION__);
309 debug3("%s: signature %p(%d)", __FUNCTION__, signature, siglen);
312 buffer_put_string(m, signature, siglen);
317 /* Turn on permissions for getpwnam */
318 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
320 mm_request_send(socket, MONITOR_ANS_SIGN, m);
324 /* Retrieves the password entry and also checks if the user is permitted */
327 mm_answer_pwnamallow(int socket, Buffer *m)
330 struct passwd *pwent;
333 debug3("%s", __FUNCTION__);
335 if (authctxt->attempt++ != 0)
336 fatal("%s: multiple attempts for getpwnam", __FUNCTION__);
338 login = buffer_get_string(m, NULL);
340 /* XXX - probably latch the username here */
341 pwent = getpwnam(login);
342 authctxt->user = xstrdup(login);
343 setproctitle("%s [priv]", pwent ? login : "unknown");
346 /* Allow service/style information on the auth context */
347 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
352 buffer_put_char(m, 0);
353 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
357 /* Check if we permit this user */
358 allowed = allowed_user(pwent);
361 authctxt->pw = pwcopy(pwent);
364 buffer_put_char(m, allowed);
365 buffer_put_string(m, pwent, sizeof(struct passwd));
366 buffer_put_cstring(m, pwent->pw_name);
367 buffer_put_cstring(m, "*");
368 buffer_put_cstring(m, pwent->pw_gecos);
369 buffer_put_cstring(m, pwent->pw_class);
370 buffer_put_cstring(m, pwent->pw_dir);
371 buffer_put_cstring(m, pwent->pw_shell);
373 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __FUNCTION__, allowed);
374 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
380 mm_answer_authserv(int socket, Buffer *m)
382 /* Disallow service/style information on the auth context */
383 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 0);
385 monitor_permit_authentications(1);
387 authctxt->service = buffer_get_string(m, NULL);
388 authctxt->style = buffer_get_string(m, NULL);
389 if (strlen(authctxt->style) == 0) {
390 xfree(authctxt->style);
391 authctxt->style = NULL;
394 debug3("%s: service=%s, style=%s",
395 __FUNCTION__, authctxt->service, authctxt->style);
401 mm_answer_authpassword(int socket, Buffer *m)
406 passwd = buffer_get_string(m, NULL);
407 /* Only authenticate if the context is valid */
408 authenticated = authctxt->valid && auth_password(authctxt, passwd);
409 memset(passwd, 0, strlen(passwd));
413 buffer_put_int(m, authenticated);
415 debug3("%s: sending result %d", __FUNCTION__, authenticated);
416 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
418 /* Causes monitor loop to terminate if authenticated */
419 return (authenticated);
423 mm_answer_keyallowed(int socket, Buffer *m)
426 u_char *cuser, *chost, *blob;
428 enum mm_keytype type = 0;
431 debug3("%s entering", __FUNCTION__);
433 type = buffer_get_int(m);
434 cuser = buffer_get_string(m, NULL);
435 chost = buffer_get_string(m, NULL);
436 blob = buffer_get_string(m, &bloblen);
438 key = key_from_blob(blob, bloblen);
440 debug3("%s: key_from_blob: %p", __FUNCTION__, key);
442 if (key != NULL && authctxt->pw != NULL) {
445 allowed = user_key_allowed(authctxt->pw, key);
448 allowed = hostbased_key_allowed(authctxt->pw,
452 fatal("%s: unknown key type %d", __FUNCTION__,
462 debug3("%s: key %p is %s",
463 __FUNCTION__, key, allowed ? "allowed" : "disallowed");
466 buffer_put_int(m, allowed);
468 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
473 mm_answer_keyverify(int socket, Buffer *m)
476 u_char *signature, *data, *cuser, *chost, *blob;
477 u_int signaturelen, datalen, bloblen;
481 type = buffer_get_int(m);
482 cuser = buffer_get_string(m, NULL);
483 chost = buffer_get_string(m, NULL);
484 blob = buffer_get_string(m, &bloblen);
485 signature = buffer_get_string(m, &signaturelen);
486 data = buffer_get_string(m, &datalen);
488 key = key_from_blob(blob, bloblen);
490 fatal("%s: bad public key blob", __FUNCTION__);
492 if (authctxt->pw == NULL || !user_key_allowed(authctxt->pw, key))
493 fatal("%s: user not allowed", __FUNCTION__);
494 verified = key_verify(key, signature, signaturelen, data, datalen);
495 debug3("%s: key %p signature %s",
496 __FUNCTION__, key, verified ? "verified" : "unverified");
506 buffer_put_int(m, verified);
507 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
513 mm_answer_pty(int socket, Buffer *m)
518 debug3("%s entering", __FUNCTION__);
524 s->authctxt = authctxt;
525 s->pw = authctxt->pw;
526 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
529 fatal_add_cleanup(session_pty_cleanup, (void *)s);
530 pty_setowner(authctxt->pw, s->tty);
532 buffer_put_int(m, 1);
533 buffer_put_cstring(m, s->tty);
534 mm_request_send(socket, MONITOR_ANS_PTY, m);
536 mm_send_fd(mm_sendfd, s->ptyfd);
537 mm_send_fd(mm_sendfd, s->ttyfd);
543 buffer_put_int(m, 0);
544 mm_request_send(socket, MONITOR_ANS_PTY, m);
549 mm_answer_term(int socket, Buffer *req)
551 debug3("%s: tearing down sessions", __FUNCTION__);
553 /* The child is terminating */
554 session_destroy_all();
560 mm_apply_keystate(struct mm_master *mm)
562 /* XXX - delegate to child? */
563 set_newkeys(MODE_IN);
564 set_newkeys(MODE_OUT);
566 packet_set_keycontext(MODE_OUT, child_state.keyout);
567 xfree(child_state.keyout);
568 packet_set_keycontext(MODE_IN, child_state.keyin);
569 xfree(child_state.keyin);
571 memcpy(&incoming_stream, &child_state.incoming,
572 sizeof(incoming_stream));
573 memcpy(&outgoing_stream, &child_state.outgoing,
574 sizeof(outgoing_stream));
576 /* Update with new address */
577 mm_init_compression(mm);
580 /* This function requries careful sanity checking */
583 mm_get_keystate(int socket)
589 debug3("%s: Waiting for new keys", __FUNCTION__);
592 mm_request_receive_expect(socket, MONITOR_REQ_KEYEXPORT, &m);
594 blob = buffer_get_string(&m, &bloblen);
595 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
598 debug3("%s: Waiting for second key", __FUNCTION__);
599 blob = buffer_get_string(&m, &bloblen);
600 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
603 /* Now get sequence numbers for the packets */
604 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
605 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
607 /* Get the key context */
608 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
609 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
611 debug3("%s: Getting compression state", __FUNCTION__);
612 /* Get compression state */
613 p = buffer_get_string(&m, &plen);
614 if (plen != sizeof(child_state.outgoing))
615 fatal("%s: bad request size", __FUNCTION__);
616 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
619 p = buffer_get_string(&m, &plen);
620 if (plen != sizeof(child_state.incoming))
621 fatal("%s: bad request size", __FUNCTION__);
622 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
629 /* Allocation functions for zlib */
631 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
635 address = mm_malloc(mm, size * ncount);
641 mm_zfree(struct mm_master *mm, void *address)
643 mm_free(mm, address);
647 mm_init_compression(struct mm_master *mm)
649 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
650 outgoing_stream.zfree = (free_func)mm_zfree;
651 outgoing_stream.opaque = mm;
653 incoming_stream.zalloc = (alloc_func)mm_zalloc;
654 incoming_stream.zfree = (free_func)mm_zfree;
655 incoming_stream.opaque = mm;