auth2-hostbased.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* $OpenBSD: auth2-hostbased.c,v 1.42 2019/11/25 00:51:37 djm Exp $ */
  2. /*
  3. * Copyright (c) 2000 Markus Friedl. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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.
  13. *
  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.
  24. */
  25. #include "includes.h"
  26. #include <sys/types.h>
  27. #include <stdlib.h>
  28. #include <pwd.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include "xmalloc.h"
  32. #include "ssh2.h"
  33. #include "packet.h"
  34. #include "kex.h"
  35. #include "sshbuf.h"
  36. #include "log.h"
  37. #include "misc.h"
  38. #include "servconf.h"
  39. #include "compat.h"
  40. #include "sshkey.h"
  41. #include "hostfile.h"
  42. #include "auth.h"
  43. #include "canohost.h"
  44. #ifdef GSSAPI
  45. #include "ssh-gss.h"
  46. #endif
  47. #include "monitor_wrap.h"
  48. #include "pathnames.h"
  49. #include "ssherr.h"
  50. #include "match.h"
  51. /* import */
  52. extern ServerOptions options;
  53. static int
  54. userauth_hostbased(struct ssh *ssh)
  55. {
  56. Authctxt *authctxt = ssh->authctxt;
  57. struct sshbuf *b;
  58. struct sshkey *key = NULL;
  59. char *pkalg, *cuser, *chost;
  60. u_char *pkblob, *sig;
  61. size_t alen, blen, slen;
  62. int r, pktype, authenticated = 0;
  63. /* XXX use sshkey_froms() */
  64. if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
  65. (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
  66. (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
  67. (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
  68. (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
  69. fatal("%s: packet parsing: %s", __func__, ssh_err(r));
  70. debug("%s: cuser %s chost %s pkalg %s slen %zu", __func__,
  71. cuser, chost, pkalg, slen);
  72. #ifdef DEBUG_PK
  73. debug("signature:");
  74. sshbuf_dump_data(sig, slen, stderr);
  75. #endif
  76. pktype = sshkey_type_from_name(pkalg);
  77. if (pktype == KEY_UNSPEC) {
  78. /* this is perfectly legal */
  79. logit("%s: unsupported public key algorithm: %s",
  80. __func__, pkalg);
  81. goto done;
  82. }
  83. if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
  84. error("%s: key_from_blob: %s", __func__, ssh_err(r));
  85. goto done;
  86. }
  87. if (key == NULL) {
  88. error("%s: cannot decode key: %s", __func__, pkalg);
  89. goto done;
  90. }
  91. if (key->type != pktype) {
  92. error("%s: type mismatch for decoded key "
  93. "(received %d, expected %d)", __func__, key->type, pktype);
  94. goto done;
  95. }
  96. if (sshkey_type_plain(key->type) == KEY_RSA &&
  97. (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
  98. error("Refusing RSA key because peer uses unsafe "
  99. "signature format");
  100. goto done;
  101. }
  102. if (match_pattern_list(pkalg, options.hostbased_accepted_algos, 0) != 1) {
  103. logit("%s: key type %s not in HostbasedAcceptedAlgorithms",
  104. __func__, sshkey_type(key));
  105. goto done;
  106. }
  107. if ((r = sshkey_check_cert_sigtype(key,
  108. options.ca_sign_algorithms)) != 0) {
  109. logit("%s: certificate signature algorithm %s: %s", __func__,
  110. (key->cert == NULL || key->cert->signature_type == NULL) ?
  111. "(null)" : key->cert->signature_type, ssh_err(r));
  112. goto done;
  113. }
  114. if (!authctxt->valid || authctxt->user == NULL) {
  115. debug2("%s: disabled because of invalid user", __func__);
  116. goto done;
  117. }
  118. if ((b = sshbuf_new()) == NULL)
  119. fatal("%s: sshbuf_new failed", __func__);
  120. /* reconstruct packet */
  121. if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
  122. (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  123. #ifdef WITH_SELINUX
  124. (authctxt->role
  125. ? ( (r = sshbuf_put_u32(b, strlen(authctxt->user)+strlen(authctxt->role)+1)) != 0 ||
  126. (r = sshbuf_put(b, authctxt->user, strlen(authctxt->user))) != 0 ||
  127. (r = sshbuf_put_u8(b, '/') != 0) ||
  128. (r = sshbuf_put(b, authctxt->role, strlen(authctxt->role))) != 0)
  129. : (r = sshbuf_put_cstring(b, authctxt->user)) != 0) ||
  130. #else
  131. (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
  132. #endif
  133. (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
  134. (r = sshbuf_put_cstring(b, "hostbased")) != 0 ||
  135. (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
  136. (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
  137. (r = sshbuf_put_cstring(b, chost)) != 0 ||
  138. (r = sshbuf_put_cstring(b, cuser)) != 0)
  139. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  140. #ifdef DEBUG_PK
  141. sshbuf_dump(b, stderr);
  142. #endif
  143. auth2_record_info(authctxt,
  144. "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
  145. /* test for allowed key and correct signature */
  146. authenticated = 0;
  147. if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
  148. chost, key)) &&
  149. PRIVSEP(hostbased_key_verify(ssh, key, sig, slen,
  150. sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL)) == 0)
  151. authenticated = 1;
  152. auth2_record_key(authctxt, authenticated, key);
  153. sshbuf_free(b);
  154. done:
  155. debug2("%s: authenticated %d", __func__, authenticated);
  156. sshkey_free(key);
  157. free(pkalg);
  158. free(pkblob);
  159. free(cuser);
  160. free(chost);
  161. free(sig);
  162. return authenticated;
  163. }
  164. int
  165. hostbased_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig,
  166. size_t slen, const u_char *data, size_t datalen, const char *pkalg, u_int compat,
  167. struct sshkey_sig_details **detailsp)
  168. {
  169. int rv;
  170. rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat, detailsp);
  171. #ifdef SSH_AUDIT_EVENTS
  172. audit_key(ssh, 0, &rv, key);
  173. #endif
  174. return rv;
  175. }
  176. /* return 1 if given hostkey is allowed */
  177. int
  178. hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
  179. const char *cuser, char *chost, struct sshkey *key)
  180. {
  181. const char *resolvedname, *ipaddr, *lookup, *reason;
  182. HostStatus host_status;
  183. int len;
  184. char *fp;
  185. if (auth_key_is_revoked(key))
  186. return 0;
  187. resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
  188. ipaddr = ssh_remote_ipaddr(ssh);
  189. debug2("%s: chost %s resolvedname %s ipaddr %s", __func__,
  190. chost, resolvedname, ipaddr);
  191. if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
  192. debug2("stripping trailing dot from chost %s", chost);
  193. chost[len - 1] = '\0';
  194. }
  195. if (options.hostbased_uses_name_from_packet_only) {
  196. if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
  197. debug2("%s: auth_rhosts2 refused "
  198. "user \"%.100s\" host \"%.100s\" (from packet)",
  199. __func__, cuser, chost);
  200. return 0;
  201. }
  202. lookup = chost;
  203. } else {
  204. if (strcasecmp(resolvedname, chost) != 0)
  205. logit("userauth_hostbased mismatch: "
  206. "client sends %s, but we resolve %s to %s",
  207. chost, ipaddr, resolvedname);
  208. if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
  209. debug2("%s: auth_rhosts2 refused "
  210. "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
  211. __func__, cuser, resolvedname, ipaddr);
  212. return 0;
  213. }
  214. lookup = resolvedname;
  215. }
  216. debug2("%s: access allowed by auth_rhosts2", __func__);
  217. if (sshkey_is_cert(key) &&
  218. sshkey_cert_check_authority(key, 1, 0, 0, lookup, &reason)) {
  219. error("%s", reason);
  220. auth_debug_add("%s", reason);
  221. return 0;
  222. }
  223. host_status = check_key_in_hostfiles(pw, key, lookup,
  224. _PATH_SSH_SYSTEM_HOSTFILE,
  225. options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
  226. /* backward compat if no key has been found. */
  227. if (host_status == HOST_NEW) {
  228. host_status = check_key_in_hostfiles(pw, key, lookup,
  229. _PATH_SSH_SYSTEM_HOSTFILE2,
  230. options.ignore_user_known_hosts ? NULL :
  231. _PATH_SSH_USER_HOSTFILE2);
  232. }
  233. if (host_status == HOST_OK) {
  234. if (sshkey_is_cert(key)) {
  235. if ((fp = sshkey_fingerprint(key->cert->signature_key,
  236. options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
  237. fatal("%s: sshkey_fingerprint fail", __func__);
  238. verbose("Accepted certificate ID \"%s\" signed by "
  239. "%s CA %s from %s@%s", key->cert->key_id,
  240. sshkey_type(key->cert->signature_key), fp,
  241. cuser, lookup);
  242. } else {
  243. if ((fp = sshkey_fingerprint(key,
  244. options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
  245. fatal("%s: sshkey_fingerprint fail", __func__);
  246. verbose("Accepted %s public key %s from %s@%s",
  247. sshkey_type(key), fp, cuser, lookup);
  248. }
  249. free(fp);
  250. }
  251. return (host_status == HOST_OK);
  252. }
  253. Authmethod method_hostbased = {
  254. "hostbased",
  255. userauth_hostbased,
  256. &options.hostbased_authentication
  257. };