sysdb_services.c revision a928f7a6bd7681db6e26cba3eb7da22d14288737
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher/*
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan SSSD
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan Authors:
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher Stephen Gallagher <sgallagh@redhat.com>
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek Copyright (C) 2012 Red Hat
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek This program is free software; you can redistribute it and/or modify
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan it under the terms of the GNU General Public License as published by
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan the Free Software Foundation; either version 3 of the License, or
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek (at your option) any later version.
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
a86d6cd05e3f823214587475b83d907f394c035eJakub Hrozek This program is distributed in the hope that it will be useful,
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek GNU General Public License for more details.
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher You should have received a copy of the GNU General Public License
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan along with this program. If not, see <http://www.gnu.org/licenses/>.
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan*/
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek#include "util/util.h"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek#include "db/sysdb.h"
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan#include "db/sysdb_private.h"
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher#include "db/sysdb_services.h"
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagherstatic errno_t
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozeksysdb_svc_update(struct sysdb_ctx *sysdb,
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek struct ldb_dn *dn,
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek int port,
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek const char **aliases,
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek const char **protocols);
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
a86d6cd05e3f823214587475b83d907f394c035eJakub Hrozekerrno_t
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivansysdb_svc_remove_alias(struct sysdb_ctx *sysdb,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct ldb_dn *dn,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan const char *alias);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivanerrno_t
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivansysdb_getservbyname(TALLOC_CTX *mem_ctx,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct sss_domain_info *domain,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan const char *name,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher const char *proto,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher struct ldb_result **_res)
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek{
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek errno_t ret;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek TALLOC_CTX *tmp_ctx;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek static const char *attrs[] = SYSDB_SVC_ATTRS;
a86d6cd05e3f823214587475b83d907f394c035eJakub Hrozek char *sanitized_name;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan char *sanitized_proto;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan char *subfilter;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct ldb_result *res = NULL;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher struct ldb_message **msgs;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan size_t msgs_count;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan tmp_ctx = talloc_new(NULL);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (!tmp_ctx) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher return ENOMEM;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher ret = sss_filter_sanitize(tmp_ctx, name, &sanitized_name);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher if (ret != EOK) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher if (proto) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher ret = sss_filter_sanitize(tmp_ctx, proto, &sanitized_proto);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (ret != EOK) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher goto done;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher }
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek subfilter = talloc_asprintf(tmp_ctx, SYSDB_SVC_BYNAME_FILTER,
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek proto ? sanitized_proto : "*",
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek sanitized_name, sanitized_name);
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek if (!subfilter) {
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek ret = ENOMEM;
a86d6cd05e3f823214587475b83d907f394c035eJakub Hrozek goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = sysdb_search_services(mem_ctx, domain, subfilter,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher attrs, &msgs_count, &msgs);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (ret == EOK) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan res = talloc_zero(mem_ctx, struct ldb_result);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (!res) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = ENOMEM;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan goto done;
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan }
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan res->count = msgs_count;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan res->msgs = talloc_steal(res, msgs);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher *_res = res;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozekdone:
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek talloc_free(tmp_ctx);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan return ret;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan}
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallaghererrno_t
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivansysdb_getservbyport(TALLOC_CTX *mem_ctx,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct sss_domain_info *domain,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher int port,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher const char *proto,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct ldb_result **_res)
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher{
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher errno_t ret;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan TALLOC_CTX *tmp_ctx;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher static const char *attrs[] = SYSDB_SVC_ATTRS;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan char *sanitized_proto = NULL;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan char *subfilter;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher struct ldb_result *res = NULL;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher struct ldb_message **msgs;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher size_t msgs_count;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher if (port <= 0) {
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher return EINVAL;
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher tmp_ctx = talloc_new(NULL);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (!tmp_ctx) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan return ENOMEM;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher }
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (proto) {
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher ret = sss_filter_sanitize(tmp_ctx, proto, &sanitized_proto);
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher if (ret != EOK) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan goto done;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher subfilter = talloc_asprintf(tmp_ctx, SYSDB_SVC_BYPORT_FILTER,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher proto ? sanitized_proto : "*",
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan (unsigned int) port);
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher if (!subfilter) {
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher ret = ENOMEM;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan goto done;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = sysdb_search_services(mem_ctx, domain, subfilter,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan attrs, &msgs_count, &msgs);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (ret == EOK) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher res = talloc_zero(mem_ctx, struct ldb_result);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher if (!res) {
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek ret = ENOMEM;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan res->count = msgs_count;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan res->msgs = talloc_steal(res, msgs);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher }
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek *_res = res;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivandone:
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan talloc_free(tmp_ctx);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher return ret;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek}
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivanerrno_t
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivansysdb_store_service(struct sss_domain_info *domain,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher const char *primary_name,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan int port,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan const char **aliases,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan const char **protocols,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct sysdb_attrs *extra_attrs,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher char **remove_attrs,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan uint64_t cache_timeout,
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek time_t now)
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek{
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek errno_t ret;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek errno_t sret;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek TALLOC_CTX *tmp_ctx;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan bool in_transaction = false;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct ldb_result *res = NULL;
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek const char *name;
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek unsigned int i;
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek struct ldb_dn *update_dn = NULL;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct sysdb_attrs *attrs;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct sysdb_ctx *sysdb;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan tmp_ctx = talloc_new(NULL);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (!tmp_ctx) return ENOMEM;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan sysdb = domain->sysdb;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher ret = sysdb_transaction_start(sysdb);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (ret != EOK) {
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan DEBUG(SSSDBG_CRIT_FAILURE, "Failed to start transaction\n");
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan goto done;
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan }
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher in_transaction = true;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher /* Check that the port is unique
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan * If the port appears for any service other than
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan * the one matching the primary_name, we need to
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan * remove them so that getservbyport() can work
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan * properly. Last entry saved to the cache should
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan * always "win".
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan */
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan ret = sysdb_getservbyport(tmp_ctx, domain, port, NULL, &res);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (ret != EOK && ret != ENOENT) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan } else if (ret != ENOENT) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (res->count != 1) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan /* Somehow the cache has multiple entries with
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher * the same port. This is corrupted. We'll delete
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan * them all to sort it out.
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher */
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek for (i = 0; i < res->count; i++) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan DEBUG(SSSDBG_TRACE_FUNC,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher "Corrupt cache entry [%s] detected. Deleting\n",
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher ldb_dn_canonical_string(tmp_ctx,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan res->msgs[i]->dn));
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = sysdb_delete_entry(sysdb, res->msgs[i]->dn, true);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (ret != EOK) {
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher "Could not delete corrupt cache entry [%s]\n",
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ldb_dn_canonical_string(tmp_ctx,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher res->msgs[i]->dn));
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan } else {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan /* Check whether this is the same name as we're currently
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan * saving to the cache.
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan */
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan name = ldb_msg_find_attr_as_string(res->msgs[0],
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan SYSDB_NAME,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher NULL);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (!name || strcmp(name, primary_name) != 0) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek if (!name) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "A service with no name?\n");
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* Corrupted */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek /* Either this is a corrupt entry or it's another service
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * claiming ownership of this port. In order to account
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * for port reassignments, we need to delete the old entry.
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek DEBUG(SSSDBG_TRACE_FUNC,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "Corrupt or replaced cache entry [%s] detected. "
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek "Deleting\n",
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek ldb_dn_canonical_string(tmp_ctx,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek res->msgs[0]->dn));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek ret = sysdb_delete_entry(sysdb, res->msgs[0]->dn, true);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (ret != EOK) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "Could not delete cache entry [%s]\n",
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek ldb_dn_canonical_string(tmp_ctx,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek res->msgs[0]->dn));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek talloc_zfree(res);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* Ok, ports should now be unique. Now look
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * the service up by name to determine if we
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * need to update existing entries or modify
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * aliases.
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_getservbyname(tmp_ctx, domain, primary_name, NULL, &res);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret != EOK && ret != ENOENT) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek goto done;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek } else if (ret != ENOENT) { /* Found entries */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek for (i = 0; i < res->count; i++) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* Check whether this is the same name as we're currently
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * saving to the cache.
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek name = ldb_msg_find_attr_as_string(res->msgs[i],
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek SYSDB_NAME,
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek NULL);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (!name) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek /* Corrupted */
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek "A service with no name?\n");
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek DEBUG(SSSDBG_TRACE_FUNC,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "Corrupt cache entry [%s] detected. Deleting\n",
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ldb_dn_canonical_string(tmp_ctx,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek res->msgs[i]->dn));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_delete_entry(sysdb, res->msgs[i]->dn, true);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret != EOK) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "Could not delete corrupt cache entry [%s]\n",
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek ldb_dn_canonical_string(tmp_ctx,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek res->msgs[i]->dn));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek goto done;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek } else if (strcmp(name, primary_name) == 0) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* This is the same service name, so we need
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * to update this entry with the values
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * provided.
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek */
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if(update_dn) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "Two existing services with the same name: [%s]? "
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek "Deleting both.\n",
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek primary_name);
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* Delete the entry from the previous pass */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek ret = sysdb_delete_entry(sysdb, update_dn, true);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret != EOK) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "Could not delete cache entry [%s]\n",
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek ldb_dn_canonical_string(tmp_ctx,
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek update_dn));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek goto done;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* Delete the new entry as well */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_delete_entry(sysdb, res->msgs[i]->dn, true);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (ret != EOK) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "Could not delete cache entry [%s]\n",
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek ldb_dn_canonical_string(tmp_ctx,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek res->msgs[i]->dn));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek goto done;
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek update_dn = NULL;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek } else {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek update_dn = talloc_steal(tmp_ctx, res->msgs[i]->dn);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek } else {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* Another service is claiming this name as an alias.
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * In order to account for aliases being promoted to
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * primary names, we need to make sure to remove the
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * old alias entry.
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek ret = sysdb_svc_remove_alias(sysdb,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek res->msgs[i]->dn,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek primary_name);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (ret != EOK) goto done;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek talloc_zfree(res);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (update_dn) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* Update the existing entry */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_svc_update(sysdb, update_dn, port, aliases, protocols);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan } else {
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan /* Add a new entry */
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = sysdb_svc_add(tmp_ctx, domain, primary_name, port,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher aliases, protocols, &update_dn);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (ret != EOK) goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan /* Set the cache timeout */
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher if (!extra_attrs) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek attrs = sysdb_new_attrs(tmp_ctx);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (!attrs) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = ENOMEM;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan goto done;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek } else {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan attrs = extra_attrs;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher ret = sysdb_attrs_add_time_t(attrs, SYSDB_LAST_UPDATE, now);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret) goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = sysdb_attrs_add_time_t(attrs, SYSDB_CACHE_EXPIRE,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ((cache_timeout) ?
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan (now + cache_timeout) : 0));
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan if (ret) goto done;
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = sysdb_set_entry_attr(sysdb, update_dn, attrs, SYSDB_MOD_REP);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher if (ret != EOK) goto done;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (remove_attrs) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = sysdb_remove_attrs(domain, primary_name,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan SYSDB_MEMBER_SERVICE,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher remove_attrs);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret != EOK) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan DEBUG(SSSDBG_MINOR_FAILURE,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan "Could not remove missing attributes: [%s]\n",
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan strerror(ret));
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan goto done;
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_transaction_commit(sysdb);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (ret != EOK) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE, "Failed to commit transaction\n");
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher goto done;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek in_transaction = false;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagherdone:
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (in_transaction) {
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek sret = sysdb_transaction_cancel(sysdb);
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek if (sret != EOK) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan DEBUG(SSSDBG_CRIT_FAILURE, "Could not cancel transaction\n");
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan talloc_free(tmp_ctx);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan return ret;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan}
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstruct ldb_dn *
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivansysdb_svc_dn(struct sysdb_ctx *sysdb, TALLOC_CTX *mem_ctx,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan const char *domain, const char *name)
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan{
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan errno_t ret;
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher char *clean_name;
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher struct ldb_dn *dn;
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = sysdb_dn_sanitize(NULL, name, &clean_name);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher if (ret != EOK) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return NULL;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan dn = ldb_dn_new_fmt(mem_ctx, sysdb->ldb, SYSDB_TMPL_SVC,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher clean_name, domain);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek talloc_free(clean_name);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan return dn;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan}
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekerrno_t
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivansysdb_svc_add(TALLOC_CTX *mem_ctx,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct sss_domain_info *domain,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan const char *primary_name,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan int port,
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek const char **aliases,
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek const char **protocols,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct ldb_dn **dn)
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan{
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan errno_t ret;
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan int lret;
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher TALLOC_CTX *tmp_ctx;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct ldb_message *msg;
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek unsigned long i;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher tmp_ctx = talloc_new(NULL);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!tmp_ctx) return ENOMEM;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan msg = ldb_msg_new(tmp_ctx);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (!msg) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher ret = ENOMEM;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher /* svc dn */
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan msg->dn = sysdb_svc_dn(domain->sysdb, msg, domain->name, primary_name);
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek if (!msg->dn) {
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek ret = ENOMEM;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan goto done;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan /* Objectclass */
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_SVC_CLASS);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (ret != EOK) goto done;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan /* Set the primary name */
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek ret = sysdb_add_string(msg, SYSDB_NAME, primary_name);
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek if (ret != EOK) goto done;
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek /* Set the port number */
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = add_ulong(msg, LDB_FLAG_MOD_ADD,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher SYSDB_SVC_PORT, port);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret != EOK) goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan /* If this service has any aliases, include them */
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (aliases && aliases[0]) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Set the name aliases */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek lret = ldb_msg_add_empty(msg, SYSDB_NAME_ALIAS,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan LDB_FLAG_MOD_ADD, NULL);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (lret != LDB_SUCCESS) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = sysdb_error_to_errno(lret);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek goto done;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek for (i=0; aliases[i]; i++) {
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek lret = ldb_msg_add_string(msg, SYSDB_NAME_ALIAS, aliases[i]);
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek if (lret != LDB_SUCCESS) {
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek ret = sysdb_error_to_errno(lret);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek goto done;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Set the protocols */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek lret = ldb_msg_add_empty(msg, SYSDB_SVC_PROTO,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek LDB_FLAG_MOD_ADD, NULL);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (lret != LDB_SUCCESS) {
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek ret = sysdb_error_to_errno(lret);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek goto done;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek for (i=0; protocols[i]; i++) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek lret = ldb_msg_add_string(msg, SYSDB_SVC_PROTO, protocols[i]);
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek if (lret != LDB_SUCCESS) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = sysdb_error_to_errno(lret);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek goto done;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* creation time */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_CREATE_TIME,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek (unsigned long)time(NULL));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (ret) goto done;
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek lret = ldb_add(domain->sysdb->ldb, msg);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = sysdb_error_to_errno(lret);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (ret == EOK && dn) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek *dn = talloc_steal(mem_ctx, msg->dn);
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozekdone:
a9228ebcce14888b3123bdf46e610e0900bcd2ccJakub Hrozek if (ret) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(SSSDBG_TRACE_INTERNAL,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek "Error: %d (%s)\n", ret, strerror(ret));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_free(tmp_ctx);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozekstatic errno_t
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozeksysdb_svc_update(struct sysdb_ctx *sysdb,
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan struct ldb_dn *dn,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher int port,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char **aliases,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher const char **protocols)
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher{
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher errno_t ret;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher struct ldb_message *msg;
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek int lret;
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek unsigned int i;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher if (!dn || !protocols || !protocols[0]) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return EINVAL;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan msg = ldb_msg_new(NULL);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher if (!msg) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = ENOMEM;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan msg->dn = dn;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan /* Update the port */
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan ret = add_ulong(msg, SYSDB_MOD_REP,
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan SYSDB_SVC_PORT, port);
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan if (ret != EOK) goto done;
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (aliases && aliases[0]) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher /* Update the aliases */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek lret = ldb_msg_add_empty(msg, SYSDB_NAME_ALIAS, SYSDB_MOD_REP, NULL);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (lret != LDB_SUCCESS) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan ret = ENOMEM;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan for (i = 0; aliases[i]; i++) {
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan lret = ldb_msg_add_string(msg, SYSDB_NAME_ALIAS, aliases[i]);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (lret != LDB_SUCCESS) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher ret = EINVAL;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan /* Update the protocols */
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan lret = ldb_msg_add_empty(msg, SYSDB_SVC_PROTO, SYSDB_MOD_REP, NULL);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (lret != LDB_SUCCESS) {
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher ret = ENOMEM;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan for (i = 0; protocols[i]; i++) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan lret = ldb_msg_add_string(msg, SYSDB_SVC_PROTO, protocols[i]);
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan if (lret != LDB_SUCCESS) {
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan ret = EINVAL;
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan goto done;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lret = ldb_modify(sysdb->ldb, msg);
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher if (lret != LDB_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "ldb_modify failed: [%s](%d)[%s]\n",
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ldb_strerror(lret), lret, ldb_errstring(sysdb->ldb));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = sysdb_error_to_errno(lret);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherdone:
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher if (ret) {
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher DEBUG(SSSDBG_TRACE_INTERNAL,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "Error: %d (%s)\n", ret, strerror(ret));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek talloc_free(msg);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghererrno_t
b355dcb54194f498921743ca33304eac35d89718Stephen Gallaghersysdb_svc_remove_alias(struct sysdb_ctx *sysdb,
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher struct ldb_dn *dn,
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher const char *alias)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher errno_t ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct ldb_message *msg;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int lret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher msg = ldb_msg_new(NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!msg) {
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher ret = ENOMEM;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek msg->dn = dn;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek ret = sysdb_delete_string(msg, SYSDB_NAME_ALIAS, alias);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (ret != EOK) goto done;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek lret = ldb_modify(sysdb->ldb, msg);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (lret != LDB_SUCCESS) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek "ldb_modify failed: [%s](%d)[%s]\n",
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek ldb_strerror(lret), lret, ldb_errstring(sysdb->ldb));
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek }
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek ret = sysdb_error_to_errno(lret);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozekdone:
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek DEBUG(SSSDBG_TRACE_INTERNAL,
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek "Error: %d (%s)\n", ret, strerror(ret));
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek }
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek talloc_zfree(msg);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return ret;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek}
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozekerrno_t
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozeksysdb_svc_delete(struct sss_domain_info *domain,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char *name,
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek int port,
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek const char *proto)
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek{
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek errno_t ret, sret;
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek TALLOC_CTX *tmp_ctx;
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek struct ldb_result *res;
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek unsigned int i;
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek bool in_transaction = false;
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek struct sysdb_ctx *sysdb = domain->sysdb;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek tmp_ctx = talloc_new(NULL);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek if (!tmp_ctx) {
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek return ENOMEM;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_transaction_start(sysdb);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (ret != EOK) {
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, "Failed to start transaction\n");
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek goto done;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek }
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek in_transaction = true;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek if (name) {
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek ret = sysdb_getservbyname(tmp_ctx, domain, name, proto, &res);
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek if (ret != EOK && ret != ENOENT) goto done;
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek if (ret == ENOENT) {
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek /* Doesn't exist in the DB. Nothing to do */
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek ret = EOK;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek goto done;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek } else {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_getservbyport(tmp_ctx, domain, port, proto, &res);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret != EOK && ret != ENOENT) goto done;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret == ENOENT) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek /* Doesn't exist in the DB. Nothing to do */
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek ret = EOK;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek goto done;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek /* There should only be one matching entry,
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek * but if there are multiple, we should delete
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek * them all to de-corrupt the DB.
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek */
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek for (i = 0; i < res->count; i++) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek ret = sysdb_delete_entry(sysdb, res->msgs[i]->dn, false);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (ret != EOK) goto done;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek ret = sysdb_transaction_commit(sysdb);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (ret != EOK) {
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, "Failed to commit transaction\n");
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek goto done;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek }
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek in_transaction = false;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozekdone:
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (in_transaction) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek sret = sysdb_transaction_cancel(sysdb);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (sret != EOK) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek "Could not cancel transaction\n");
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek if (ret != EOK && ret != ENOENT) {
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek DEBUG(SSSDBG_TRACE_INTERNAL,
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek "Error: %d (%s)\n", ret, strerror(ret));
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek talloc_zfree(tmp_ctx);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek return ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek}
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozekerrno_t
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozeksysdb_enumservent(TALLOC_CTX *mem_ctx,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct sss_domain_info *domain,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct ldb_result **_res)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek{
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek errno_t ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek TALLOC_CTX *tmp_ctx;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek static const char *attrs[] = SYSDB_SVC_ATTRS;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct ldb_result *res = NULL;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct ldb_message **msgs;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek size_t msgs_count;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek tmp_ctx = talloc_new(NULL);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!tmp_ctx) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return ENOMEM;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_search_services(mem_ctx, domain, "",
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek attrs, &msgs_count, &msgs);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret == EOK) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek res = talloc_zero(mem_ctx, struct ldb_result);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!res) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = ENOMEM;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek goto done;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek res->count = msgs_count;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek res->msgs = talloc_steal(res, msgs);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek *_res = res;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekdone:
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek talloc_free(tmp_ctx);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek}
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekerrno_t
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozeksysdb_set_service_attr(struct sss_domain_info *domain,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char *name,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct sysdb_attrs *attrs,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek int mod_op)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek{
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek errno_t ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct ldb_dn *dn;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek TALLOC_CTX *tmp_ctx;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek tmp_ctx = talloc_new(NULL);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!tmp_ctx) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return ENOMEM;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek dn = sysdb_svc_dn(domain->sysdb, tmp_ctx, domain->name, name);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!dn) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = ENOMEM;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek goto done;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_set_entry_attr(domain->sysdb, dn, attrs, mod_op);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekdone:
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek talloc_free(tmp_ctx);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek}
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekerrno_t sysdb_search_services(TALLOC_CTX *mem_ctx,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct sss_domain_info *domain,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char *sub_filter,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char **attrs,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek size_t *msgs_count,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct ldb_message ***msgs)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek{
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek TALLOC_CTX *tmp_ctx;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct ldb_dn *basedn;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek char *filter;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek int ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek tmp_ctx = talloc_new(NULL);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!tmp_ctx) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return ENOMEM;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek basedn = ldb_dn_new_fmt(tmp_ctx, domain->sysdb->ldb,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek SYSDB_TMPL_SVC_BASE, domain->name);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!basedn) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE, "Failed to build base dn\n");
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = ENOMEM;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek goto fail;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek filter = talloc_asprintf(tmp_ctx, "(&(%s)%s)", SYSDB_SC, sub_filter);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!filter) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE, "Failed to build filter\n");
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = ENOMEM;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek goto fail;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek DEBUG(SSSDBG_TRACE_INTERNAL,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "Search services with filter: %s\n", filter);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ret = sysdb_search_entry(mem_ctx, domain->sysdb, basedn,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek LDB_SCOPE_SUBTREE, filter, attrs,
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher msgs_count, msgs);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (ret) {
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan goto fail;
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan talloc_zfree(tmp_ctx);
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan return EOK;
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagherfail:
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan if (ret == ENOENT) {
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan DEBUG(SSSDBG_TRACE_INTERNAL, "No such entry\n");
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan }
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan else if (ret) {
f9fdc87c80f2744780c6a0f2bf5b1b57bcbb095aYuri Chornoivan DEBUG(SSSDBG_MINOR_FAILURE, "Error: %d (%s)\n", ret, strerror(ret));
c938f4ba417328fe62eded0806b2d9ca053f34a5Stephen Gallagher }
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan talloc_zfree(tmp_ctx);
cbe7c54c2caf718bdea7ca6660ba8193d759d2d5Stephen Gallagher return ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek}
bde69429374859acff41273c0771d2b5f5c199b1Yuri Chornoivan