gss-serv-krb5.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* $OpenBSD: gss-serv-krb5.c,v 1.9 2018/07/09 21:37:55 markus Exp $ */
  2. /*
  3. * Copyright (c) 2001-2003 Simon Wilkinson. 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. #ifdef GSSAPI
  27. #ifdef KRB5
  28. #include <sys/types.h>
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include "xmalloc.h"
  34. #include "sshkey.h"
  35. #include "hostfile.h"
  36. #include "auth.h"
  37. #include "log.h"
  38. #include "misc.h"
  39. #include "servconf.h"
  40. #include "ssh-gss.h"
  41. extern Authctxt *the_authctxt;
  42. extern ServerOptions options;
  43. #ifdef HEIMDAL
  44. # include <krb5.h>
  45. #endif
  46. #ifdef HAVE_GSSAPI_KRB5_H
  47. # include <gssapi_krb5.h>
  48. #elif HAVE_GSSAPI_GSSAPI_KRB5_H
  49. # include <gssapi/gssapi_krb5.h>
  50. #endif
  51. /* all commands are allowed by default */
  52. char **k5users_allowed_cmds = NULL;
  53. static int ssh_gssapi_k5login_exists();
  54. static int ssh_gssapi_krb5_cmdok(krb5_principal, const char *, const char *,
  55. int);
  56. static krb5_context krb_context = NULL;
  57. /* Initialise the krb5 library, for the stuff that GSSAPI won't do */
  58. static int
  59. ssh_gssapi_krb5_init(void)
  60. {
  61. krb5_error_code problem;
  62. if (krb_context != NULL)
  63. return 1;
  64. problem = krb5_init_context(&krb_context);
  65. if (problem) {
  66. logit("Cannot initialize krb5 context");
  67. return 0;
  68. }
  69. return 1;
  70. }
  71. /* Check if this user is OK to login. This only works with krb5 - other
  72. * GSSAPI mechanisms will need their own.
  73. * Returns true if the user is OK to log in, otherwise returns 0
  74. */
  75. static int
  76. ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
  77. {
  78. krb5_principal princ;
  79. int retval;
  80. const char *errmsg;
  81. int k5login_exists;
  82. if (ssh_gssapi_krb5_init() == 0)
  83. return 0;
  84. if ((retval = krb5_parse_name(krb_context, client->exportedname.value,
  85. &princ))) {
  86. errmsg = krb5_get_error_message(krb_context, retval);
  87. logit("krb5_parse_name(): %.100s", errmsg);
  88. krb5_free_error_message(krb_context, errmsg);
  89. return 0;
  90. }
  91. /* krb5_kuserok() returns 1 if .k5login DNE and this is self-login.
  92. * We have to make sure to check .k5users in that case. */
  93. k5login_exists = ssh_gssapi_k5login_exists();
  94. /* NOTE: .k5login and .k5users must opened as root, not the user,
  95. * because if they are on a krb5-protected filesystem, user credentials
  96. * to access these files aren't available yet. */
  97. if (krb5_kuserok(krb_context, princ, name) && k5login_exists) {
  98. retval = 1;
  99. logit("Authorized to %s, krb5 principal %s (krb5_kuserok)",
  100. name, (char *)client->displayname.value);
  101. } else if (ssh_gssapi_krb5_cmdok(princ, client->exportedname.value,
  102. name, k5login_exists)) {
  103. retval = 1;
  104. logit("Authorized to %s, krb5 principal %s "
  105. "(ssh_gssapi_krb5_cmdok)",
  106. name, (char *)client->displayname.value);
  107. } else
  108. retval = 0;
  109. krb5_free_principal(krb_context, princ);
  110. return retval;
  111. }
  112. /* Test for existence of .k5login.
  113. * We need this as part of our .k5users check, because krb5_kuserok()
  114. * returns success if .k5login DNE and user is logging in as himself.
  115. * With .k5login absent and .k5users present, we don't want absence
  116. * of .k5login to authorize self-login. (absence of both is required)
  117. * Returns 1 if .k5login is available, 0 otherwise.
  118. */
  119. static int
  120. ssh_gssapi_k5login_exists()
  121. {
  122. char file[MAXPATHLEN];
  123. struct passwd *pw = the_authctxt->pw;
  124. snprintf(file, sizeof(file), "%s/.k5login", pw->pw_dir);
  125. return access(file, F_OK) == 0;
  126. }
  127. /* check .k5users for login or command authorization
  128. * Returns 1 if principal is authorized, 0 otherwise.
  129. * If principal is authorized, (global) k5users_allowed_cmds may be populated.
  130. */
  131. static int
  132. ssh_gssapi_krb5_cmdok(krb5_principal principal, const char *name,
  133. const char *luser, int k5login_exists)
  134. {
  135. FILE *fp;
  136. char file[MAXPATHLEN];
  137. char *line = NULL;
  138. char kuser[65]; /* match krb5_kuserok() */
  139. struct stat st;
  140. struct passwd *pw = the_authctxt->pw;
  141. int found_principal = 0;
  142. int ncommands = 0, allcommands = 0;
  143. u_long linenum = 0;
  144. size_t linesize = 0;
  145. snprintf(file, sizeof(file), "%s/.k5users", pw->pw_dir);
  146. /* If both .k5login and .k5users DNE, self-login is ok. */
  147. if (!k5login_exists && (access(file, F_OK) == -1)) {
  148. return (krb5_aname_to_localname(krb_context, principal,
  149. sizeof(kuser), kuser) == 0) &&
  150. (strcmp(kuser, luser) == 0);
  151. }
  152. if ((fp = fopen(file, "r")) == NULL) {
  153. int saved_errno = errno;
  154. /* 2nd access check to ease debugging if file perms are wrong.
  155. * But we don't want to report this if .k5users simply DNE. */
  156. if (access(file, F_OK) == 0) {
  157. logit("User %s fopen %s failed: %s",
  158. pw->pw_name, file, strerror(saved_errno));
  159. }
  160. return 0;
  161. }
  162. /* .k5users must be owned either by the user or by root */
  163. if (fstat(fileno(fp), &st) == -1) {
  164. /* can happen, but very wierd error so report it */
  165. logit("User %s fstat %s failed: %s",
  166. pw->pw_name, file, strerror(errno));
  167. fclose(fp);
  168. return 0;
  169. }
  170. if (!(st.st_uid == pw->pw_uid || st.st_uid == 0)) {
  171. logit("User %s %s is not owned by root or user",
  172. pw->pw_name, file);
  173. fclose(fp);
  174. return 0;
  175. }
  176. /* .k5users must be a regular file. krb5_kuserok() doesn't do this
  177. * check, but we don't want to be deficient if they add a check. */
  178. if (!S_ISREG(st.st_mode)) {
  179. logit("User %s %s is not a regular file", pw->pw_name, file);
  180. fclose(fp);
  181. return 0;
  182. }
  183. /* file exists; initialize k5users_allowed_cmds (to none!) */
  184. k5users_allowed_cmds = xcalloc(++ncommands,
  185. sizeof(*k5users_allowed_cmds));
  186. /* Check each line. ksu allows unlimited length lines. */
  187. while (!allcommands && getline(&line, &linesize, fp) != -1) {
  188. linenum++;
  189. char *token;
  190. /* we parse just like ksu, even though we could do better */
  191. if ((token = strtok(line, " \t\n")) == NULL)
  192. continue;
  193. if (strcmp(name, token) == 0) {
  194. /* we matched on client principal */
  195. found_principal = 1;
  196. if ((token = strtok(NULL, " \t\n")) == NULL) {
  197. /* only shell is allowed */
  198. k5users_allowed_cmds[ncommands-1] =
  199. xstrdup(pw->pw_shell);
  200. k5users_allowed_cmds =
  201. xreallocarray(k5users_allowed_cmds, ++ncommands,
  202. sizeof(*k5users_allowed_cmds));
  203. break;
  204. }
  205. /* process the allowed commands */
  206. while (token) {
  207. if (strcmp(token, "*") == 0) {
  208. allcommands = 1;
  209. break;
  210. }
  211. k5users_allowed_cmds[ncommands-1] =
  212. xstrdup(token);
  213. k5users_allowed_cmds =
  214. xreallocarray(k5users_allowed_cmds, ++ncommands,
  215. sizeof(*k5users_allowed_cmds));
  216. token = strtok(NULL, " \t\n");
  217. }
  218. }
  219. }
  220. free(line);
  221. if (k5users_allowed_cmds) {
  222. /* terminate vector */
  223. k5users_allowed_cmds[ncommands-1] = NULL;
  224. /* if all commands are allowed, free vector */
  225. if (allcommands) {
  226. int i;
  227. for (i = 0; i < ncommands; i++) {
  228. free(k5users_allowed_cmds[i]);
  229. }
  230. free(k5users_allowed_cmds);
  231. k5users_allowed_cmds = NULL;
  232. }
  233. }
  234. fclose(fp);
  235. return found_principal;
  236. }
  237. /* This writes out any forwarded credentials from the structure populated
  238. * during userauth. Called after we have setuid to the user */
  239. static void
  240. ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client)
  241. {
  242. krb5_ccache ccache;
  243. krb5_error_code problem;
  244. krb5_principal princ;
  245. OM_uint32 maj_status, min_status;
  246. int len;
  247. const char *errmsg;
  248. if (client->creds == NULL) {
  249. debug("No credentials stored");
  250. return;
  251. }
  252. if (ssh_gssapi_krb5_init() == 0)
  253. return;
  254. #ifdef HEIMDAL
  255. # ifdef HAVE_KRB5_CC_NEW_UNIQUE
  256. if ((problem = krb5_cc_new_unique(krb_context, krb5_fcc_ops.prefix,
  257. NULL, &ccache)) != 0) {
  258. errmsg = krb5_get_error_message(krb_context, problem);
  259. logit("krb5_cc_new_unique(): %.100s", errmsg);
  260. # else
  261. if ((problem = krb5_cc_gen_new(krb_context, &krb5_fcc_ops, &ccache))) {
  262. logit("krb5_cc_gen_new(): %.100s",
  263. krb5_get_err_text(krb_context, problem));
  264. # endif
  265. krb5_free_error_message(krb_context, errmsg);
  266. return;
  267. }
  268. #else
  269. if ((problem = ssh_krb5_cc_gen(krb_context, &ccache))) {
  270. errmsg = krb5_get_error_message(krb_context, problem);
  271. logit("ssh_krb5_cc_gen(): %.100s", errmsg);
  272. krb5_free_error_message(krb_context, errmsg);
  273. return;
  274. }
  275. #endif /* #ifdef HEIMDAL */
  276. if ((problem = krb5_parse_name(krb_context,
  277. client->exportedname.value, &princ))) {
  278. errmsg = krb5_get_error_message(krb_context, problem);
  279. logit("krb5_parse_name(): %.100s", errmsg);
  280. krb5_free_error_message(krb_context, errmsg);
  281. return;
  282. }
  283. if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
  284. errmsg = krb5_get_error_message(krb_context, problem);
  285. logit("krb5_cc_initialize(): %.100s", errmsg);
  286. krb5_free_error_message(krb_context, errmsg);
  287. krb5_free_principal(krb_context, princ);
  288. krb5_cc_destroy(krb_context, ccache);
  289. return;
  290. }
  291. krb5_free_principal(krb_context, princ);
  292. if ((maj_status = gss_krb5_copy_ccache(&min_status,
  293. client->creds, ccache))) {
  294. logit("gss_krb5_copy_ccache() failed");
  295. krb5_cc_destroy(krb_context, ccache);
  296. return;
  297. }
  298. client->store.filename = xstrdup(krb5_cc_get_name(krb_context, ccache));
  299. client->store.envvar = "KRB5CCNAME";
  300. len = strlen(client->store.filename) + 6;
  301. client->store.envval = xmalloc(len);
  302. snprintf(client->store.envval, len, "FILE:%s", client->store.filename);
  303. #ifdef USE_PAM
  304. if (options.use_pam)
  305. do_pam_putenv(client->store.envvar, client->store.envval);
  306. #endif
  307. krb5_cc_close(krb_context, ccache);
  308. return;
  309. }
  310. ssh_gssapi_mech gssapi_kerberos_mech = {
  311. "toWM5Slw5Ew8Mqkay+al2g==",
  312. "Kerberos",
  313. {9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"},
  314. NULL,
  315. &ssh_gssapi_krb5_userok,
  316. NULL,
  317. &ssh_gssapi_krb5_storecreds
  318. };
  319. #endif /* KRB5 */
  320. #endif /* GSSAPI */