1 /*	$NetBSD: auth2-hostbased.c,v 1.3 2010/11/21 18:29:48 adam Exp $	*/
2 /* $OpenBSD: auth2-hostbased.c,v 1.14 2010/08/04 05:42:47 djm Exp $ */
3 /*
4  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "includes.h"
28 __RCSID("$NetBSD: auth2-hostbased.c,v 1.3 2010/11/21 18:29:48 adam Exp $");
29 #include <sys/types.h>
30 
31 #include <pwd.h>
32 #include <string.h>
33 #include <stdarg.h>
34 
35 #include "xmalloc.h"
36 #include "ssh2.h"
37 #include "packet.h"
38 #include "buffer.h"
39 #include "log.h"
40 #include "servconf.h"
41 #include "compat.h"
42 #include "key.h"
43 #include "hostfile.h"
44 #include "auth.h"
45 #include "canohost.h"
46 #ifdef GSSAPI
47 #include "ssh-gss.h"
48 #endif
49 #include "monitor_wrap.h"
50 #include "pathnames.h"
51 
52 /* import */
53 extern ServerOptions options;
54 extern u_char *session_id2;
55 extern u_int session_id2_len;
56 
57 static int
58 userauth_hostbased(Authctxt *authctxt)
59 {
60 	Buffer b;
61 	Key *key = NULL;
62 	char *pkalg, *cuser, *chost, *service;
63 	u_char *pkblob, *sig;
64 	u_int alen, blen, slen;
65 	int pktype;
66 	int authenticated = 0;
67 
68 	if (!authctxt->valid) {
69 		debug2("userauth_hostbased: disabled because of invalid user");
70 		return 0;
71 	}
72 	pkalg = packet_get_string(&alen);
73 	pkblob = packet_get_string(&blen);
74 	chost = packet_get_string(NULL);
75 	cuser = packet_get_string(NULL);
76 	sig = packet_get_string(&slen);
77 
78 	debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
79 	    cuser, chost, pkalg, slen);
80 #ifdef DEBUG_PK
81 	debug("signature:");
82 	buffer_init(&b);
83 	buffer_append(&b, sig, slen);
84 	buffer_dump(&b);
85 	buffer_free(&b);
86 #endif
87 	pktype = key_type_from_name(pkalg);
88 	if (pktype == KEY_UNSPEC) {
89 		/* this is perfectly legal */
90 		logit("userauth_hostbased: unsupported "
91 		    "public key algorithm: %s", pkalg);
92 		goto done;
93 	}
94 	key = key_from_blob(pkblob, blen);
95 	if (key == NULL) {
96 		error("userauth_hostbased: cannot decode key: %s", pkalg);
97 		goto done;
98 	}
99 	if (key->type != pktype) {
100 		error("userauth_hostbased: type mismatch for decoded key "
101 		    "(received %d, expected %d)", key->type, pktype);
102 		goto done;
103 	}
104 	service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
105 	    authctxt->service;
106 	buffer_init(&b);
107 	buffer_put_string(&b, session_id2, session_id2_len);
108 	/* reconstruct packet */
109 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
110 	buffer_put_cstring(&b, authctxt->user);
111 	buffer_put_cstring(&b, service);
112 	buffer_put_cstring(&b, "hostbased");
113 	buffer_put_string(&b, pkalg, alen);
114 	buffer_put_string(&b, pkblob, blen);
115 	buffer_put_cstring(&b, chost);
116 	buffer_put_cstring(&b, cuser);
117 #ifdef DEBUG_PK
118 	buffer_dump(&b);
119 #endif
120 	/* test for allowed key and correct signature */
121 	authenticated = 0;
122 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
123 	    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
124 			buffer_len(&b))) == 1)
125 		authenticated = 1;
126 
127 	buffer_free(&b);
128 done:
129 	debug2("userauth_hostbased: authenticated %d", authenticated);
130 	if (key != NULL)
131 		key_free(key);
132 	xfree(pkalg);
133 	xfree(pkblob);
134 	xfree(cuser);
135 	xfree(chost);
136 	xfree(sig);
137 	return authenticated;
138 }
139 
140 /* return 1 if given hostkey is allowed */
141 int
142 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
143     Key *key)
144 {
145 	const char *resolvedname, *ipaddr, *lookup, *reason;
146 	HostStatus host_status;
147 	int len;
148 	char *fp;
149 
150 	if (auth_key_is_revoked(key))
151 		return 0;
152 
153 	resolvedname = get_canonical_hostname(options.use_dns);
154 	ipaddr = get_remote_ipaddr();
155 
156 	debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
157 	    chost, resolvedname, ipaddr);
158 
159 	if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
160 		debug2("stripping trailing dot from chost %s", chost);
161 		chost[len - 1] = '\0';
162 	}
163 
164 	if (options.hostbased_uses_name_from_packet_only) {
165 		if (auth_rhosts2(pw, cuser, chost, chost) == 0)
166 			return 0;
167 		lookup = chost;
168 	} else {
169 		if (strcasecmp(resolvedname, chost) != 0)
170 			logit("userauth_hostbased mismatch: "
171 			    "client sends %s, but we resolve %s to %s",
172 			    chost, ipaddr, resolvedname);
173 		if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
174 			return 0;
175 		lookup = resolvedname;
176 	}
177 	debug2("userauth_hostbased: access allowed by auth_rhosts2");
178 
179 	if (key_is_cert(key) &&
180 	    key_cert_check_authority(key, 1, 0, lookup, &reason)) {
181 		error("%s", reason);
182 		auth_debug_add("%s", reason);
183 		return 0;
184 	}
185 
186 	host_status = check_key_in_hostfiles(pw, key, lookup,
187 	    _PATH_SSH_SYSTEM_HOSTFILE,
188 	    options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
189 
190 	/* backward compat if no key has been found. */
191 	if (host_status == HOST_NEW) {
192 		host_status = check_key_in_hostfiles(pw, key, lookup,
193 		    _PATH_SSH_SYSTEM_HOSTFILE2,
194 		    options.ignore_user_known_hosts ? NULL :
195 		    _PATH_SSH_USER_HOSTFILE2);
196 	}
197 
198 	if (host_status == HOST_OK) {
199 		if (key_is_cert(key)) {
200 			fp = key_fingerprint(key->cert->signature_key,
201 			    SSH_FP_MD5, SSH_FP_HEX);
202 			verbose("Accepted certificate ID \"%s\" signed by "
203 			    "%s CA %s from %s@%s", key->cert->key_id,
204 			    key_type(key->cert->signature_key), fp,
205 			    cuser, lookup);
206 		} else {
207 			fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
208 			verbose("Accepted %s public key %s from %s@%s",
209 			    key_type(key), fp, cuser, lookup);
210 		}
211 		xfree(fp);
212 	}
213 
214 	return (host_status == HOST_OK);
215 }
216 
217 Authmethod method_hostbased = {
218 	"hostbased",
219 	userauth_hostbased,
220 	&options.hostbased_authentication
221 };
222