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 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,
681742138b2afbbefa7f14de937beb438409208eSimo Sorce struct sss_domain_info *domain,
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;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce char *subfilter;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce struct ldb_result *res = NULL;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce struct ldb_message **msgs;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce 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
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce subfilter = talloc_asprintf(tmp_ctx, SYSDB_SVC_BYNAME_FILTER,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce proto ? sanitized_proto : "*",
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce sanitized_name, sanitized_name);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (!subfilter) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = ENOMEM;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_search_services(mem_ctx, domain, subfilter,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce attrs, &msgs_count, &msgs);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (ret == EOK) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce res = talloc_zero(mem_ctx, struct ldb_result);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (!res) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = ENOMEM;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce goto done;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce }
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce res->count = msgs_count;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce res->msgs = talloc_steal(res, msgs);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce *_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,
681742138b2afbbefa7f14de937beb438409208eSimo Sorce struct sss_domain_info *domain,
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;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce char *subfilter;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce struct ldb_result *res = NULL;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce struct ldb_message **msgs;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce 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
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce subfilter = talloc_asprintf(tmp_ctx, SYSDB_SVC_BYPORT_FILTER,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce proto ? sanitized_proto : "*",
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce (unsigned int) port);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (!subfilter) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = ENOMEM;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_search_services(mem_ctx, domain, subfilter,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce attrs, &msgs_count, &msgs);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (ret == EOK) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce res = talloc_zero(mem_ctx, struct ldb_result);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (!res) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = ENOMEM;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce goto done;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce }
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce res->count = msgs_count;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce res->msgs = talloc_steal(res, msgs);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce *_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
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zideksysdb_store_service(struct sss_domain_info *domain,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher const char *primary_name,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher int port,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher const char **aliases,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher const char **protocols,
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher struct sysdb_attrs *extra_attrs,
e299638926171e0e92a36122aeff6611cd52418dStephen 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;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher const char *name;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher unsigned int i;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher struct ldb_dn *update_dn = NULL;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher struct sysdb_attrs *attrs;
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek struct sysdb_ctx *sysdb;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher tmp_ctx = talloc_new(NULL);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (!tmp_ctx) return ENOMEM;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek sysdb = domain->sysdb;
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_transaction_start(sysdb);
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Failed to start transaction\n");
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek goto done;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek }
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 */
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_getservbyport(tmp_ctx, domain, 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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Corrupt cache entry [%s] detected. Deleting\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ldb_dn_canonical_string(tmp_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not delete corrupt cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ldb_dn_canonical_string(tmp_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Corrupt or replaced cache entry [%s] detected. "
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher "Deleting\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ldb_dn_canonical_string(tmp_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not delete cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ldb_dn_canonical_string(tmp_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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 */
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_getservbyname(tmp_ctx, domain, 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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "A service with no name?\n");
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher DEBUG(SSSDBG_TRACE_FUNC,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Corrupt cache entry [%s] detected. Deleting\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ldb_dn_canonical_string(tmp_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not delete corrupt cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ldb_dn_canonical_string(tmp_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Two existing services with the same name: [%s]? "
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher "Deleting both.\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not delete cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ldb_dn_canonical_string(tmp_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not delete cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ldb_dn_canonical_string(tmp_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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 */
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_svc_add(tmp_ctx, domain, 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 */
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (!extra_attrs) {
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher attrs = sysdb_new_attrs(tmp_ctx);
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (!attrs) {
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher ret = ENOMEM;
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher goto done;
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher }
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher } else {
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher attrs = extra_attrs;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
e299638926171e0e92a36122aeff6611cd52418dStephen 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) ?
4c1bf6607060cea867fccf667063c028dfd51e96Stephen 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
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (remove_attrs) {
d115f40c7a3999e3cbe705a2ff9cf0fd493f80fbMichal Zidek ret = sysdb_remove_attrs(domain, primary_name,
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher SYSDB_MEMBER_SERVICE,
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher remove_attrs);
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher if (ret != EOK) {
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not remove missing attributes: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov strerror(ret));
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher goto done;
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher }
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher }
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_transaction_commit(sysdb);
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Failed to commit transaction\n");
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek goto done;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek }
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek in_transaction = false;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagherdone:
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (in_transaction) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher sret = sysdb_transaction_cancel(sysdb);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (sret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov 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,
681742138b2afbbefa7f14de937beb438409208eSimo Sorce struct sss_domain_info *domain,
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 */
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek msg->dn = sysdb_svc_dn(domain->sysdb, msg, domain->name, primary_name);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (!msg->dn) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = ENOMEM;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Objectclass */
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano Fidêncio ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_SVC_CLASS);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret != EOK) goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Set the primary name */
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano Fidêncio ret = sysdb_add_string(msg, SYSDB_NAME, primary_name);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret != EOK) goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Set the port number */
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano Fidêncio ret = sysdb_add_ulong(msg, 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 */
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano Fidêncio ret = sysdb_add_ulong(msg, SYSDB_CREATE_TIME, (unsigned long)time(NULL));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret) goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek lret = ldb_add(domain->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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "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 */
49d84c926b00ba1368372cdec255bceb58d66f43Fabiano Fidêncio ret = sysdb_replace_ulong(msg, 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++) {
d3d297c62e0340151da1d4ce1e082dcfcb45b431Jakub Hrozek 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++) {
d3d297c62e0340151da1d4ce1e082dcfcb45b431Jakub Hrozek 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);
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik if (lret != LDB_SUCCESS) {
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik DEBUG(SSSDBG_MINOR_FAILURE,
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik "ldb_modify failed: [%s](%d)[%s]\n",
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik ldb_strerror(lret), lret, ldb_errstring(sysdb->ldb));
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_error_to_errno(lret);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagherdone:
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher DEBUG(SSSDBG_TRACE_INTERNAL,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "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
a928f7a6bd7681db6e26cba3eb7da22d14288737Fabiano Fidêncio ret = sysdb_delete_string(msg, SYSDB_NAME_ALIAS, alias);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret != EOK) goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_modify(sysdb->ldb, msg);
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik if (lret != LDB_SUCCESS) {
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik DEBUG(SSSDBG_MINOR_FAILURE,
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik "ldb_modify failed: [%s](%d)[%s]\n",
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik ldb_strerror(lret), lret, ldb_errstring(sysdb->ldb));
04d138472cc086fb7961f0d378852b09961b1a33Lukas Slebodnik }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_error_to_errno(lret);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagherdone:
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher DEBUG(SSSDBG_TRACE_INTERNAL,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "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
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zideksysdb_svc_delete(struct sss_domain_info *domain,
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;
ae765daed93b64b9b4177466a2191561b6734e63Stephen Gallagher bool in_transaction = false;
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek struct sysdb_ctx *sysdb = domain->sysdb;
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 = sysdb_transaction_start(sysdb);
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Failed to start transaction\n");
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek goto done;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher in_transaction = true;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (name) {
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_getservbyname(tmp_ctx, domain, 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 {
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_getservbyport(tmp_ctx, domain, 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);
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Failed to commit transaction\n");
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek goto done;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek }
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,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not cancel transaction\n");
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret != EOK && ret != ENOENT) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher DEBUG(SSSDBG_TRACE_INTERNAL,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Error: %d (%s)\n", ret, strerror(ret));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher }
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher talloc_zfree(tmp_ctx);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher return ret;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher}
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallaghererrno_t
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallaghersysdb_enumservent(TALLOC_CTX *mem_ctx,
681742138b2afbbefa7f14de937beb438409208eSimo Sorce struct sss_domain_info *domain,
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;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce struct ldb_result *res = NULL;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce struct ldb_message **msgs;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce 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
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_search_services(mem_ctx, domain, "",
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce attrs, &msgs_count, &msgs);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (ret == EOK) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce res = talloc_zero(mem_ctx, struct ldb_result);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce if (!res) {
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = ENOMEM;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce goto done;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce }
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce res->count = msgs_count;
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce res->msgs = talloc_steal(res, msgs);
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher }
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce *_res = res;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagherdone:
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher talloc_free(tmp_ctx);
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher return ret;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher}
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozekerrno_t
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zideksysdb_set_service_attr(struct sss_domain_info *domain,
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek const char *name,
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek struct sysdb_attrs *attrs,
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek int mod_op)
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek{
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek errno_t ret;
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek struct ldb_dn *dn;
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek TALLOC_CTX *tmp_ctx;
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek tmp_ctx = talloc_new(NULL);
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek if (!tmp_ctx) {
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek return ENOMEM;
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek }
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek dn = sysdb_svc_dn(domain->sysdb, tmp_ctx, domain->name, name);
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek if (!dn) {
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek ret = ENOMEM;
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek goto done;
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek }
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_set_entry_attr(domain->sysdb, dn, attrs, mod_op);
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozekdone:
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek talloc_free(tmp_ctx);
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek return ret;
eb29ae58117ca88868491fe2240e27393c7a9068Jakub Hrozek}
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorceerrno_t sysdb_search_services(TALLOC_CTX *mem_ctx,
681742138b2afbbefa7f14de937beb438409208eSimo Sorce struct sss_domain_info *domain,
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce const char *sub_filter,
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce const char **attrs,
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce size_t *msgs_count,
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce struct ldb_message ***msgs)
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce{
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce TALLOC_CTX *tmp_ctx;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce struct ldb_dn *basedn;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce char *filter;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce int ret;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce tmp_ctx = talloc_new(NULL);
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce if (!tmp_ctx) {
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce return ENOMEM;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce }
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek basedn = ldb_dn_new_fmt(tmp_ctx, domain->sysdb->ldb,
681742138b2afbbefa7f14de937beb438409208eSimo Sorce SYSDB_TMPL_SVC_BASE, domain->name);
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce if (!basedn) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "Failed to build base dn\n");
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce ret = ENOMEM;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce goto fail;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce }
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce filter = talloc_asprintf(tmp_ctx, "(&(%s)%s)", SYSDB_SC, sub_filter);
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce if (!filter) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "Failed to build filter\n");
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce ret = ENOMEM;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce goto fail;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce }
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce DEBUG(SSSDBG_TRACE_INTERNAL,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Search services with filter: %s\n", filter);
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_search_entry(mem_ctx, domain->sysdb, basedn,
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce LDB_SCOPE_SUBTREE, filter, attrs,
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce msgs_count, msgs);
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce if (ret) {
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce goto fail;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce }
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce talloc_zfree(tmp_ctx);
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce return EOK;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorcefail:
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce if (ret == ENOENT) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "No such entry\n");
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce }
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce else if (ret) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_MINOR_FAILURE, "Error: %d (%s)\n", ret, strerror(ret));
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce }
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce talloc_zfree(tmp_ctx);
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce return ret;
d0483eefc41ac295ed4c56e08ad76ca7b5fb3b2cSimo Sorce}