1*0fdf8faeSEd Maste /* $OpenBSD: auth2-hostbased.c,v 1.53 2024/05/17 00:30:23 djm Exp $ */
2545d5ecaSDag-Erling Smørgrav /*
3545d5ecaSDag-Erling Smørgrav * Copyright (c) 2000 Markus Friedl. All rights reserved.
4545d5ecaSDag-Erling Smørgrav *
5545d5ecaSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
6545d5ecaSDag-Erling Smørgrav * modification, are permitted provided that the following conditions
7545d5ecaSDag-Erling Smørgrav * are met:
8545d5ecaSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
9545d5ecaSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer.
10545d5ecaSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
11545d5ecaSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
12545d5ecaSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
13545d5ecaSDag-Erling Smørgrav *
14545d5ecaSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15545d5ecaSDag-Erling Smørgrav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16545d5ecaSDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17545d5ecaSDag-Erling Smørgrav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18545d5ecaSDag-Erling Smørgrav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19545d5ecaSDag-Erling Smørgrav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20545d5ecaSDag-Erling Smørgrav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21545d5ecaSDag-Erling Smørgrav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22545d5ecaSDag-Erling Smørgrav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23545d5ecaSDag-Erling Smørgrav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24545d5ecaSDag-Erling Smørgrav */
25545d5ecaSDag-Erling Smørgrav
26545d5ecaSDag-Erling Smørgrav #include "includes.h"
27545d5ecaSDag-Erling Smørgrav
28761efaa7SDag-Erling Smørgrav #include <sys/types.h>
29761efaa7SDag-Erling Smørgrav
3019261079SEd Maste #include <stdlib.h>
31761efaa7SDag-Erling Smørgrav #include <pwd.h>
32761efaa7SDag-Erling Smørgrav #include <string.h>
33761efaa7SDag-Erling Smørgrav #include <stdarg.h>
34761efaa7SDag-Erling Smørgrav
35545d5ecaSDag-Erling Smørgrav #include "xmalloc.h"
36761efaa7SDag-Erling Smørgrav #include "ssh2.h"
37545d5ecaSDag-Erling Smørgrav #include "packet.h"
3819261079SEd Maste #include "kex.h"
39190cef3dSDag-Erling Smørgrav #include "sshbuf.h"
40545d5ecaSDag-Erling Smørgrav #include "log.h"
41a0ee8cc6SDag-Erling Smørgrav #include "misc.h"
42545d5ecaSDag-Erling Smørgrav #include "servconf.h"
434f52dfbbSDag-Erling Smørgrav #include "sshkey.h"
44761efaa7SDag-Erling Smørgrav #include "hostfile.h"
45761efaa7SDag-Erling Smørgrav #include "auth.h"
46545d5ecaSDag-Erling Smørgrav #include "canohost.h"
47761efaa7SDag-Erling Smørgrav #ifdef GSSAPI
48761efaa7SDag-Erling Smørgrav #include "ssh-gss.h"
49761efaa7SDag-Erling Smørgrav #endif
50545d5ecaSDag-Erling Smørgrav #include "monitor_wrap.h"
51545d5ecaSDag-Erling Smørgrav #include "pathnames.h"
524f52dfbbSDag-Erling Smørgrav #include "ssherr.h"
53bc5531deSDag-Erling Smørgrav #include "match.h"
54545d5ecaSDag-Erling Smørgrav
55545d5ecaSDag-Erling Smørgrav /* import */
56545d5ecaSDag-Erling Smørgrav extern ServerOptions options;
57*0fdf8faeSEd Maste extern struct authmethod_cfg methodcfg_hostbased;
58545d5ecaSDag-Erling Smørgrav
59545d5ecaSDag-Erling Smørgrav static int
userauth_hostbased(struct ssh * ssh,const char * method)601323ec57SEd Maste userauth_hostbased(struct ssh *ssh, const char *method)
61545d5ecaSDag-Erling Smørgrav {
624f52dfbbSDag-Erling Smørgrav Authctxt *authctxt = ssh->authctxt;
634f52dfbbSDag-Erling Smørgrav struct sshbuf *b;
644f52dfbbSDag-Erling Smørgrav struct sshkey *key = NULL;
6547dd1d1bSDag-Erling Smørgrav char *pkalg, *cuser, *chost;
66545d5ecaSDag-Erling Smørgrav u_char *pkblob, *sig;
674f52dfbbSDag-Erling Smørgrav size_t alen, blen, slen;
684f52dfbbSDag-Erling Smørgrav int r, pktype, authenticated = 0;
69545d5ecaSDag-Erling Smørgrav
704f52dfbbSDag-Erling Smørgrav /* XXX use sshkey_froms() */
714f52dfbbSDag-Erling Smørgrav if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
724f52dfbbSDag-Erling Smørgrav (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
734f52dfbbSDag-Erling Smørgrav (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
744f52dfbbSDag-Erling Smørgrav (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
754f52dfbbSDag-Erling Smørgrav (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
7619261079SEd Maste fatal_fr(r, "parse packet");
77545d5ecaSDag-Erling Smørgrav
7819261079SEd Maste debug_f("cuser %s chost %s pkalg %s slen %zu",
79545d5ecaSDag-Erling Smørgrav cuser, chost, pkalg, slen);
80545d5ecaSDag-Erling Smørgrav #ifdef DEBUG_PK
81545d5ecaSDag-Erling Smørgrav debug("signature:");
822f513db7SEd Maste sshbuf_dump_data(sig, slen, stderr);
83545d5ecaSDag-Erling Smørgrav #endif
844f52dfbbSDag-Erling Smørgrav pktype = sshkey_type_from_name(pkalg);
85545d5ecaSDag-Erling Smørgrav if (pktype == KEY_UNSPEC) {
86545d5ecaSDag-Erling Smørgrav /* this is perfectly legal */
8719261079SEd Maste logit_f("unsupported public key algorithm: %s",
8819261079SEd Maste pkalg);
89545d5ecaSDag-Erling Smørgrav goto done;
90545d5ecaSDag-Erling Smørgrav }
914f52dfbbSDag-Erling Smørgrav if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
9219261079SEd Maste error_fr(r, "key_from_blob");
934f52dfbbSDag-Erling Smørgrav goto done;
944f52dfbbSDag-Erling Smørgrav }
95545d5ecaSDag-Erling Smørgrav if (key == NULL) {
9619261079SEd Maste error_f("cannot decode key: %s", pkalg);
97545d5ecaSDag-Erling Smørgrav goto done;
98545d5ecaSDag-Erling Smørgrav }
99545d5ecaSDag-Erling Smørgrav if (key->type != pktype) {
10019261079SEd Maste error_f("type mismatch for decoded key "
10119261079SEd Maste "(received %d, expected %d)", key->type, pktype);
102545d5ecaSDag-Erling Smørgrav goto done;
103545d5ecaSDag-Erling Smørgrav }
10419261079SEd Maste if (match_pattern_list(pkalg, options.hostbased_accepted_algos, 0) != 1) {
1051323ec57SEd Maste logit_f("signature algorithm %s not in "
1061323ec57SEd Maste "HostbasedAcceptedAlgorithms", pkalg);
107bc5531deSDag-Erling Smørgrav goto done;
108bc5531deSDag-Erling Smørgrav }
1092f513db7SEd Maste if ((r = sshkey_check_cert_sigtype(key,
1102f513db7SEd Maste options.ca_sign_algorithms)) != 0) {
11119261079SEd Maste logit_fr(r, "certificate signature algorithm %s",
1122f513db7SEd Maste (key->cert == NULL || key->cert->signature_type == NULL) ?
11319261079SEd Maste "(null)" : key->cert->signature_type);
1142f513db7SEd Maste goto done;
1152f513db7SEd Maste }
11638a52bd3SEd Maste if ((r = sshkey_check_rsa_length(key,
11738a52bd3SEd Maste options.required_rsa_size)) != 0) {
11838a52bd3SEd Maste logit_r(r, "refusing %s key", sshkey_type(key));
11938a52bd3SEd Maste goto done;
12038a52bd3SEd Maste }
121bc5531deSDag-Erling Smørgrav
122190cef3dSDag-Erling Smørgrav if (!authctxt->valid || authctxt->user == NULL) {
12319261079SEd Maste debug2_f("disabled because of invalid user");
124190cef3dSDag-Erling Smørgrav goto done;
125190cef3dSDag-Erling Smørgrav }
126190cef3dSDag-Erling Smørgrav
1274f52dfbbSDag-Erling Smørgrav if ((b = sshbuf_new()) == NULL)
12819261079SEd Maste fatal_f("sshbuf_new failed");
129545d5ecaSDag-Erling Smørgrav /* reconstruct packet */
13019261079SEd Maste if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
1314f52dfbbSDag-Erling Smørgrav (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1324f52dfbbSDag-Erling Smørgrav (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
13347dd1d1bSDag-Erling Smørgrav (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1341323ec57SEd Maste (r = sshbuf_put_cstring(b, method)) != 0 ||
1354f52dfbbSDag-Erling Smørgrav (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
1364f52dfbbSDag-Erling Smørgrav (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
1374f52dfbbSDag-Erling Smørgrav (r = sshbuf_put_cstring(b, chost)) != 0 ||
1384f52dfbbSDag-Erling Smørgrav (r = sshbuf_put_cstring(b, cuser)) != 0)
13919261079SEd Maste fatal_fr(r, "reconstruct packet");
140545d5ecaSDag-Erling Smørgrav #ifdef DEBUG_PK
1414f52dfbbSDag-Erling Smørgrav sshbuf_dump(b, stderr);
142545d5ecaSDag-Erling Smørgrav #endif
143e4a9863fSDag-Erling Smørgrav
1444f52dfbbSDag-Erling Smørgrav auth2_record_info(authctxt,
145e4a9863fSDag-Erling Smørgrav "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
146e4a9863fSDag-Erling Smørgrav
147545d5ecaSDag-Erling Smørgrav /* test for allowed key and correct signature */
148545d5ecaSDag-Erling Smørgrav authenticated = 0;
149*0fdf8faeSEd Maste if (mm_hostbased_key_allowed(ssh, authctxt->pw, cuser,
150*0fdf8faeSEd Maste chost, key) &&
151*0fdf8faeSEd Maste mm_sshkey_verify(key, sig, slen,
152*0fdf8faeSEd Maste sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL) == 0)
153545d5ecaSDag-Erling Smørgrav authenticated = 1;
154545d5ecaSDag-Erling Smørgrav
1554f52dfbbSDag-Erling Smørgrav auth2_record_key(authctxt, authenticated, key);
1564f52dfbbSDag-Erling Smørgrav sshbuf_free(b);
157545d5ecaSDag-Erling Smørgrav done:
15819261079SEd Maste debug2_f("authenticated %d", authenticated);
1594f52dfbbSDag-Erling Smørgrav sshkey_free(key);
160e4a9863fSDag-Erling Smørgrav free(pkalg);
161e4a9863fSDag-Erling Smørgrav free(pkblob);
162e4a9863fSDag-Erling Smørgrav free(cuser);
163e4a9863fSDag-Erling Smørgrav free(chost);
164e4a9863fSDag-Erling Smørgrav free(sig);
165545d5ecaSDag-Erling Smørgrav return authenticated;
166545d5ecaSDag-Erling Smørgrav }
167545d5ecaSDag-Erling Smørgrav
168545d5ecaSDag-Erling Smørgrav /* return 1 if given hostkey is allowed */
169545d5ecaSDag-Erling Smørgrav int
hostbased_key_allowed(struct ssh * ssh,struct passwd * pw,const char * cuser,char * chost,struct sshkey * key)17019261079SEd Maste hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
17119261079SEd Maste const char *cuser, char *chost, struct sshkey *key)
172545d5ecaSDag-Erling Smørgrav {
173e2f6069cSDag-Erling Smørgrav const char *resolvedname, *ipaddr, *lookup, *reason;
174545d5ecaSDag-Erling Smørgrav HostStatus host_status;
175545d5ecaSDag-Erling Smørgrav int len;
176e2f6069cSDag-Erling Smørgrav char *fp;
177545d5ecaSDag-Erling Smørgrav
178b15c8340SDag-Erling Smørgrav if (auth_key_is_revoked(key))
179b15c8340SDag-Erling Smørgrav return 0;
180b15c8340SDag-Erling Smørgrav
181076ad2f8SDag-Erling Smørgrav resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
182076ad2f8SDag-Erling Smørgrav ipaddr = ssh_remote_ipaddr(ssh);
183545d5ecaSDag-Erling Smørgrav
18419261079SEd Maste debug2_f("chost %s resolvedname %s ipaddr %s",
185545d5ecaSDag-Erling Smørgrav chost, resolvedname, ipaddr);
186545d5ecaSDag-Erling Smørgrav
187d4af9e69SDag-Erling Smørgrav if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
188d4af9e69SDag-Erling Smørgrav debug2("stripping trailing dot from chost %s", chost);
189d4af9e69SDag-Erling Smørgrav chost[len - 1] = '\0';
190d4af9e69SDag-Erling Smørgrav }
191d4af9e69SDag-Erling Smørgrav
192545d5ecaSDag-Erling Smørgrav if (options.hostbased_uses_name_from_packet_only) {
193bc5531deSDag-Erling Smørgrav if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
19419261079SEd Maste debug2_f("auth_rhosts2 refused user \"%.100s\" "
19519261079SEd Maste "host \"%.100s\" (from packet)", cuser, chost);
196545d5ecaSDag-Erling Smørgrav return 0;
197bc5531deSDag-Erling Smørgrav }
198545d5ecaSDag-Erling Smørgrav lookup = chost;
199545d5ecaSDag-Erling Smørgrav } else {
200545d5ecaSDag-Erling Smørgrav if (strcasecmp(resolvedname, chost) != 0)
201d95e11bfSDag-Erling Smørgrav logit("userauth_hostbased mismatch: "
202545d5ecaSDag-Erling Smørgrav "client sends %s, but we resolve %s to %s",
203545d5ecaSDag-Erling Smørgrav chost, ipaddr, resolvedname);
204bc5531deSDag-Erling Smørgrav if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
20519261079SEd Maste debug2_f("auth_rhosts2 refused "
206bc5531deSDag-Erling Smørgrav "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
20719261079SEd Maste cuser, resolvedname, ipaddr);
208545d5ecaSDag-Erling Smørgrav return 0;
209bc5531deSDag-Erling Smørgrav }
210545d5ecaSDag-Erling Smørgrav lookup = resolvedname;
211545d5ecaSDag-Erling Smørgrav }
21219261079SEd Maste debug2_f("access allowed by auth_rhosts2");
213545d5ecaSDag-Erling Smørgrav
2144f52dfbbSDag-Erling Smørgrav if (sshkey_is_cert(key) &&
21519261079SEd Maste sshkey_cert_check_authority_now(key, 1, 0, 0, lookup, &reason)) {
216e2f6069cSDag-Erling Smørgrav error("%s", reason);
217e2f6069cSDag-Erling Smørgrav auth_debug_add("%s", reason);
218e2f6069cSDag-Erling Smørgrav return 0;
219e2f6069cSDag-Erling Smørgrav }
220e2f6069cSDag-Erling Smørgrav
221545d5ecaSDag-Erling Smørgrav host_status = check_key_in_hostfiles(pw, key, lookup,
222545d5ecaSDag-Erling Smørgrav _PATH_SSH_SYSTEM_HOSTFILE,
223545d5ecaSDag-Erling Smørgrav options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
224545d5ecaSDag-Erling Smørgrav
225545d5ecaSDag-Erling Smørgrav /* backward compat if no key has been found. */
226e2f6069cSDag-Erling Smørgrav if (host_status == HOST_NEW) {
227545d5ecaSDag-Erling Smørgrav host_status = check_key_in_hostfiles(pw, key, lookup,
228545d5ecaSDag-Erling Smørgrav _PATH_SSH_SYSTEM_HOSTFILE2,
229545d5ecaSDag-Erling Smørgrav options.ignore_user_known_hosts ? NULL :
230545d5ecaSDag-Erling Smørgrav _PATH_SSH_USER_HOSTFILE2);
231e2f6069cSDag-Erling Smørgrav }
232e2f6069cSDag-Erling Smørgrav
233e2f6069cSDag-Erling Smørgrav if (host_status == HOST_OK) {
2344f52dfbbSDag-Erling Smørgrav if (sshkey_is_cert(key)) {
235bc5531deSDag-Erling Smørgrav if ((fp = sshkey_fingerprint(key->cert->signature_key,
236bc5531deSDag-Erling Smørgrav options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
23719261079SEd Maste fatal_f("sshkey_fingerprint fail");
238e2f6069cSDag-Erling Smørgrav verbose("Accepted certificate ID \"%s\" signed by "
239e2f6069cSDag-Erling Smørgrav "%s CA %s from %s@%s", key->cert->key_id,
2404f52dfbbSDag-Erling Smørgrav sshkey_type(key->cert->signature_key), fp,
241e2f6069cSDag-Erling Smørgrav cuser, lookup);
242e2f6069cSDag-Erling Smørgrav } else {
243bc5531deSDag-Erling Smørgrav if ((fp = sshkey_fingerprint(key,
244bc5531deSDag-Erling Smørgrav options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
24519261079SEd Maste fatal_f("sshkey_fingerprint fail");
246e2f6069cSDag-Erling Smørgrav verbose("Accepted %s public key %s from %s@%s",
2474f52dfbbSDag-Erling Smørgrav sshkey_type(key), fp, cuser, lookup);
248e2f6069cSDag-Erling Smørgrav }
249e4a9863fSDag-Erling Smørgrav free(fp);
250e2f6069cSDag-Erling Smørgrav }
251545d5ecaSDag-Erling Smørgrav
252545d5ecaSDag-Erling Smørgrav return (host_status == HOST_OK);
253545d5ecaSDag-Erling Smørgrav }
254545d5ecaSDag-Erling Smørgrav
255545d5ecaSDag-Erling Smørgrav Authmethod method_hostbased = {
256*0fdf8faeSEd Maste &methodcfg_hostbased,
257545d5ecaSDag-Erling Smørgrav userauth_hostbased,
258545d5ecaSDag-Erling Smørgrav };
259