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