2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "monitor_wrap.h"
39 #include "pathnames.h"
42 extern ServerOptions options;
43 extern u_char *session_id2;
44 extern u_int session_id2_len;
47 userauth_hostbased(Authctxt *authctxt)
51 char *pkalg, *cuser, *chost, *service;
53 u_int alen, blen, slen;
55 int authenticated = 0;
57 if (!authctxt->valid) {
58 debug2("userauth_hostbased: disabled because of invalid user");
61 pkalg = packet_get_string(&alen);
62 pkblob = packet_get_string(&blen);
63 chost = packet_get_string(NULL);
64 cuser = packet_get_string(NULL);
65 sig = packet_get_string(&slen);
67 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
68 cuser, chost, pkalg, slen);
72 buffer_append(&b, sig, slen);
76 pktype = key_type_from_name(pkalg);
77 if (pktype == KEY_UNSPEC) {
78 /* this is perfectly legal */
79 logit("userauth_hostbased: unsupported "
80 "public key algorithm: %s", pkalg);
83 key = key_from_blob(pkblob, blen);
85 error("userauth_hostbased: cannot decode key: %s", pkalg);
88 if (key->type != pktype) {
89 error("userauth_hostbased: type mismatch for decoded key "
90 "(received %d, expected %d)", key->type, pktype);
93 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
96 buffer_put_string(&b, session_id2, session_id2_len);
97 /* reconstruct packet */
98 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
99 buffer_put_cstring(&b, authctxt->user);
100 buffer_put_cstring(&b, service);
101 buffer_put_cstring(&b, "hostbased");
102 buffer_put_string(&b, pkalg, alen);
103 buffer_put_string(&b, pkblob, blen);
104 buffer_put_cstring(&b, chost);
105 buffer_put_cstring(&b, cuser);
109 /* test for allowed key and correct signature */
111 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
112 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
113 buffer_len(&b))) == 1)
118 debug2("userauth_hostbased: authenticated %d", authenticated);
126 return authenticated;
129 /* return 1 if given hostkey is allowed */
131 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
134 const char *resolvedname, *ipaddr, *lookup;
135 HostStatus host_status;
138 resolvedname = get_canonical_hostname(options.use_dns);
139 ipaddr = get_remote_ipaddr();
141 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
142 chost, resolvedname, ipaddr);
144 if (options.hostbased_uses_name_from_packet_only) {
145 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
149 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
150 debug2("stripping trailing dot from chost %s", chost);
151 chost[len - 1] = '\0';
153 if (strcasecmp(resolvedname, chost) != 0)
154 logit("userauth_hostbased mismatch: "
155 "client sends %s, but we resolve %s to %s",
156 chost, ipaddr, resolvedname);
157 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
159 lookup = resolvedname;
161 debug2("userauth_hostbased: access allowed by auth_rhosts2");
163 host_status = check_key_in_hostfiles(pw, key, lookup,
164 _PATH_SSH_SYSTEM_HOSTFILE,
165 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
167 /* backward compat if no key has been found. */
168 if (host_status == HOST_NEW)
169 host_status = check_key_in_hostfiles(pw, key, lookup,
170 _PATH_SSH_SYSTEM_HOSTFILE2,
171 options.ignore_user_known_hosts ? NULL :
172 _PATH_SSH_USER_HOSTFILE2);
174 return (host_status == HOST_OK);
177 Authmethod method_hostbased = {
180 &options.hostbased_authentication