auth-krb5.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* $OpenBSD: auth-krb5.c,v 1.23 2018/07/09 21:35:50 markus Exp $ */
  2. /*
  3. * Kerberos v5 authentication and ticket-passing routines.
  4. *
  5. * From: FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar
  6. */
  7. /*
  8. * Copyright (c) 2002 Daniel Kouril. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "includes.h"
  31. #include <sys/types.h>
  32. #include <pwd.h>
  33. #include <stdarg.h>
  34. #include "xmalloc.h"
  35. #include "ssh.h"
  36. #include "packet.h"
  37. #include "log.h"
  38. #include "sshbuf.h"
  39. #include "sshkey.h"
  40. #include "misc.h"
  41. #include "servconf.h"
  42. #include "uidswap.h"
  43. #include "hostfile.h"
  44. #include "auth.h"
  45. #ifdef KRB5
  46. #include <errno.h>
  47. #include <unistd.h>
  48. #include <string.h>
  49. #include <krb5.h>
  50. extern ServerOptions options;
  51. static int
  52. krb5_init(void *context)
  53. {
  54. Authctxt *authctxt = (Authctxt *)context;
  55. krb5_error_code problem;
  56. if (authctxt->krb5_ctx == NULL) {
  57. problem = krb5_init_context(&authctxt->krb5_ctx);
  58. if (problem)
  59. return (problem);
  60. }
  61. return (0);
  62. }
  63. int
  64. auth_krb5_password(Authctxt *authctxt, const char *password)
  65. {
  66. #ifndef HEIMDAL
  67. krb5_creds creds;
  68. krb5_principal server;
  69. #endif
  70. krb5_error_code problem;
  71. krb5_ccache ccache = NULL;
  72. int len;
  73. char *client, *platform_client;
  74. const char *errmsg;
  75. /* get platform-specific kerberos client principal name (if it exists) */
  76. platform_client = platform_krb5_get_principal_name(authctxt->pw->pw_name);
  77. client = platform_client ? platform_client : authctxt->pw->pw_name;
  78. temporarily_use_uid(authctxt->pw);
  79. problem = krb5_init(authctxt);
  80. if (problem)
  81. goto out;
  82. problem = krb5_parse_name(authctxt->krb5_ctx, client,
  83. &authctxt->krb5_user);
  84. if (problem)
  85. goto out;
  86. #ifdef HEIMDAL
  87. # ifdef HAVE_KRB5_CC_NEW_UNIQUE
  88. problem = krb5_cc_new_unique(authctxt->krb5_ctx,
  89. krb5_mcc_ops.prefix, NULL, &ccache);
  90. # else
  91. problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops, &ccache);
  92. # endif
  93. if (problem)
  94. goto out;
  95. problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
  96. authctxt->krb5_user);
  97. if (problem)
  98. goto out;
  99. restore_uid();
  100. problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
  101. ccache, password, 1, NULL);
  102. temporarily_use_uid(authctxt->pw);
  103. if (problem)
  104. goto out;
  105. # ifdef HAVE_KRB5_CC_NEW_UNIQUE
  106. problem = krb5_cc_new_unique(authctxt->krb5_ctx,
  107. krb5_fcc_ops.prefix, NULL, &authctxt->krb5_fwd_ccache);
  108. # else
  109. problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops,
  110. &authctxt->krb5_fwd_ccache);
  111. # endif
  112. if (problem)
  113. goto out;
  114. problem = krb5_cc_copy_cache(authctxt->krb5_ctx, ccache,
  115. authctxt->krb5_fwd_ccache);
  116. krb5_cc_destroy(authctxt->krb5_ctx, ccache);
  117. ccache = NULL;
  118. if (problem)
  119. goto out;
  120. #else
  121. problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
  122. authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
  123. if (problem)
  124. goto out;
  125. problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
  126. KRB5_NT_SRV_HST, &server);
  127. if (problem)
  128. goto out;
  129. restore_uid();
  130. problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
  131. NULL, NULL, NULL);
  132. krb5_free_principal(authctxt->krb5_ctx, server);
  133. temporarily_use_uid(authctxt->pw);
  134. if (problem)
  135. goto out;
  136. if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
  137. authctxt->pw->pw_name)) {
  138. problem = -1;
  139. goto out;
  140. }
  141. problem = ssh_krb5_cc_gen(authctxt->krb5_ctx, &authctxt->krb5_fwd_ccache);
  142. if (problem)
  143. goto out;
  144. problem = krb5_cc_initialize(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
  145. authctxt->krb5_user);
  146. if (problem)
  147. goto out;
  148. problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
  149. &creds);
  150. if (problem)
  151. goto out;
  152. #endif
  153. authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
  154. len = strlen(authctxt->krb5_ticket_file) + 6;
  155. authctxt->krb5_ccname = xmalloc(len);
  156. snprintf(authctxt->krb5_ccname, len, "FILE:%s",
  157. authctxt->krb5_ticket_file);
  158. #ifdef USE_PAM
  159. if (options.use_pam)
  160. do_pam_putenv("KRB5CCNAME", authctxt->krb5_ccname);
  161. #endif
  162. out:
  163. restore_uid();
  164. free(platform_client);
  165. if (problem) {
  166. if (ccache)
  167. krb5_cc_destroy(authctxt->krb5_ctx, ccache);
  168. if (authctxt->krb5_ctx != NULL && problem!=-1) {
  169. errmsg = krb5_get_error_message(authctxt->krb5_ctx,
  170. problem);
  171. debug("Kerberos password authentication failed: %s",
  172. errmsg);
  173. krb5_free_error_message(authctxt->krb5_ctx, errmsg);
  174. } else
  175. debug("Kerberos password authentication failed: %d",
  176. problem);
  177. krb5_cleanup_proc(authctxt);
  178. if (options.kerberos_or_local_passwd)
  179. return (-1);
  180. else
  181. return (0);
  182. }
  183. return (authctxt->valid ? 1 : 0);
  184. }
  185. void
  186. krb5_cleanup_proc(Authctxt *authctxt)
  187. {
  188. debug("krb5_cleanup_proc called");
  189. if (authctxt->krb5_fwd_ccache) {
  190. krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
  191. authctxt->krb5_fwd_ccache = NULL;
  192. }
  193. if (authctxt->krb5_user) {
  194. krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
  195. authctxt->krb5_user = NULL;
  196. }
  197. if (authctxt->krb5_ctx) {
  198. krb5_free_context(authctxt->krb5_ctx);
  199. authctxt->krb5_ctx = NULL;
  200. }
  201. }
  202. #ifndef HEIMDAL
  203. krb5_error_code
  204. ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
  205. int tmpfd, ret, oerrno;
  206. char ccname[40];
  207. mode_t old_umask;
  208. ret = snprintf(ccname, sizeof(ccname),
  209. "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
  210. if (ret < 0 || (size_t)ret >= sizeof(ccname))
  211. return ENOMEM;
  212. old_umask = umask(0177);
  213. tmpfd = mkstemp(ccname + strlen("FILE:"));
  214. oerrno = errno;
  215. umask(old_umask);
  216. if (tmpfd == -1) {
  217. logit("mkstemp(): %.100s", strerror(oerrno));
  218. return oerrno;
  219. }
  220. if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
  221. oerrno = errno;
  222. logit("fchmod(): %.100s", strerror(oerrno));
  223. close(tmpfd);
  224. return oerrno;
  225. }
  226. close(tmpfd);
  227. return (krb5_cc_resolve(ctx, ccname, ccache));
  228. }
  229. #endif /* !HEIMDAL */
  230. #endif /* KRB5 */