krb5_common.c revision 5843ad321944a028f6dee7e1fd4f9381c4953d07
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski/*
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski SSSD
c06dd8856a03b72f6b3f69e874f8700f10cb8522Christian Maeder
75a6279dbae159d018ef812185416cf6df386c10Till Mossakowski Kerberos Provider Common Functions
75a6279dbae159d018ef812185416cf6df386c10Till Mossakowski
c06dd8856a03b72f6b3f69e874f8700f10cb8522Christian Maeder Authors:
ae17d457c2d00d47d65e8cd510c3fd21b9516ccbTill Mossakowski Sumit Bose <sbose@redhat.com>
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski Copyright (C) 2008-2009 Red Hat
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder This program is free software; you can redistribute it and/or modify
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder it under the terms of the GNU General Public License as published by
679d3f541f7a9ede4079e045f7758873bb901872Till Mossakowski the Free Software Foundation; either version 3 of the License, or
679d3f541f7a9ede4079e045f7758873bb901872Till Mossakowski (at your option) any later version.
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder This program is distributed in the hope that it will be useful,
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski but WITHOUT ANY WARRANTY; without even the implied warranty of
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82d681fe6950e2a35f28fdefb874d060632faccaTill Mossakowski GNU General Public License for more details.
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski You should have received a copy of the GNU General Public License
e3c9174a782e90f965a0b080c22861c3ef5af12dTill Mossakowski along with this program. If not, see <http://www.gnu.org/licenses/>.
70e2af8d4bf21bcdfb53e9a0414e27173b577a1eTill Mossakowski*/
70e2af8d4bf21bcdfb53e9a0414e27173b577a1eTill Mossakowski#include <sys/types.h>
2b4130336e941b7d01c78a6da55449a4c6eca609Till Mossakowski#include <sys/stat.h>
2b4130336e941b7d01c78a6da55449a4c6eca609Till Mossakowski#include <unistd.h>
2b4130336e941b7d01c78a6da55449a4c6eca609Till Mossakowski#include <netdb.h>
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder#include <arpa/inet.h>
82d681fe6950e2a35f28fdefb874d060632faccaTill Mossakowski#include <ctype.h>
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder#include "providers/dp_backend.h"
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder#include "providers/krb5/krb5_common.h"
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maederstruct dp_option default_krb5_opts[] = {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_server", DP_OPT_STRING, NULL_STRING, NULL_STRING },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_realm", DP_OPT_STRING, NULL_STRING, NULL_STRING },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_ccachedir", DP_OPT_STRING, { "/tmp" }, NULL_STRING },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_ccname_template", DP_OPT_STRING, { "FILE:%d/krb5cc_%U_XXXXXX" }, NULL_STRING},
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_auth_timeout", DP_OPT_NUMBER, { .number = 15 }, NULL_NUMBER },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_keytab", DP_OPT_STRING, { "/etc/krb5.keytab" }, NULL_STRING },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_validate", DP_OPT_BOOL, BOOL_FALSE, BOOL_FALSE },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_kpasswd", DP_OPT_STRING, NULL_STRING, NULL_STRING },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_store_password_if_offline", DP_OPT_BOOL, BOOL_FALSE, BOOL_FALSE },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_renewable_lifetime", DP_OPT_STRING, NULL_STRING, NULL_STRING },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_lifetime", DP_OPT_STRING, NULL_STRING, NULL_STRING },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_renew_interval", DP_OPT_NUMBER, NULL_NUMBER, NULL_NUMBER },
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder { "krb5_use_fast", DP_OPT_STRING, NULL_STRING, NULL_STRING }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder};
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
c529224e0ec191fbaa87261f05c34f89c17b3f3aTill Mossakowskierrno_t check_and_export_lifetime(struct dp_option *opts, const int opt_id,
c529224e0ec191fbaa87261f05c34f89c17b3f3aTill Mossakowski const char *env_name)
c529224e0ec191fbaa87261f05c34f89c17b3f3aTill Mossakowski{
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder int ret;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder char *str;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder krb5_deltat lifetime;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder bool free_str = false;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder str = dp_opt_get_string(opts, opt_id);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (str == NULL || *str == '\0') {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(5, ("No lifetime configured.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return EOK;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (isdigit(str[strlen(str)-1])) {
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski str = talloc_asprintf(opts, "%ss", str);
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski if (str == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("talloc_asprintf failed\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return ENOMEM;
e3c9174a782e90f965a0b080c22861c3ef5af12dTill Mossakowski }
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski free_str = true;
8fe1a8e240ccd5f3682a936ef2fa4c22fee973bcTill Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = dp_opt_set_string(opts, opt_id, str);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski if (ret != EOK) {
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski DEBUG(1, ("dp_opt_set_string failed\n"));
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski goto done;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski }
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski }
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski ret = krb5_string_to_deltat(str, &lifetime);
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski if (ret != 0) {
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski DEBUG(1, ("Invalid value [%s] for a lifetime.\n", str));
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski ret = EINVAL;
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski goto done;
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski }
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder ret = setenv(env_name, str, 1);
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder if (ret != EOK) {
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski DEBUG(2, ("setenv [%s] failed.\n", env_name));
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski goto done;
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder }
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski ret = EOK;
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maederdone:
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder if (free_str) {
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder talloc_free(str);
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder }
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski return ret;
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski}
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maedererrno_t check_and_export_options(struct dp_option *opts,
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder struct sss_domain_info *dom,
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder struct krb5_ctx *krb5_ctx)
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder{
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder int ret;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder const char *realm;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder const char *dummy;
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski char *use_fast_str;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
ba904a15082557e939db689fcfba0c68c9a4f740Christian Maeder realm = dp_opt_get_cstring(opts, KRB5_REALM);
e9249d3ecd51a2b6a966a58669953e58d703adc6Till Mossakowski if (realm == NULL) {
ba904a15082557e939db689fcfba0c68c9a4f740Christian Maeder ret = dp_opt_set_string(opts, KRB5_REALM, dom->name);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
ba904a15082557e939db689fcfba0c68c9a4f740Christian Maeder DEBUG(1, ("dp_opt_set_string failed.\n"));
ba904a15082557e939db689fcfba0c68c9a4f740Christian Maeder return ret;
ba904a15082557e939db689fcfba0c68c9a4f740Christian Maeder }
ba904a15082557e939db689fcfba0c68c9a4f740Christian Maeder realm = dom->name;
ba904a15082557e939db689fcfba0c68c9a4f740Christian Maeder }
0c2a90cbfb63865ff485c3fbe20a14589a5914beTill Mossakowski
c616e681da8c052b62e14247fea522da099ac0e4Christian Maeder ret = setenv(SSSD_KRB5_REALM, realm, 1);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
ba904a15082557e939db689fcfba0c68c9a4f740Christian Maeder DEBUG(2, ("setenv %s failed, authentication might fail.\n",
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski SSSD_KRB5_REALM));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski ret = check_and_export_lifetime(opts, KRB5_RENEWABLE_LIFETIME,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder SSSD_KRB5_RENEWABLE_LIFETIME);
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski if (ret != EOK) {
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski DEBUG(1, ("Failed to check value of krb5_renewable_lifetime. [%d][%s]\n",
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski ret, strerror(ret)));
31c49f2fa23d4ac089f35145d80a224deb6ea7e4Till Mossakowski return ret;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = check_and_export_lifetime(opts, KRB5_LIFETIME,
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski SSSD_KRB5_LIFETIME);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("Failed to check value of krb5_lifetime. [%d][%s]\n",
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret, strerror(ret)));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return ret;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowski
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder use_fast_str = dp_opt_get_string(opts, KRB5_USE_FAST);
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski if (use_fast_str != NULL) {
31c49f2fa23d4ac089f35145d80a224deb6ea7e4Till Mossakowski ret = check_fast(use_fast_str, &krb5_ctx->use_fast);
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski if (ret != EOK) {
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski DEBUG(1, ("check_fast failed.\n"));
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski return ret;
1b05bdb88b90d3c947351f262d7ae7d68f0a4a6fTill Mossakowski }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski if (krb5_ctx->use_fast) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = setenv(SSSD_KRB5_USE_FAST, use_fast_str, 1);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski DEBUG(2, ("setenv [%s] failed.\n", SSSD_KRB5_USE_FAST));
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder dummy = dp_opt_get_cstring(opts, KRB5_KDC);
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski if (dummy == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("No KDC explicitly configured, using defaults.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder dummy = dp_opt_get_cstring(opts, KRB5_KPASSWD);
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski if (dummy == NULL) {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski DEBUG(1, ("No kpasswd server explicitly configured, "
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski "using the KDC or defaults.\n"));
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder dummy = dp_opt_get_cstring(opts, KRB5_CCNAME_TMPL);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski if (dummy == NULL) {
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski DEBUG(1, ("Missing credential cache name template.\n"));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski return EINVAL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski if (dummy[0] != '/' && strncmp(dummy, "FILE:", 5) != 0) {
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski DEBUG(1, ("Currently only file based credential caches are supported "
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder "and krb5ccname_template must start with '/' or 'FILE:'\n"));
788dd403da4203e895e15892ef7fa48129617d30Till Mossakowski return EINVAL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return EOK;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder}
a938729e277da5c7742bb88946ab2c150416fd5dTill Mossakowski
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowskierrno_t krb5_try_kdcip(TALLOC_CTX *memctx, struct confdb_ctx *cdb,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder const char *conf_path, struct dp_option *opts)
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder{
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder char *krb5_servers = NULL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder errno_t ret;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder krb5_servers = dp_opt_get_string(opts, KRB5_KDC);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski if (krb5_servers == NULL) {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski DEBUG(4, ("No KDC found in configuration, trying legacy option\n"));
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski ret = confdb_get_string(cdb, memctx, conf_path,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder "krb5_kdcip", NULL, &krb5_servers);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("confdb_get_string failed.\n"));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski return ret;
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (krb5_servers != NULL)
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = dp_opt_set_string(opts, KRB5_KDC, krb5_servers);
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski if (ret != EOK) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("dp_opt_set_string failed.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder talloc_free(krb5_servers);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return ret;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(9, ("Set krb5 server [%s] based on legacy krb5_kdcip option\n"));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski DEBUG(0, ("Your configuration uses the deprecated option 'krb5_kdcip' "
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder "to specify the KDC. Please change the configuration to use "
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder "the 'krb5_server' option instead."));
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski }
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return EOK;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder}
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maedererrno_t krb5_get_options(TALLOC_CTX *memctx, struct confdb_ctx *cdb,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder const char *conf_path, struct dp_option **_opts)
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski{
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski int ret;
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski struct dp_option *opts;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder opts = talloc_zero(memctx, struct dp_option);
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder if (opts == NULL) {
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder DEBUG(1, ("talloc_zero failed.\n"));
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder return ENOMEM;
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder }
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski ret = dp_get_options(opts, cdb, conf_path, default_krb5_opts,
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski KRB5_OPTS, &opts);
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski if (ret != EOK) {
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski DEBUG(1, ("dp_get_options failed.\n"));
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski goto done;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski }
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski /* If there is no KDC, try the deprecated krb5_kdcip option, too */
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski /* FIXME - this can be removed in a future version */
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski ret = krb5_try_kdcip(memctx, cdb, conf_path, opts);
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski if (ret != EOK) {
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski DEBUG(1, ("sss_krb5_try_kdcip failed.\n"));
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski goto done;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski }
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski *_opts = opts;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski ret = EOK;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maederdone:
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder talloc_zfree(opts);
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski }
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return ret;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder}
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maedererrno_t write_krb5info_file(const char *realm, const char *server,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder const char *service)
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder{
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder int ret;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski int fd = -1;
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski char *tmp_name = NULL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder char *krb5info_name = NULL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder TALLOC_CTX *tmp_ctx = NULL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder const char *name_tmpl = NULL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder int server_len;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ssize_t written;
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski if (realm == NULL || *realm == '\0' || server == NULL || *server == '\0' ||
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski service == NULL || service == '\0') {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("Missing or empty realm, server or service.\n"));
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski return EINVAL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (strcmp(service, SSS_KRB5KDC_FO_SRV) == 0) {
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski name_tmpl = KDCINFO_TMPL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder } else if (strcmp(service, SSS_KRB5KPASSWD_FO_SRV) == 0) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder name_tmpl = KPASSWDINFO_TMPL;
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski } else {
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski DEBUG(1, ("Unsupported service [%s]\n.", service));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski return EINVAL;
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski }
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski
a205324331077b7d5a2c08fb3f0f57e5c029f9aaTill Mossakowski server_len = strlen(server);
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder tmp_ctx = talloc_new(NULL);
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder if (tmp_ctx == NULL) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder DEBUG(1, ("talloc_new failed.\n"));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski return ENOMEM;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski tmp_name = talloc_asprintf(tmp_ctx, PUBCONF_PATH"/.krb5info_dummy_XXXXXX");
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski if (tmp_name == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("talloc_asprintf failed.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = ENOMEM;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski goto done;
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski krb5info_name = talloc_asprintf(tmp_ctx, name_tmpl, realm);
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski if (krb5info_name == NULL) {
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski DEBUG(1, ("talloc_asprintf failed.\n"));
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski ret = ENOMEM;
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski goto done;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski fd = mkstemp(tmp_name);
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski if (fd == -1) {
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski ret = errno;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski DEBUG(1, ("mkstemp failed [%d][%s].\n", ret, strerror(ret)));
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski goto done;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder written = 0;
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski while (written < server_len) {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski ret = write(fd, server+written, server_len-written);
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski if (ret == -1) {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski if (errno == EINTR || errno == EAGAIN) {
21dae7237ac384abdb94a81e00b3f099873ec623Till Mossakowski continue;
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski }
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski ret = errno;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("write failed [%d][%s].\n", ret, strerror(ret)));
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski goto done;
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski }
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski else {
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski written += ret;
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski }
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski }
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski if (written != server_len) {
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski DEBUG(1, ("Write error, wrote [%d] bytes, expected [%d]\n",
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski written, server_len));
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski ret = EIO;
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski goto done;
d08907a7832988612fbc0682b216e150d1e738d2Christian Maeder }
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski ret = fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski if (ret == -1) {
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski ret = errno;
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski DEBUG(1, ("fchmod failed [%d][%s].\n", ret, strerror(ret)));
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski goto done;
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski }
1a7b7802544aa94828d7f4e7be5788501c572934Till Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = close(fd);
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder if (ret == -1) {
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski ret = errno;
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski DEBUG(1, ("close failed [%d][%s].\n", ret, strerror(ret)));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski goto done;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder ret = rename(tmp_name, krb5info_name);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski if (ret == -1) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder ret = errno;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("rename failed [%d][%s].\n", ret, strerror(ret)));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder goto done;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski }
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maederdone:
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder talloc_free(tmp_ctx);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return ret;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski}
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maederstatic void krb5_resolve_callback(void *private_data, struct fo_server *server)
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder{
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski struct krb5_service *krb5_service;
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski struct hostent *srvaddr;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder char *address;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder int ret;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski krb5_service = talloc_get_type(private_data, struct krb5_service);
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski if (!krb5_service) {
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski DEBUG(1, ("FATAL: Bad private_data\n"));
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski return;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski srvaddr = fo_get_server_hostent(server);
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder if (!srvaddr) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("FATAL: No hostent available for server (%s)\n",
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder fo_get_server_name(server)));
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski return;
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder address = talloc_zero_size(krb5_service, 128);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (address == NULL) {
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski DEBUG(1, ("talloc_zero failed.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski if (inet_ntop(srvaddr->h_addrtype, srvaddr->h_addr_list[0],
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski address, 128) == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = errno;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("inet_ntop failed [%d][%s].\n", ret, strerror(ret)));
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder return;
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski }
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski address = talloc_asprintf_append(address, ":%d",
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski fo_get_server_port(server));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski if (address == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("talloc_asprintf_append failed.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder talloc_zfree(krb5_service->address);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder krb5_service->address = address;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski ret = write_krb5info_file(krb5_service->realm, address,
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski krb5_service->name);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(2, ("write_krb5info_file failed, authentication might fail.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder return;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder}
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
c0380b947eef252db81ee562246bb732555427f4Till Mossakowskiint krb5_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder const char *service_name, const char *servers,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder const char *realm, struct krb5_service **_service)
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder{
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder TALLOC_CTX *tmp_ctx;
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder struct krb5_service *service;
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder char **list = NULL;
1bb1684c83317dfd1692ab53415027b67d8f2faeTill Mossakowski int ret;
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder int i;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder char *port_str;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder long port;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski char *server_spec;
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski char *endptr;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder struct servent *servent;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder tmp_ctx = talloc_new(memctx);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (!tmp_ctx) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return ENOMEM;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski service = talloc_zero(tmp_ctx, struct krb5_service);
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder if (!service) {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski ret = ENOMEM;
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski goto done;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = be_fo_add_service(ctx, service_name);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski DEBUG(1, ("Failed to create failover service!\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder goto done;
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski }
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder service->name = talloc_strdup(service, service_name);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (!service->name) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = ENOMEM;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder goto done;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski service->realm = talloc_strdup(service, realm);
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder if (!service->realm) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder ret = ENOMEM;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder goto done;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski if (!servers) {
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski servers = BE_SRV_IDENTIFIER;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski ret = split_on_separator(tmp_ctx, servers, ',', true, &list, NULL);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("Failed to parse server list!\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder goto done;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder for (i = 0; list[i]; i++) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder talloc_steal(service, list[i]);
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski server_spec = talloc_strdup(service, list[i]);
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski if (!server_spec) {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski ret = ENOMEM;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder goto done;
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (be_fo_is_srv_identifier(server_spec)) {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski ret = be_fo_add_srv_server(ctx, service_name, service_name,
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski BE_FO_PROTO_UDP, true, NULL);
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski if (ret) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(0, ("Failed to add server\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder goto done;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder }
6218c1f87fbce3087d7d013c41a3adac5ade1d9cChristian Maeder
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder DEBUG(6, ("Added service lookup\n"));
c616e681da8c052b62e14247fea522da099ac0e4Christian Maeder continue;
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder port_str = strrchr(server_spec, ':');
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski if (port_str == NULL) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder port = 0;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder } else {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski *port_str = '\0';
74e82e43f5787027c5d4e523397525a259d6d001Christian Maeder ++port_str;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder if (isdigit(*port_str)) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder errno = 0;
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski port = strtol(port_str, &endptr, 10);
74e82e43f5787027c5d4e523397525a259d6d001Christian Maeder if (errno != 0) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder ret = errno;
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski DEBUG(1, ("strtol failed on [%s]: [%d][%s].\n", port_str,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret, strerror(ret)));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder goto done;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (*endptr != '\0') {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("Found additional characters [%s] in port number "
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder "[%s].\n", endptr, port_str));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski ret = EINVAL;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder goto done;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski if (port < 1 || port > 65535) {
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski DEBUG(1, ("Illegal port number [%d].\n", port));
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski ret = EINVAL;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski goto done;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski }
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski } else if (isalpha(*port_str)) {
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski servent = getservbyname(port_str, NULL);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski if (servent == NULL) {
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski DEBUG(1, ("getservbyname cannot find service [%s].\n",
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder port_str));
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski ret = EINVAL;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski goto done;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski port = servent->s_port;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski } else {
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski DEBUG(1, ("Unsupported port specifier in [%s].\n", list[i]));
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski ret = EINVAL;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski goto done;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski ret = be_fo_add_server(ctx, service_name, server_spec, (int) port,
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski list[i]);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret && ret != EEXIST) {
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski DEBUG(0, ("Failed to add server\n"));
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski goto done;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski DEBUG(6, ("Added Server %s\n", list[i]));
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski ret = be_fo_service_add_callback(memctx, ctx, service_name,
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski krb5_resolve_callback, service);
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski if (ret != EOK) {
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski DEBUG(1, ("Failed to add failover callback!\n"));
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski goto done;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski ret = EOK;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowskidone:
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski if (ret == EOK) {
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski *_service = talloc_steal(memctx, service);
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski talloc_zfree(tmp_ctx);
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski return ret;
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski}
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maedererrno_t remove_krb5_info_files(TALLOC_CTX *mem_ctx, const char *realm)
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder{
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski int ret;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski errno_t err;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder char *file;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder file = talloc_asprintf(mem_ctx, KDCINFO_TMPL, realm);
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski if(file == NULL) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder DEBUG(1, ("talloc_asprintf failed.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return ENOMEM;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder errno = 0;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = unlink(file);
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski if (ret == -1) {
242691238a8d1a89581751d782af87ec5d7470c0Till Mossakowski err = errno;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(5, ("Could not remove [%s], [%d][%s]\n", file,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder err, strerror(err)));
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder file = talloc_asprintf(mem_ctx, KPASSWDINFO_TMPL, realm);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if(file == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("talloc_asprintf failed.\n"));
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski return ENOMEM;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder errno = 0;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = unlink(file);
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski if (ret == -1) {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski err = errno;
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski DEBUG(5, ("Could not remove [%s], [%d][%s]\n", file,
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski err, strerror(err)));
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder }
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski return EOK;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder}
f534c0116096e25659ceaa57de030c497ce9345aTill Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maedervoid remove_krb5_info_files_callback(void *pvt)
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder{
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder int ret;
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski TALLOC_CTX *tmp_ctx = NULL;
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski struct remove_info_files_ctx *ctx = talloc_get_type(pvt,
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski struct remove_info_files_ctx);
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski ret = be_fo_run_callbacks_at_next_request(ctx->be_ctx,
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder ctx->kdc_service_name);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski DEBUG(1, ("be_fo_run_callbacks_at_next_request failed, "
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder "krb5 info files will not be removed, because "
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski "it is unclear if they will be recreated properly.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return;
a946fa1fe525f04a8b4e2734fa2082bbe5e6ed3fTill Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ctx->kpasswd_service_name != NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = be_fo_run_callbacks_at_next_request(ctx->be_ctx,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ctx->kpasswd_service_name);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski if (ret != EOK) {
e379124f467e5d0ef7d3c0ca238bff0521f70831Till Mossakowski DEBUG(1, ("be_fo_run_callbacks_at_next_request failed, "
74e82e43f5787027c5d4e523397525a259d6d001Christian Maeder "krb5 info files will not be removed, because "
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder "it is unclear if they will be recreated properly.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder tmp_ctx = talloc_new(NULL);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (tmp_ctx == NULL) {
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski DEBUG(1, ("talloc_new failed, cannot remove krb5 info files.\n"));
e3c9174a782e90f965a0b080c22861c3ef5af12dTill Mossakowski return;
e3c9174a782e90f965a0b080c22861c3ef5af12dTill Mossakowski }
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski ret = remove_krb5_info_files(tmp_ctx, ctx->realm);
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski if (ret != EOK) {
21dae7237ac384abdb94a81e00b3f099873ec623Till Mossakowski DEBUG(1, ("remove_krb5_info_files failed.\n"));
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski }
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski
49b9a9cbf17489cbaf97431247161f42e9fc5ae0Till Mossakowski talloc_zfree(tmp_ctx);
49b9a9cbf17489cbaf97431247161f42e9fc5ae0Till Mossakowski}
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowskivoid krb5_finalize(struct tevent_context *ev,
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski struct tevent_signal *se,
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski int signum,
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski int count,
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowski void *siginfo,
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowski void *private_data)
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowski{
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowski char *realm = (char *)private_data;
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski int ret;
5b1394673f35f4d23cfe08175841ab414a39678eMarkus Roggenbach
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski ret = remove_krb5_info_files(se, realm);
e3c9174a782e90f965a0b080c22861c3ef5af12dTill Mossakowski if (ret != EOK) {
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski DEBUG(1, ("remove_krb5_info_files failed.\n"));
5b1394673f35f4d23cfe08175841ab414a39678eMarkus Roggenbach }
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowski sig_term(signum);
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski}
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowski
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowskierrno_t krb5_install_offline_callback(struct be_ctx *be_ctx,
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowski struct krb5_ctx *krb5_ctx)
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski{
21dae7237ac384abdb94a81e00b3f099873ec623Till Mossakowski int ret;
21dae7237ac384abdb94a81e00b3f099873ec623Till Mossakowski struct remove_info_files_ctx *ctx;
21dae7237ac384abdb94a81e00b3f099873ec623Till Mossakowski const char *krb5_realm;
1df33829303cbf924aa018ac5ce9a28e69c17d22Till Mossakowski
b3dca469a9e267d6d71acfdeca7bf284d0581dc7Till Mossakowski if (krb5_ctx->service == NULL || krb5_ctx->service->name == NULL) {
5b1394673f35f4d23cfe08175841ab414a39678eMarkus Roggenbach DEBUG(1, ("Missing KDC service name!\n"));
e3c9174a782e90f965a0b080c22861c3ef5af12dTill Mossakowski return EINVAL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski ctx = talloc_zero(krb5_ctx, struct remove_info_files_ctx);
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski if (ctx == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("talloc_zfree failed.\n"));
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski return ENOMEM;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder krb5_realm = dp_opt_get_cstring(krb5_ctx->opts, KRB5_REALM);
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder if (krb5_realm == NULL) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder DEBUG(1, ("Missing krb5_realm option!\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = EINVAL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder goto done;
4184cb191a9081cb2a9cf3ef5f060f56f0ca5922Till Mossakowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski ctx->realm = talloc_strdup(ctx, krb5_realm);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ctx->realm == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("talloc_strdup failed!\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = ENOMEM;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder goto done;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ctx->be_ctx = be_ctx;
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski ctx->kdc_service_name = krb5_ctx->service->name;
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski if (krb5_ctx->kpasswd_service == NULL) {
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder ctx->kpasswd_service_name =NULL;
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski } else {
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski ctx->kpasswd_service_name = krb5_ctx->kpasswd_service->name;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = be_add_offline_cb(ctx, be_ctx, remove_krb5_info_files_callback, ctx,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder NULL);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("be_add_offline_cb failed.\n"));
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder goto done;
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder ret = EOK;
4184cb191a9081cb2a9cf3ef5f060f56f0ca5922Till Mossakowski
4184cb191a9081cb2a9cf3ef5f060f56f0ca5922Till Mossakowskidone:
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (ret != EOK) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder talloc_zfree(ctx);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski
58b671de3fe578346fef9642ffa3c5a0a0edb3cbTill Mossakowski return ret;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder}
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maedererrno_t krb5_install_sigterm_handler(struct tevent_context *ev,
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder struct krb5_ctx *krb5_ctx)
e9249d3ecd51a2b6a966a58669953e58d703adc6Till Mossakowski{
e9249d3ecd51a2b6a966a58669953e58d703adc6Till Mossakowski const char *krb5_realm;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder char *sig_realm;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder struct tevent_signal *sige;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
4184cb191a9081cb2a9cf3ef5f060f56f0ca5922Till Mossakowski BlockSignals(false, SIGTERM);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder krb5_realm = dp_opt_get_cstring(krb5_ctx->opts, KRB5_REALM);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (krb5_realm == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("Missing krb5_realm option!\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return EINVAL;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder sig_realm = talloc_strdup(krb5_ctx, krb5_realm);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder if (sig_realm == NULL) {
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder DEBUG(1, ("talloc_strdup failed!\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return ENOMEM;
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski }
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski sige = tevent_add_signal(ev, krb5_ctx, SIGTERM, SA_SIGINFO, krb5_finalize,
7297175957c5ad3c0498032190b1dee9ec5fb873Christian Maeder sig_realm);
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski if (sige == NULL) {
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski DEBUG(1, ("tevent_add_signal failed.\n"));
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski talloc_free(sig_realm);
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski return ENOMEM;
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski }
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski talloc_steal(sige, sig_realm);
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
788dd403da4203e895e15892ef7fa48129617d30Till Mossakowski return EOK;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder}
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maedererrno_t krb5_get_simple_upn(TALLOC_CTX *mem_ctx, struct krb5_ctx *krb5_ctx,
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski const char *username, const char **_upn)
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski{
6a57a555c8ef0a79aa5d20e1d721400dbffa564aMaciek Makowski const char *realm;
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski char *upn;
79ee6b8eb396ed31807784a4bb1c9cc2ce094835Till Mossakowski
79ee6b8eb396ed31807784a4bb1c9cc2ce094835Till Mossakowski realm = dp_opt_get_cstring(krb5_ctx->opts, KRB5_REALM);
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder if (realm == NULL) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder DEBUG(1, ("Missing Kerberos realm.\n"));
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder return ENOENT;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
0c2a90cbfb63865ff485c3fbe20a14589a5914beTill Mossakowski
0c2a90cbfb63865ff485c3fbe20a14589a5914beTill Mossakowski /* NOTE: this is a hack, works only in some environments */
79ee6b8eb396ed31807784a4bb1c9cc2ce094835Till Mossakowski upn = talloc_asprintf(mem_ctx, "%s@%s", username, realm);
79ee6b8eb396ed31807784a4bb1c9cc2ce094835Till Mossakowski if (upn == NULL) {
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder DEBUG(1, ("talloc_asprintf failed.\n"));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return ENOMEM;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder }
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder
3476beb5baf84bef7cc7d627b130de9d48700399Christian Maeder DEBUG(9, ("Using simple UPN [%s].\n", upn));
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder *_upn = upn;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder return EOK;
95c3e5d11dcee331dc3876a9bf0c1d6daa38e2caChristian Maeder}
c0380b947eef252db81ee562246bb732555427f4Till Mossakowski