xref: /titanic_41/usr/src/cmd/ssh/sshd/auth2-hostbased.c (revision f44ef4668fa223436280848405e8aeef639dfe38)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
57c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
67c478bd9Sstevel@tonic-gate  * are met:
77c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
87c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
97c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
107c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
117c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate /*
25*f44ef466Sjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "includes.h"
307c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth2-hostbased.c,v 1.2 2002/05/31 11:35:15 markus Exp $");
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "ssh2.h"
357c478bd9Sstevel@tonic-gate #include "xmalloc.h"
367c478bd9Sstevel@tonic-gate #include "packet.h"
377c478bd9Sstevel@tonic-gate #include "buffer.h"
387c478bd9Sstevel@tonic-gate #include "log.h"
397c478bd9Sstevel@tonic-gate #include "servconf.h"
407c478bd9Sstevel@tonic-gate #include "compat.h"
417c478bd9Sstevel@tonic-gate #include "bufaux.h"
427c478bd9Sstevel@tonic-gate #include "auth.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #ifdef USE_PAM
457c478bd9Sstevel@tonic-gate #include "auth-pam.h"
467c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include "key.h"
497c478bd9Sstevel@tonic-gate #include "canohost.h"
507c478bd9Sstevel@tonic-gate #include "monitor_wrap.h"
517c478bd9Sstevel@tonic-gate #include "pathnames.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* import */
547c478bd9Sstevel@tonic-gate extern ServerOptions options;
557c478bd9Sstevel@tonic-gate extern u_char *session_id2;
567c478bd9Sstevel@tonic-gate extern int session_id2_len;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static void
597c478bd9Sstevel@tonic-gate userauth_hostbased(Authctxt *authctxt)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	Buffer b;
627c478bd9Sstevel@tonic-gate 	Key *key = NULL;
637c478bd9Sstevel@tonic-gate 	char *pkalg, *cuser, *chost, *service;
647c478bd9Sstevel@tonic-gate 	u_char *pkblob, *sig;
657c478bd9Sstevel@tonic-gate 	u_int alen, blen, slen;
667c478bd9Sstevel@tonic-gate 	int pktype;
677c478bd9Sstevel@tonic-gate 	int authenticated = 0;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (!authctxt || !authctxt->method)
707c478bd9Sstevel@tonic-gate 		fatal("%s: missing context", __func__);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	pkalg = packet_get_string(&alen);
737c478bd9Sstevel@tonic-gate 	pkblob = packet_get_string(&blen);
747c478bd9Sstevel@tonic-gate 	chost = packet_get_string(NULL);
757c478bd9Sstevel@tonic-gate 	cuser = packet_get_string(NULL);
767c478bd9Sstevel@tonic-gate 	sig = packet_get_string(&slen);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
797c478bd9Sstevel@tonic-gate 	    cuser, chost, pkalg, slen);
807c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
817c478bd9Sstevel@tonic-gate 	debug("signature:");
827c478bd9Sstevel@tonic-gate 	buffer_init(&b);
837c478bd9Sstevel@tonic-gate 	buffer_append(&b, sig, slen);
847c478bd9Sstevel@tonic-gate 	buffer_dump(&b);
857c478bd9Sstevel@tonic-gate 	buffer_free(&b);
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 	pktype = key_type_from_name(pkalg);
887c478bd9Sstevel@tonic-gate 	if (pktype == KEY_UNSPEC) {
897c478bd9Sstevel@tonic-gate 		/* this is perfectly legal */
907c478bd9Sstevel@tonic-gate 		log("userauth_hostbased: unsupported "
917c478bd9Sstevel@tonic-gate 		    "public key algorithm: %s", pkalg);
927c478bd9Sstevel@tonic-gate 		goto done;
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	key = key_from_blob(pkblob, blen);
957c478bd9Sstevel@tonic-gate 	if (key == NULL) {
967c478bd9Sstevel@tonic-gate 		error("userauth_hostbased: cannot decode key: %s", pkalg);
977c478bd9Sstevel@tonic-gate 		goto done;
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	if (key->type != pktype) {
1007c478bd9Sstevel@tonic-gate 		error("userauth_hostbased: type mismatch for decoded key "
1017c478bd9Sstevel@tonic-gate 		    "(received %d, expected %d)", key->type, pktype);
1027c478bd9Sstevel@tonic-gate 		goto done;
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 	service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1057c478bd9Sstevel@tonic-gate 	    authctxt->service;
1067c478bd9Sstevel@tonic-gate 	buffer_init(&b);
1077c478bd9Sstevel@tonic-gate 	buffer_put_string(&b, session_id2, session_id2_len);
1087c478bd9Sstevel@tonic-gate 	/* reconstruct packet */
1097c478bd9Sstevel@tonic-gate 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1107c478bd9Sstevel@tonic-gate 	buffer_put_cstring(&b, authctxt->user);
1117c478bd9Sstevel@tonic-gate 	buffer_put_cstring(&b, service);
1127c478bd9Sstevel@tonic-gate 	buffer_put_cstring(&b, "hostbased");
1137c478bd9Sstevel@tonic-gate 	buffer_put_string(&b, pkalg, alen);
1147c478bd9Sstevel@tonic-gate 	buffer_put_string(&b, pkblob, blen);
1157c478bd9Sstevel@tonic-gate 	buffer_put_cstring(&b, chost);
1167c478bd9Sstevel@tonic-gate 	buffer_put_cstring(&b, cuser);
1177c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
1187c478bd9Sstevel@tonic-gate 	buffer_dump(&b);
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate 	/* test for allowed key and correct signature */
1217c478bd9Sstevel@tonic-gate 	authenticated = 0;
1227c478bd9Sstevel@tonic-gate 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
1237c478bd9Sstevel@tonic-gate 	    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
1247c478bd9Sstevel@tonic-gate 			buffer_len(&b))) == 1)
1257c478bd9Sstevel@tonic-gate 		authenticated = 1;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	buffer_clear(&b);
1287c478bd9Sstevel@tonic-gate done:
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * XXX TODO: Add config options for specifying users for whom
1317c478bd9Sstevel@tonic-gate 	 *           this userauth is insufficient and what userauths
1327c478bd9Sstevel@tonic-gate 	 *           may continue.
1337c478bd9Sstevel@tonic-gate 	 *
1347c478bd9Sstevel@tonic-gate 	 *           NOTE: do_pam_non_initial_userauth() does this for
1357c478bd9Sstevel@tonic-gate 	 *                 users with expired passwords.
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate #ifdef USE_PAM
1387c478bd9Sstevel@tonic-gate 	if (authenticated) {
139*f44ef466Sjp161948 		authctxt->cuser = cuser;
1407c478bd9Sstevel@tonic-gate 		if (!do_pam_non_initial_userauth(authctxt))
1417c478bd9Sstevel@tonic-gate 			authenticated = 0;
142*f44ef466Sjp161948 		/* Make sure nobody else will use this pointer since we are
143*f44ef466Sjp161948 		 * going to free that string. */
144*f44ef466Sjp161948 		authctxt->cuser = NULL;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (authenticated)
1497c478bd9Sstevel@tonic-gate 		authctxt->method->authenticated = 1;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	debug2("userauth_hostbased: authenticated %d", authenticated);
1527c478bd9Sstevel@tonic-gate 	if (key != NULL)
1537c478bd9Sstevel@tonic-gate 		key_free(key);
1547c478bd9Sstevel@tonic-gate 	xfree(pkalg);
1557c478bd9Sstevel@tonic-gate 	xfree(pkblob);
1567c478bd9Sstevel@tonic-gate 	xfree(cuser);
1577c478bd9Sstevel@tonic-gate 	xfree(chost);
1587c478bd9Sstevel@tonic-gate 	xfree(sig);
1597c478bd9Sstevel@tonic-gate 	return;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /* return 1 if given hostkey is allowed */
1637c478bd9Sstevel@tonic-gate int
1647c478bd9Sstevel@tonic-gate hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1657c478bd9Sstevel@tonic-gate     Key *key)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	const char *resolvedname, *ipaddr, *lookup;
1687c478bd9Sstevel@tonic-gate 	HostStatus host_status;
1697c478bd9Sstevel@tonic-gate 	int len;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
1727c478bd9Sstevel@tonic-gate 	ipaddr = get_remote_ipaddr();
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
1757c478bd9Sstevel@tonic-gate 	    chost, resolvedname, ipaddr);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (pw == NULL)
1787c478bd9Sstevel@tonic-gate 		return 0;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (options.hostbased_uses_name_from_packet_only) {
1817c478bd9Sstevel@tonic-gate 		if (auth_rhosts2(pw, cuser, chost, chost) == 0)
1827c478bd9Sstevel@tonic-gate 			return 0;
1837c478bd9Sstevel@tonic-gate 		lookup = chost;
1847c478bd9Sstevel@tonic-gate 	} else {
1857c478bd9Sstevel@tonic-gate 		if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
1867c478bd9Sstevel@tonic-gate 			debug2("stripping trailing dot from chost %s", chost);
1877c478bd9Sstevel@tonic-gate 			chost[len - 1] = '\0';
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		if (strcasecmp(resolvedname, chost) != 0)
1907c478bd9Sstevel@tonic-gate 			log("userauth_hostbased mismatch: "
1917c478bd9Sstevel@tonic-gate 			    "client sends %s, but we resolve %s to %s",
1927c478bd9Sstevel@tonic-gate 			    chost, ipaddr, resolvedname);
1937c478bd9Sstevel@tonic-gate 		if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
1947c478bd9Sstevel@tonic-gate 			return 0;
1957c478bd9Sstevel@tonic-gate 		lookup = resolvedname;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	debug2("userauth_hostbased: access allowed by auth_rhosts2");
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	host_status = check_key_in_hostfiles(pw, key, lookup,
2007c478bd9Sstevel@tonic-gate 	    _PATH_SSH_SYSTEM_HOSTFILE,
2017c478bd9Sstevel@tonic-gate 	    options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* backward compat if no key has been found. */
2047c478bd9Sstevel@tonic-gate 	if (host_status == HOST_NEW)
2057c478bd9Sstevel@tonic-gate 		host_status = check_key_in_hostfiles(pw, key, lookup,
2067c478bd9Sstevel@tonic-gate 		    _PATH_SSH_SYSTEM_HOSTFILE2,
2077c478bd9Sstevel@tonic-gate 		    options.ignore_user_known_hosts ? NULL :
2087c478bd9Sstevel@tonic-gate 		    _PATH_SSH_USER_HOSTFILE2);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	return (host_status == HOST_OK);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate Authmethod method_hostbased = {
2147c478bd9Sstevel@tonic-gate 	"hostbased",
2157c478bd9Sstevel@tonic-gate 	&options.hostbased_authentication,
2167c478bd9Sstevel@tonic-gate 	userauth_hostbased,
2177c478bd9Sstevel@tonic-gate 	NULL,		    /* no abandon function */
2187c478bd9Sstevel@tonic-gate 	NULL, NULL,	    /* method data and hist data */
2197c478bd9Sstevel@tonic-gate 	0,		    /* is not initial userauth */
2207c478bd9Sstevel@tonic-gate 	0, 0, 0,	    /* counters */
2217c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0    /* state */
2227c478bd9Sstevel@tonic-gate };
223