sysdb_services.c revision ae765daed93b64b9b4177466a2191561b6734e63
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 int lret;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher TALLOC_CTX *tmp_ctx;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher static const char *attrs[] = SYSDB_SVC_ATTRS;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher struct ldb_dn *base_dn;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce struct ldb_result *res;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce char *sanitized_name;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce char *sanitized_proto;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher *_res = NULL;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher tmp_ctx = talloc_new(NULL);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (!tmp_ctx) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher return ENOMEM;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher base_dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher SYSDB_TMPL_SVC_BASE,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher sysdb->domain->name);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (!base_dn) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = ENOMEM;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sss_filter_sanitize(tmp_ctx, name, &sanitized_name);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret != EOK) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher goto done;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce }
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (proto) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = sss_filter_sanitize(tmp_ctx, proto, &sanitized_proto);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (ret != EOK) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce lret = ldb_search(sysdb->ldb, tmp_ctx, &res, base_dn,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce LDB_SCOPE_SUBTREE, attrs,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce SYSDB_SVC_BYNAME_FILTER,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce proto?sanitized_proto:"*",
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce sanitized_name, sanitized_name);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (lret != LDB_SUCCESS) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = sysdb_error_to_errno(lret);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce goto done;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (res->count == 0) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = ENOENT;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher *_res = talloc_steal(mem_ctx, res);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = EOK;
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,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce struct ldb_result **_res)
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce{
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce errno_t ret;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce int lret;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher TALLOC_CTX *tmp_ctx;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher static const char *attrs[] = SYSDB_SVC_ATTRS;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher struct ldb_dn *base_dn;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher struct ldb_result *res;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher char *sanitized_proto = NULL;
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 base_dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher SYSDB_TMPL_SVC_BASE,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce sysdb->domain->name);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (!base_dn) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = ENOMEM;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce goto done;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (proto) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sss_filter_sanitize(tmp_ctx, proto, &sanitized_proto);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (ret != EOK) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce goto done;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce }
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce }
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce lret = ldb_search(sysdb->ldb, tmp_ctx, &res, base_dn,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce LDB_SCOPE_SUBTREE, attrs,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce SYSDB_SVC_BYPORT_FILTER,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce sanitized_proto?sanitized_proto:"*",
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce (unsigned int) port);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (lret) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_error_to_errno(lret);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (res->count == 0) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = ENOENT;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher *_res = talloc_steal(mem_ctx, res);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = EOK;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagherdone:
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher talloc_free(tmp_ctx);
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher return ret;
e299638926171e0e92a36122aeff6611cd52418dStephen 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;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher struct ldb_result *res = NULL;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek const char *name;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek unsigned int i;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek struct ldb_dn *update_dn = NULL;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek 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) goto done;
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 */
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher ret = sysdb_svc_add(tmp_ctx, sysdb, primary_name, port,
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher aliases, protocols, &update_dn);
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher }
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (ret != EOK) goto done;
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher /* Set the cache timeout */
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (!extra_attrs) {
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher attrs = sysdb_new_attrs(tmp_ctx);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (!attrs) {
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher ret = ENOMEM;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher } else {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher attrs = extra_attrs;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_attrs_add_time_t(attrs, SYSDB_LAST_UPDATE, now);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret) goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_attrs_add_time_t(attrs, SYSDB_CACHE_EXPIRE,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ((cache_timeout) ?
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher (now + cache_timeout) : 0));
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (ret) goto done;
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher ret = sysdb_set_entry_attr(sysdb, update_dn, attrs, SYSDB_MOD_REP);
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (ret != EOK) goto done;
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (remove_attrs) {
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher ret = sysdb_remove_attrs(sysdb, primary_name,
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher SYSDB_MEMBER_SERVICE,
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher remove_attrs);
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (ret != EOK) {
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Could not remove missing attributes: [%s]\n",
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek strerror(ret)));
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek goto done;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek }
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek }
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_transaction_commit(sysdb);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret == EOK) 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 }
d3d297c62e0340151da1d4ce1e082dcfcb45b431Jakub Hrozek
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher for (i = 0; aliases[i]; i++) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_fmt(msg, SYSDB_NAME_ALIAS, "%s", 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 }
d3d297c62e0340151da1d4ce1e082dcfcb45b431Jakub Hrozek
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher for (i = 0; protocols[i]; i++) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_fmt(msg, SYSDB_SVC_PROTO, "%s", 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;
ae765daed93b64b9b4177466a2191561b6734e63Stephen Gallagher struct ldb_result *res;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher unsigned int i;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher bool in_transaction = false;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher tmp_ctx = talloc_new(NULL);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (!tmp_ctx) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher return ENOMEM;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek ret = sysdb_transaction_start(sysdb);
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek if (ret != EOK) goto done;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek
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 in_transaction = false;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidekdone:
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek if (in_transaction) {
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek 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 if (ret != EOK && ret != ENOENT) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher DEBUG(SSSDBG_TRACE_INTERNAL,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Error: %d (%s)\n", ret, strerror(ret)));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher talloc_zfree(tmp_ctx);
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek return ret;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher}
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallaghererrno_t
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallaghersysdb_enumservent(TALLOC_CTX *mem_ctx,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher struct sysdb_ctx *sysdb,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher struct ldb_result **_res)
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher{
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher errno_t ret;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher int lret;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher TALLOC_CTX *tmp_ctx;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher static const char *attrs[] = SYSDB_SVC_ATTRS;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher struct ldb_dn *base_dn;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher struct ldb_result *res;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher *_res = NULL;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce tmp_ctx = talloc_new(NULL);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (!tmp_ctx) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce return ENOMEM;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher }
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher base_dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher SYSDB_TMPL_SVC_BASE,
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher sysdb->domain->name);
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher if (!base_dn) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = ENOMEM;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce goto done;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce }
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce lret = ldb_search(sysdb->ldb, tmp_ctx, &res, base_dn,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce LDB_SCOPE_SUBTREE, attrs,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce SYSDB_SC);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (lret != LDB_SUCCESS) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = sysdb_error_to_errno(lret);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce goto done;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher }
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (res->count == 0) {
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher ret = ENOENT;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher goto done;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher }
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher *_res = talloc_steal(mem_ctx, res);
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek ret = EOK;
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozekdone:
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek talloc_free(tmp_ctx);
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek return ret;
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek}
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek