krb5_utils.c revision 318f12c90208971a5b6d3574f0026601161d81c7
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder Kerberos 5 Backend Module -- Utilities
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder Sumit Bose <sbose@redhat.com>
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder Copyright (C) 2009 Red Hat
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder This program is free software; you can redistribute it and/or modify
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder it under the terms of the GNU General Public License as published by
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder the Free Software Foundation; either version 3 of the License, or
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder (at your option) any later version.
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder This program is distributed in the hope that it will be useful,
3ab1e7a18f3fc3eb004464bc54b7df4483f1f060Christian Maeder but WITHOUT ANY WARRANTY; without even the implied warranty of
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wang GNU General Public License for more details.
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder You should have received a copy of the GNU General Public License
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder along with this program. If not, see <http://www.gnu.org/licenses/>.
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wangchar *expand_ccname_template(TALLOC_CTX *mem_ctx, struct krb5child_req *kr,
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wang if ( *n == '\0' ) {
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wang DEBUG(1, ("format error, single %% at the end of the template.\n"));
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wang switch( *n ) {
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder DEBUG(1, ("Cannot expand user name template "
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wang "because user name is empty.\n"));
264b794970b6f2bd437f14233f367f1067565728Jian Chun Wang result = talloc_asprintf_append(result, "%s%s", p,
264b794970b6f2bd437f14233f367f1067565728Jian Chun Wang "because uid is invalid.\n"));
264b794970b6f2bd437f14233f367f1067565728Jian Chun Wang result = talloc_asprintf_append(result, "%s%d", p,
eab576044505ba1fbc64610323053490fbd9e82cChristian Maeder DEBUG(1, ("Cannot expand user principal name template "
57d9ffd4f0d821632c5dd116a5301c3305599b19Christian Maeder "because upn is empty.\n"));
264b794970b6f2bd437f14233f367f1067565728Jian Chun Wang result = talloc_asprintf_append(result, "%s%s", p, kr->upn);
8519df804b37f95a2394a6cd5662da02efa3400bChristian Maeder result = talloc_asprintf_append(result, "%s%%", p);
8519df804b37f95a2394a6cd5662da02efa3400bChristian Maeder dummy = dp_opt_get_string(kr->krb5_ctx->opts, KRB5_REALM);
8519df804b37f95a2394a6cd5662da02efa3400bChristian Maeder result = talloc_asprintf_append(result, "%s%s", p, dummy);
264b794970b6f2bd437f14233f367f1067565728Jian Chun Wang DEBUG(1, ("Cannot expand home directory template "
03a6bbff551286168d0b15dc53476c2ede4e60d0Christian Maeder "because the path is not available.\n"));
66939c546b3eaf25eb34d1dc36c0c82943f85552Christian Maeder result = talloc_asprintf_append(result, "%s%s", p, kr->homedir);
66939c546b3eaf25eb34d1dc36c0c82943f85552Christian Maeder cache_dir_tmpl = dp_opt_get_string(kr->krb5_ctx->opts,
8c4c53f1d84490c7eac208905e92964c6508c1d6Christian Maeder DEBUG(1, ("Missing credential cache directory.\n"));
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder dummy = expand_ccname_template(mem_ctx, kr, cache_dir_tmpl,
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder DEBUG(1, ("Expanding credential cache directory "
0015e1756b734b34d4b550318c078f9a0c585611Christian Maeder "template failed.\n"));
0015e1756b734b34d4b550318c078f9a0c585611Christian Maeder result = talloc_asprintf_append(result, "%s%s", p, dummy);
0015e1756b734b34d4b550318c078f9a0c585611Christian Maeder DEBUG(1, ("'%%d' is not allowed in this template.\n"));
0015e1756b734b34d4b550318c078f9a0c585611Christian Maeder DEBUG(1, ("'%%P' is not allowed in this template.\n"));
0015e1756b734b34d4b550318c078f9a0c585611Christian Maeder "because PID is not available.\n"));
0015e1756b734b34d4b550318c078f9a0c585611Christian Maeder result = talloc_asprintf_append(result, "%s%d", p,
0015e1756b734b34d4b550318c078f9a0c585611Christian Maeder DEBUG(1, ("format error, unknown template [%%%c].\n", *n));
0015e1756b734b34d4b550318c078f9a0c585611Christian Maeder DEBUG(1, ("talloc_asprintf_append failed.\n"));
03a6bbff551286168d0b15dc53476c2ede4e60d0Christian Maeder result = talloc_asprintf_append(result, "%s", p);
c4a8059d0469a85bb58c28ac66729ac19d743d3cChristian Maederstatic errno_t check_parent_stat(bool private_path, struct stat *parent_stat,
c4a8059d0469a85bb58c28ac66729ac19d743d3cChristian Maeder if (!((parent_stat->st_uid == 0 && parent_stat->st_gid == 0) ||
03a6bbff551286168d0b15dc53476c2ede4e60d0Christian Maeder DEBUG(1, ("Private directory can only be created below a "
03a6bbff551286168d0b15dc53476c2ede4e60d0Christian Maeder "directory belonging to root or to [%d][%d].\n",
c4a8059d0469a85bb58c28ac66729ac19d743d3cChristian Maeder DEBUG(1, ("Parent directory does have the search bit set for "
66939c546b3eaf25eb34d1dc36c0c82943f85552Christian Maeder "the owner.\n"));
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wang DEBUG(1, ("Parent directory does have the search bit set for "
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wang "others.\n"));
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder if (parent_stat->st_uid != 0 || parent_stat->st_gid != 0) {
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder DEBUG(1, ("Public directory cannot be created below a user "
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder "directory.\n"));
50ed946595d60c06f773e73bb22b21f5cf1199caChristian Maeder DEBUG(1, ("Parent directory does have the search bit set for "
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder "others.\n"));
d06598e0c310f65ab552ca55626c2f7694ffd5e3Christian Maederstatic errno_t find_ccdir_parent_data(TALLOC_CTX *mem_ctx, const char *dirname,
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder DEBUG(1, ("[%s] is not a directory.\n", dirname));
db453fe9625a9dab5d108f7a5e464598814144b8Jian Chun Wang DEBUG(1, ("stat for [%s] failed: [%d][%s].\n", dirname, ret,
db453fe9625a9dab5d108f7a5e464598814144b8Jian Chun Wang li = talloc_zero(mem_ctx, struct string_list);
db453fe9625a9dab5d108f7a5e464598814144b8Jian Chun Wang DEBUG(1, ("Cannot find parent directory of [%s], / is not allowed.\n",
db453fe9625a9dab5d108f7a5e464598814144b8Jian Chun Wang ret = find_ccdir_parent_data(mem_ctx, parent, parent_stat, missing_parents);
8c4c53f1d84490c7eac208905e92964c6508c1d6Christian Maedererrno_t create_ccache_dir(TALLOC_CTX *mem_ctx, const char *filename,
abcb1baa565c878598d732d0aa7724f474c9265cChristian Maeder dirname = talloc_strdup(tmp_ctx, filename + offset);
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder DEBUG(1, ("Only absolute paths are allowed, not [%s] .\n", dirname));
b9fef2e5a40737182d451b846f6656c9abebb203Christian Maeder ret = pcre_exec(illegal_re, NULL, dirname, strlen(dirname),
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder DEBUG(1, ("Illegal pattern in ccache directory name [%s].\n",
07284edd3063927c626df9c1cea9a0a7c2774efdChristian Maeder DEBUG(9, ("Ccache directory name [%s] does not contain "
07284edd3063927c626df9c1cea9a0a7c2774efdChristian Maeder DEBUG(1, ("pcre_exec failed [%d].\n", ret));
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder DEBUG(1, ("Missing filename in [%s].\n", dirname));
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder ret = find_ccdir_parent_data(tmp_ctx, dirname, &parent_stat,
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder DEBUG(1, ("find_ccdir_parent_data failed.\n"));
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder ret = check_parent_stat(private_path, &parent_stat, uid, gid);
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder DEBUG(1, ("check_parent_stat failed for %s directory [%s].\n",
9031d53c51b21d50ff4af9e8a365f0252401539fChristian Maeder private_path ? "private" : "public", dirname));
ce944c156ca6b4a56e81e232d7a22e582fbdcf33Christian Maeder DEBUG(9, ("Creating directory [%s].\n", li->s));
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder new_dir_mode = private_path ? 0700 : 01777;
d06598e0c310f65ab552ca55626c2f7694ffd5e3Christian Maeder parent_stat.st_uid == uid && parent_stat.st_gid == gid) {
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder DEBUG(1, ("mkdir [%s] failed: [%d][%s].\n", li->s, ret,
f799084b320209cdd71a29e74fff1be054c1d342Christian Maeder ((parent_stat.st_uid == uid && parent_stat.st_gid == gid) ||
abcb1baa565c878598d732d0aa7724f474c9265cChristian Maeder DEBUG(1, ("chown failed [%d][%s].\n", ret, strerror(ret)));
f799084b320209cdd71a29e74fff1be054c1d342Christian Maedererrno_t get_ccache_file_data(const char *ccache_file, const char *client_name,
47d6bc7bc9a708427f96be8d805f712697ad3d9eChristian Maeder kerr = krb5_parse_name(ctx, client_name, &client_princ);
47d6bc7bc9a708427f96be8d805f712697ad3d9eChristian Maeder server_name = talloc_asprintf(NULL, "krbtgt/%.*s@%.*s",
47d6bc7bc9a708427f96be8d805f712697ad3d9eChristian Maeder krb5_princ_realm(ctx, client_princ)->length,
47d6bc7bc9a708427f96be8d805f712697ad3d9eChristian Maeder krb5_princ_realm(ctx, client_princ)->length,
47d6bc7bc9a708427f96be8d805f712697ad3d9eChristian Maeder krb5_princ_realm(ctx, client_princ)->data);
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wang kerr = krb5_parse_name(ctx, server_name, &server_princ);
5be2fb5bcfaa6abbb6043d679a1d536b4878b789Jian Chun Wang kerr = krb5_cc_resolve(ctx, ccache_file, &cc);
5a06d13dafa3f6c8470951a4c7cb05891ed12328Jian Chun Wang kerr = krb5_cc_retrieve_cred(ctx, cc, 0, &mcred, &cred);
5a06d13dafa3f6c8470951a4c7cb05891ed12328Jian Chun Wang DEBUG(1, ("krb5_cc_retrieve_cred failed.\n"));
if (kerr != 0) {
goto done;
kerr = 0;
done:
if (kerr != 0) {
return EIO;
return EOK;