sysdb.c revision 35480afaefafb77b28d35b29039989ab888aafe9
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder/*
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder SSSD
e6d40133bc9f858308654afb1262b8b483ec5922Till Mossakowski
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder System Database
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder Copyright (C) Simo Sorce <ssorce@redhat.com> 2008
3f69b6948966979163bdfe8331c38833d5d90ecdChristian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder This program is free software; you can redistribute it and/or modify
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder it under the terms of the GNU General Public License as published by
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder the Free Software Foundation; either version 3 of the License, or
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder (at your option) any later version.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder This program is distributed in the hope that it will be useful,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder but WITHOUT ANY WARRANTY; without even the implied warranty of
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder GNU General Public License for more details.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder You should have received a copy of the GNU General Public License
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder along with this program. If not, see <http://www.gnu.org/licenses/>.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder*/
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder#include "util/util.h"
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include "db/sysdb_private.h"
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include "confdb/confdb.h"
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder#include <time.h>
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder
fbc7d11880751ef87862b1f4650b16c01c6763f1Klaus Luettich
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstruct ldb_dn *sysdb_custom_subtree_dn(struct sysdb_ctx *ctx, void *memctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *domain,
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder const char *subtree_name)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ldb_dn_new_fmt(memctx, ctx->ldb, SYSDB_TMPL_CUSTOM_SUBTREE,
38c817b94e0a5b1ae94178b1075c187e07bcc5e1Christian Maeder subtree_name, domain);
38c817b94e0a5b1ae94178b1075c187e07bcc5e1Christian Maeder}
38c817b94e0a5b1ae94178b1075c187e07bcc5e1Christian Maederstruct ldb_dn *sysdb_custom_dn(struct sysdb_ctx *ctx, void *memctx,
38c817b94e0a5b1ae94178b1075c187e07bcc5e1Christian Maeder const char *domain, const char *object_name,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *subtree_name)
2353f65833a3da763392f771223250cd50b8d873Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ldb_dn_new_fmt(memctx, ctx->ldb, SYSDB_TMPL_CUSTOM, object_name,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder subtree_name, domain);
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder}
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder
d81905a5b924415c524d702df26204683c82c12eChristian Maederstruct ldb_dn *sysdb_user_dn(struct sysdb_ctx *ctx, void *memctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *domain, const char *name)
d81905a5b924415c524d702df26204683c82c12eChristian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ldb_dn_new_fmt(memctx, ctx->ldb, SYSDB_TMPL_USER, name, domain);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstruct ldb_dn *sysdb_group_dn(struct sysdb_ctx *ctx, void *memctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *domain, const char *name)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ldb_dn_new_fmt(memctx, ctx->ldb, SYSDB_TMPL_GROUP, name, domain);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstruct ldb_dn *sysdb_domain_dn(struct sysdb_ctx *ctx, void *memctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *domain)
083bc1972a66d73749760eab3a90bf4eb9ca7951Christian Maeder{
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder return ldb_dn_new_fmt(memctx, ctx->ldb, SYSDB_DOM_BASE, domain);
083bc1972a66d73749760eab3a90bf4eb9ca7951Christian Maeder}
6352f3c31da3043783a13be6594aacb2147378baRazvan Pascanu
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstruct ldb_context *sysdb_ctx_get_ldb(struct sysdb_ctx *ctx)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ctx->ldb;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstruct sysdb_attrs *sysdb_new_attrs(TALLOC_CTX *memctx)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return talloc_zero(memctx, struct sysdb_attrs);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstatic int sysdb_attrs_get_el_int(struct sysdb_attrs *attrs, const char *name,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder bool alloc, struct ldb_message_element **el)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
a9e804dbec424ec36e34bab955cbe90edac5baa6Christian Maeder struct ldb_message_element *e = NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int i;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder for (i = 0; i < attrs->num; i++) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (strcasecmp(name, attrs->a[i].name) == 0)
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder e = &(attrs->a[i]);
fdac680252d7347858bd67b4c2a2aaa52e623815Christian Maeder }
fdac680252d7347858bd67b4c2a2aaa52e623815Christian Maeder
a9e804dbec424ec36e34bab955cbe90edac5baa6Christian Maeder if (!e && alloc) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder e = talloc_realloc(attrs, attrs->a,
b76d27eba526ecac2a20400fa505ec5c642ae7d2Dominik Luecke struct ldb_message_element, attrs->num+1);
b76d27eba526ecac2a20400fa505ec5c642ae7d2Dominik Luecke if (!e) return ENOMEM;
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich attrs->a = e;
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich e[attrs->num].name = talloc_strdup(e, name);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (!e[attrs->num].name) return ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder e[attrs->num].num_values = 0;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder e[attrs->num].values = NULL;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder e[attrs->num].flags = 0;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder e = &(attrs->a[attrs->num]);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder attrs->num++;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (!e) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder return ENOENT;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder *el = e;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder return EOK;
d81905a5b924415c524d702df26204683c82c12eChristian Maeder}
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maederint sysdb_attrs_get_el(struct sysdb_attrs *attrs, const char *name,
083bc1972a66d73749760eab3a90bf4eb9ca7951Christian Maeder struct ldb_message_element **el)
2353f65833a3da763392f771223250cd50b8d873Christian Maeder{
2353f65833a3da763392f771223250cd50b8d873Christian Maeder return sysdb_attrs_get_el_int(attrs, name, true, el);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maederint sysdb_attrs_get_string(struct sysdb_attrs *attrs, const char *name,
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder const char **string)
ab2f38d9cd1249f6bc9cc5b838dc2fcd76189c0fChristian Maeder{
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder struct ldb_message_element *el;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_attrs_get_el_int(attrs, name, false, &el);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder if (el->num_values != 1) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ERANGE;
d81905a5b924415c524d702df26204683c82c12eChristian Maeder }
d81905a5b924415c524d702df26204683c82c12eChristian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder *string = (const char *)el->values[0].data;
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder return EOK;
083bc1972a66d73749760eab3a90bf4eb9ca7951Christian Maeder}
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maederint sysdb_attrs_add_val(struct sysdb_attrs *attrs,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *name, const struct ldb_val *val)
2353f65833a3da763392f771223250cd50b8d873Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_message_element *el = NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_val *vals;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_attrs_get_el(attrs, name, &el);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder vals = talloc_realloc(attrs->a, el->values,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_val, el->num_values+1);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!vals) return ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder vals[el->num_values] = ldb_val_dup(vals, val);
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder if (vals[el->num_values].data == NULL &&
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder vals[el->num_values].length != 0) {
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder return ENOMEM;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder }
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder el->values = vals;
d81905a5b924415c524d702df26204683c82c12eChristian Maeder el->num_values++;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder return EOK;
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder}
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maederint sysdb_attrs_add_string(struct sysdb_attrs *attrs,
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder const char *name, const char *str)
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder{
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder struct ldb_val v;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder v.data = (uint8_t *)discard_const(str);
5b5f3190cc8d51a7942dda33a1ec45345cca5028Thiemo Wiedemeyer v.length = strlen(str);
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder return sysdb_attrs_add_val(attrs, name, &v);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint sysdb_attrs_add_bool(struct sysdb_attrs *attrs,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *name, bool value)
2353f65833a3da763392f771223250cd50b8d873Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if(value) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return sysdb_attrs_add_string(attrs, name, "TRUE");
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder return sysdb_attrs_add_string(attrs, name, "FALSE");
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
d81905a5b924415c524d702df26204683c82c12eChristian Maederint sysdb_attrs_steal_string(struct sysdb_attrs *attrs,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *name, char *str)
4a2f7efdf67dfcda0946f1b6373f41976ddea7a4Christian Maeder{
083bc1972a66d73749760eab3a90bf4eb9ca7951Christian Maeder struct ldb_message_element *el = NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_val *vals;
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder int ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_attrs_get_el(attrs, name, &el);
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder
6352f3c31da3043783a13be6594aacb2147378baRazvan Pascanu vals = talloc_realloc(attrs->a, el->values,
ab2f38d9cd1249f6bc9cc5b838dc2fcd76189c0fChristian Maeder struct ldb_val, el->num_values+1);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!vals) return ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder el->values = vals;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder /* now steal and assign the string */
2353f65833a3da763392f771223250cd50b8d873Christian Maeder talloc_steal(el->values, str);
ab2f38d9cd1249f6bc9cc5b838dc2fcd76189c0fChristian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder el->values[el->num_values].data = (uint8_t *)str;
083bc1972a66d73749760eab3a90bf4eb9ca7951Christian Maeder el->values[el->num_values].length = strlen(str);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder el->num_values++;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder return EOK;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder}
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maederint sysdb_attrs_add_long(struct sysdb_attrs *attrs,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder const char *name, long value)
d81905a5b924415c524d702df26204683c82c12eChristian Maeder{
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder struct ldb_val v;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder char *str;
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder int ret;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder str = talloc_asprintf(attrs, "%ld", value);
6352f3c31da3043783a13be6594aacb2147378baRazvan Pascanu if (!str) return ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder v.data = (uint8_t *)str;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder v.length = strlen(str);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_attrs_add_val(attrs, name, &v);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder talloc_free(str);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint sysdb_attrs_add_uint32(struct sysdb_attrs *attrs,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *name, uint32_t value)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder unsigned long val = value;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_val v;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder char *str;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder str = talloc_asprintf(attrs, "%lu", val);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!str) return ENOMEM;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
980c2505814d75dc689de1412f4de30b4d96314fRazvan Pascanu v.data = (uint8_t *)str;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder v.length = strlen(str);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_attrs_add_val(attrs, name, &v);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder talloc_free(str);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
4b1833c7d3af466e6bcba24f16304e0a78e8da87Christian Maeder
a9e804dbec424ec36e34bab955cbe90edac5baa6Christian Maederint sysdb_attrs_add_time_t(struct sysdb_attrs *attrs,
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder const char *name, time_t value)
fdac680252d7347858bd67b4c2a2aaa52e623815Christian Maeder{
980c2505814d75dc689de1412f4de30b4d96314fRazvan Pascanu long long val = value;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_val v;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder char *str;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder str = talloc_asprintf(attrs, "%lld", val);
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder if (!str) return ENOMEM;
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder v.data = (uint8_t *)str;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder v.length = strlen(str);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_attrs_add_val(attrs, name, &v);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder talloc_free(str);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint sysdb_attrs_users_from_str_list(struct sysdb_attrs *attrs,
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder const char *attr_name,
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder const char *domain,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char **list)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_message_element *el = NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_val *vals;
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich int i, j, num;
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich char *member;
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich int ret;
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder ret = sysdb_attrs_get_el(attrs, attr_name, &el);
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich if (ret) {
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich return ret;
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich }
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich for (num = 0; list[num]; num++) /* count */ ;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder vals = talloc_realloc(attrs->a, el->values,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_val, el->num_values + num);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!vals) {
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder return ENOMEM;
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder }
fdac680252d7347858bd67b4c2a2aaa52e623815Christian Maeder el->values = vals;
a9e804dbec424ec36e34bab955cbe90edac5baa6Christian Maeder
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder DEBUG(9, ("Adding %d members to existing %d ones\n",
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich num, el->num_values));
b76d27eba526ecac2a20400fa505ec5c642ae7d2Dominik Luecke
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder for (i = 0, j = el->num_values; i < num; i++) {
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder member = sysdb_user_strdn(el->values, domain, list[i]);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!member) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder DEBUG(4, ("Failed to get user dn for [%s]\n", list[i]));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder continue;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder el->values[j].data = (uint8_t *)member;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder el->values[j].length = strlen(member);
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder j++;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
fdac680252d7347858bd67b4c2a2aaa52e623815Christian Maeder DEBUG(7, (" member #%d: [%s]\n", i, member));
a9e804dbec424ec36e34bab955cbe90edac5baa6Christian Maeder }
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maeder el->num_values = j;
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich
b76d27eba526ecac2a20400fa505ec5c642ae7d2Dominik Luecke return EOK;
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maeder}
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maeder
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maederint sysdb_attrs_users_from_ldb_vals(struct sysdb_attrs *attrs,
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maeder const char *attr_name,
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maeder const char *domain,
a9e804dbec424ec36e34bab955cbe90edac5baa6Christian Maeder struct ldb_val *values,
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder int num_values)
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder{
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder struct ldb_message_element *el = NULL;
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maeder struct ldb_val *vals;
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder int i, j;
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder char *member;
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder int ret;
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich
b76d27eba526ecac2a20400fa505ec5c642ae7d2Dominik Luecke ret = sysdb_attrs_get_el(attrs, attr_name, &el);
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder if (ret) {
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder return ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder vals = talloc_realloc(attrs->a, el->values, struct ldb_val,
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder el->num_values + num_values);
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder if (!vals) {
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder return ENOMEM;
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder }
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder el->values = vals;
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder DEBUG(9, ("Adding %d members to existing %d ones\n",
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder num_values, el->num_values));
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder for (i = 0, j = el->num_values; i < num_values; i++) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder member = sysdb_user_strdn(el->values, domain,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder (char *)values[i].data);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!member) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder DEBUG(4, ("Failed to get user dn for [%s]\n",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder (char *)values[i].data));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder el->values[j].data = (uint8_t *)member;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder el->values[j].length = strlen(member);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder j++;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder DEBUG(7, (" member #%d: [%s]\n", i, member));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder el->num_values = j;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EOK;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstatic char *build_dom_dn_str_escape(TALLOC_CTX *memctx, const char *template,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *domain, const char *name)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder char *ret;
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder int l;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
5cfeedad8c9d43f62f8e8b85ab73c0dd4e91d976Christian Maeder l = strcspn(name, ",=\n+<>#;\\\"");
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (name[l] != '\0') {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_val v;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder char *tmp;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder v.data = discard_const_p(uint8_t, name);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder v.length = strlen(name);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder tmp = ldb_dn_escape_value(memctx, v);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!tmp) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = talloc_asprintf(memctx, template, tmp, domain);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder talloc_zfree(tmp);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!ret) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = talloc_asprintf(memctx, template, name, domain);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!ret) {
0b73fd9cab131c1b25b542007c98b5f8717b1d36Klaus Luettich return NULL;
0b73fd9cab131c1b25b542007c98b5f8717b1d36Klaus Luettich }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder
0b73fd9cab131c1b25b542007c98b5f8717b1d36Klaus Luettichchar *sysdb_user_strdn(TALLOC_CTX *memctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *domain, const char *name)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return build_dom_dn_str_escape(memctx, SYSDB_TMPL_USER, domain, name);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
0b73fd9cab131c1b25b542007c98b5f8717b1d36Klaus Luettich
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederchar *sysdb_group_strdn(TALLOC_CTX *memctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *domain, const char *name)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return build_dom_dn_str_escape(memctx, SYSDB_TMPL_GROUP, domain, name);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder/* TODO: make a more complete and precise mapping */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint sysdb_error_to_errno(int ldberr)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder switch (ldberr) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder case LDB_SUCCESS:
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EOK;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder case LDB_ERR_OPERATIONS_ERROR:
2353f65833a3da763392f771223250cd50b8d873Christian Maeder return EIO;
4f1815aad02f2d2a7a34864bbafd563961b72886Christian Maeder case LDB_ERR_NO_SUCH_OBJECT:
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ENOENT;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder case LDB_ERR_BUSY:
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EBUSY;
5b5f3190cc8d51a7942dda33a1ec45345cca5028Thiemo Wiedemeyer case LDB_ERR_ENTRY_ALREADY_EXISTS:
5b5f3190cc8d51a7942dda33a1ec45345cca5028Thiemo Wiedemeyer return EEXIST;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder default:
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EFAULT;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder/* =Transactions========================================================== */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint sysdb_transaction_start(struct sysdb_ctx *ctx)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ldb_transaction_start(ctx->ldb);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != LDB_SUCCESS) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder DEBUG(1, ("Failed to start ldb transaction! (%d)\n", ret));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return sysdb_error_to_errno(ret);
d81905a5b924415c524d702df26204683c82c12eChristian Maeder}
d81905a5b924415c524d702df26204683c82c12eChristian Maeder
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maederint sysdb_transaction_commit(struct sysdb_ctx *ctx)
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ldb_transaction_commit(ctx->ldb);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != LDB_SUCCESS) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder DEBUG(1, ("Failed to commit ldb transaction! (%d)\n", ret));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return sysdb_error_to_errno(ret);
4a2f7efdf67dfcda0946f1b6373f41976ddea7a4Christian Maeder}
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maeder
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maederint sysdb_transaction_cancel(struct sysdb_ctx *ctx)
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder{
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder int ret;
3490b73f69b58ab742417b0867d0e2d4a7778cc0Christian Maeder
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder ret = ldb_transaction_cancel(ctx->ldb);
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder if (ret != LDB_SUCCESS) {
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder DEBUG(1, ("Failed to cancel ldb transaction! (%d)\n", ret));
8a5c05062ef501bf725a86a370a5145a198e81fdKlaus Luettich }
ab2f38d9cd1249f6bc9cc5b838dc2fcd76189c0fChristian Maeder return sysdb_error_to_errno(ret);
974b0baababf2878820de073b8fad8db68bef08aDominik Luecke}
083bc1972a66d73749760eab3a90bf4eb9ca7951Christian Maeder
083bc1972a66d73749760eab3a90bf4eb9ca7951Christian Maeder/* =Initialization======================================================== */
083bc1972a66d73749760eab3a90bf4eb9ca7951Christian Maeder
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maederstatic int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx,
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder struct sss_domain_info *domain,
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder const char *db_path,
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder bool allow_upgrade,
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder struct sysdb_ctx **_ctx);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maederstatic int sysdb_get_db_file(TALLOC_CTX *mem_ctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *provider, const char *name,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder const char *base_path, char **_ldb_file)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder char *ldb_file;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* special case for the local domain */
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (strcasecmp(provider, "local") == 0) {
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder ldb_file = talloc_asprintf(mem_ctx, "%s/"LOCAL_SYSDB_FILE,
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder base_path);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder } else {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ldb_file = talloc_asprintf(mem_ctx, "%s/"CACHE_SYSDB_FILE,
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder base_path, name);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!ldb_file) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder *_ldb_file = ldb_file;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EOK;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder/* serach all groups that have a memberUid attribute.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * change it into a member attribute for a user of same domain.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * remove the memberUid attribute
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * add the new member attribute
9dd71ac51c9a6e72bcb126224f9c64131698b636Christian Maeder * finally stop indexing memberUid
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder * upgrade version to 0.2
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder */
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maederstatic int sysdb_upgrade_01(TALLOC_CTX *mem_ctx,
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder struct ldb_context *ldb,
0ea2cddb8715a770e646895e16b7b8085f49167cChristian Maeder const char **ver)
0ea2cddb8715a770e646895e16b7b8085f49167cChristian Maeder{
7245138e91992b96b153b8ac527e263d9dc8ff5bChristian Maeder struct ldb_message_element *el;
7245138e91992b96b153b8ac527e263d9dc8ff5bChristian Maeder struct ldb_result *res;
7245138e91992b96b153b8ac527e263d9dc8ff5bChristian Maeder struct ldb_dn *basedn;
7245138e91992b96b153b8ac527e263d9dc8ff5bChristian Maeder struct ldb_dn *mem_dn;
7245138e91992b96b153b8ac527e263d9dc8ff5bChristian Maeder struct ldb_message *msg;
0ea2cddb8715a770e646895e16b7b8085f49167cChristian Maeder const struct ldb_val *val;
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder const char *filter = "(&(memberUid=*)(objectclass=group))";
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder const char *attrs[] = { "memberUid", NULL };
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder const char *mdn;
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder char *domain;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret, i, j;
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder basedn = ldb_dn_new(mem_ctx, ldb, "cn=sysdb");
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder if (!basedn) {
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder ret = EIO;
beff4152e9f0fe90885458d1a1733b183a2a8816Christian Maeder goto done;
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder }
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder ret = ldb_search(ldb, mem_ctx, &res,
7245138e91992b96b153b8ac527e263d9dc8ff5bChristian Maeder basedn, LDB_SCOPE_SUBTREE,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder attrs, filter);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (ret != LDB_SUCCESS) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = EIO;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto done;
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder }
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder ret = ldb_transaction_start(ldb);
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder if (ret != LDB_SUCCESS) {
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder ret = EIO;
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder goto done;
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder }
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder for (i = 0; i < res->count; i++) {
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder el = ldb_msg_find_element(res->msgs[i], "memberUid");
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder if (!el) {
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder DEBUG(1, ("memberUid is missing from message [%s], skipping\n",
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder ldb_dn_get_linearized(res->msgs[i]->dn)));
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder continue;
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder }
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder /* create modification message */
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder msg = ldb_msg_new(mem_ctx);
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder if (!msg) {
daec53c285f692c56db0cefe16061b46ba602cf0Christian Maeder ret = ENOMEM;
93bc87ee96c68506945dbad8c704badaa42ecf14Christian Maeder goto done;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder }
7245138e91992b96b153b8ac527e263d9dc8ff5bChristian Maeder msg->dn = res->msgs[i]->dn;
7245138e91992b96b153b8ac527e263d9dc8ff5bChristian Maeder
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder ret = ldb_msg_add_empty(msg, "memberUid", LDB_FLAG_MOD_DELETE, NULL);
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder if (ret != LDB_SUCCESS) {
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder ret = ENOMEM;
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder goto done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
1e3950d5c1f0e041dd7677856e43f07796567d5bChristian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = ldb_msg_add_empty(msg, SYSDB_MEMBER, LDB_FLAG_MOD_ADD, NULL);
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder if (ret != LDB_SUCCESS) {
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder ret = ENOMEM;
7245138e91992b96b153b8ac527e263d9dc8ff5bChristian Maeder goto done;
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* get domain name component value */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder val = ldb_dn_get_component_val(res->msgs[i]->dn, 2);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder domain = talloc_strndup(mem_ctx, (const char *)val->data, val->length);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!domain) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto done;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder }
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder for (j = 0; j < el->num_values; j++) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder mem_dn = ldb_dn_new_fmt(mem_ctx, ldb, SYSDB_TMPL_USER,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder (const char *)el->values[j].data, domain);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!mem_dn) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder mdn = talloc_strdup(msg, ldb_dn_get_linearized(mem_dn));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!mdn) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = ENOMEM;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder goto done;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder }
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder ret = ldb_msg_add_string(msg, SYSDB_MEMBER, mdn);
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder if (ret != LDB_SUCCESS) {
528539f3d544c24afe14e979fe51f03e50aa6e9cChristian Maeder ret = ENOMEM;
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder goto done;
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder }
0ae7a79e865d4a6022d705d160530682b3c1f825Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder talloc_zfree(mem_dn);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* ok now we are ready to modify the entry */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ldb_modify(ldb, msg);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != LDB_SUCCESS) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_error_to_errno(ret);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder talloc_zfree(msg);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* conversion done, upgrade version number */
59a10395caff224b2ec541f94dac5082a506c00fChristian Maeder msg = ldb_msg_new(mem_ctx);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!msg) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = ENOMEM;
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder goto done;
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder }
3bcd9d942601d59dd55a6069d8b2d1c33d7ced0eChristian Maeder msg->dn = ldb_dn_new(mem_ctx, ldb, "cn=sysdb");
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!msg->dn) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto done;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = ldb_msg_add_empty(msg, "version", LDB_FLAG_MOD_REPLACE, NULL);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (ret != LDB_SUCCESS) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ENOMEM;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder goto done;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ldb_msg_add_string(msg, "version", SYSDB_VERSION_0_2);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != LDB_SUCCESS) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ldb_modify(ldb, msg);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != LDB_SUCCESS) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_error_to_errno(ret);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder ret = EOK;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maederdone:
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder if (ret != EOK) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ldb_transaction_cancel(ldb);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder } else {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ldb_transaction_commit(ldb);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != LDB_SUCCESS) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EIO;
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder *ver = SYSDB_VERSION_0_2;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstatic int sysdb_check_upgrade_02(TALLOC_CTX *mem_ctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct sss_domain_info *domains,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *db_path)
2353f65833a3da763392f771223250cd50b8d873Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder TALLOC_CTX *tmp_ctx = NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_context *ldb;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder char *ldb_file;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct sysdb_ctx *ctx;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct sss_domain_info *dom;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_message_element *el;
59a10395caff224b2ec541f94dac5082a506c00fChristian Maeder struct ldb_message *msg;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_result *res;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder struct ldb_dn *verdn;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder const char *version = NULL;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder bool do_02_upgrade = false;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder bool ctx_trans = false;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder int ret;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder tmp_ctx = talloc_new(mem_ctx);
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder if (!tmp_ctx) {
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder return ENOMEM;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_get_db_file(mem_ctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder "local", "UPGRADE",
59a10395caff224b2ec541f94dac5082a506c00fChristian Maeder db_path, &ldb_file);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (ret != EOK) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto exit;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ldb = ldb_init(tmp_ctx, NULL);
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (!ldb) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = EIO;
4b1833c7d3af466e6bcba24f16304e0a78e8da87Christian Maeder goto exit;
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder }
e953bea49e7f0e1a43bccf2a66c5e2a2b50848e0Christian Maeder
12aef5992d3af07dee81a4e02cf4be65a83f28bcChristian Maeder ret = ldb_set_debug(ldb, ldb_debug_messages, NULL);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (ret != LDB_SUCCESS) {
4b1833c7d3af466e6bcba24f16304e0a78e8da87Christian Maeder ret = EIO;
e953bea49e7f0e1a43bccf2a66c5e2a2b50848e0Christian Maeder goto exit;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#ifdef SYSDB_TEST
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ldb_set_modules_dir(ctx->ldb, ABS_BUILD_DIR"/.libs");
2353f65833a3da763392f771223250cd50b8d873Christian Maeder#endif
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder ret = ldb_connect(ldb, ldb_file, 0, NULL);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (ret != LDB_SUCCESS) {
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder ret = EIO;
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder goto exit;
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder }
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder verdn = ldb_dn_new(tmp_ctx, ldb, "cn=sysdb");
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (!verdn) {
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder ret = EIO;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder goto exit;
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder }
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = ldb_search(ldb, tmp_ctx, &res,
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder verdn, LDB_SCOPE_BASE,
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder NULL, NULL);
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder if (ret != LDB_SUCCESS) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = EIO;
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder goto exit;
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder }
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (res->count > 1) {
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder ret = EIO;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder goto exit;
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder }
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (res->count == 1) {
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder el = ldb_msg_find_element(res->msgs[0], "version");
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (el) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (el->num_values != 1) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = EINVAL;
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder goto exit;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder version = talloc_strndup(tmp_ctx,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder (char *)(el->values[0].data),
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder el->values[0].length);
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (!version) {
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder ret = ENOMEM;
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder goto exit;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (strcmp(version, SYSDB_VERSION) == 0) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* all fine, return */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = EOK;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto exit;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder DEBUG(4, ("Upgrading DB from version: %s\n", version));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (strcmp(version, SYSDB_VERSION_0_1) == 0) {
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder /* convert database */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = sysdb_upgrade_01(tmp_ctx, ldb, &version);
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (ret != EOK) goto exit;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (strcmp(version, SYSDB_VERSION_0_2) == 0) {
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder /* need to convert database to split files */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder do_02_upgrade = true;
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder }
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!do_02_upgrade) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* not a v2 upgrade, return and let the normal code take over any
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * further upgrade */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = EOK;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto exit;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* == V2->V3 UPGRADE == */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder DEBUG(0, ("UPGRADING DB TO VERSION %s\n", SYSDB_VERSION_0_3));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* ldb uses posix locks,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * posix is stupid and kills all locks when you close *any* file
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * descriptor associated to the same file.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * Therefore we must close and reopen the ldb file here */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* == Backup and reopen ldb == */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* close */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder talloc_zfree(ldb);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* backup*/
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = backup_file(ldb_file, 0);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != EOK) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto exit;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* reopen */
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder ldb = ldb_init(tmp_ctx, NULL);
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (!ldb) {
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder ret = EIO;
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder goto exit;
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder }
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder ret = ldb_set_debug(ldb, ldb_debug_messages, NULL);
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder if (ret != LDB_SUCCESS) {
c5a4c5f506ea34fa527065b4187127a18c6e2418Christian Maeder ret = EIO;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto exit;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ldb_connect(ldb, ldb_file, 0, NULL);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != LDB_SUCCESS) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = EIO;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto exit;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* open a transaction */
d01f5008234242395b1eac85792fd703acf755d9Christian Maeder ret = ldb_transaction_start(ldb);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != LDB_SUCCESS) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder DEBUG(1, ("Failed to start ldb transaction! (%d)\n", ret));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = EIO;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto exit;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* == Upgrade contents == */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder for (dom = domains; dom; dom = dom->next) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder struct ldb_dn *domain_dn;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_dn *users_dn;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct ldb_dn *groups_dn;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int i;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* skip local */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (strcasecmp(dom->provider, "local") == 0) {
a1c6679d00e15a949730ab640159e0adc5b0e3e7Christian Maeder continue;
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder }
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder /* create new dom db */
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder ret = sysdb_domain_init_internal(tmp_ctx, dom,
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder db_path, false, &ctx);
7968d3a131e5a684ec1ff0c6d88aae638549153dChristian Maeder if (ret != EOK) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder ret = ldb_transaction_start(ctx->ldb);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != LDB_SUCCESS) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder DEBUG(1, ("Failed to start ldb transaction! (%d)\n", ret));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = EIO;
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder goto done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ctx_trans = true;
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder /* search all entries for this domain in local,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * copy them all in the new database,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * then remove them from local */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder domain_dn = ldb_dn_new_fmt(tmp_ctx, ctx->ldb,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder SYSDB_DOM_BASE, ctx->domain->name);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!domain_dn) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
ret = ldb_search(ldb, tmp_ctx, &res,
domain_dn, LDB_SCOPE_SUBTREE,
NULL, NULL);
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
users_dn = ldb_dn_new_fmt(tmp_ctx, ctx->ldb,
SYSDB_TMPL_USER_BASE, ctx->domain->name);
if (!users_dn) {
ret = ENOMEM;
goto done;
}
groups_dn = ldb_dn_new_fmt(tmp_ctx, ctx->ldb,
SYSDB_TMPL_GROUP_BASE, ctx->domain->name);
if (!groups_dn) {
ret = ENOMEM;
goto done;
}
for (i = 0; i < res->count; i++) {
struct ldb_dn *orig_dn;
msg = res->msgs[i];
/* skip pre-created congtainers */
if ((ldb_dn_compare(msg->dn, domain_dn) == 0) ||
(ldb_dn_compare(msg->dn, users_dn) == 0) ||
(ldb_dn_compare(msg->dn, groups_dn) == 0)) {
continue;
}
/* regenerate the DN against the new ldb as it may have different
* casefolding rules (example: name changing from case insensitive
* to case sensitive) */
orig_dn = msg->dn;
msg->dn = ldb_dn_new(msg, ctx->ldb,
ldb_dn_get_linearized(orig_dn));
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_add(ctx->ldb, msg);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("WARNING: Could not add entry %s,"
" to new ldb file! (%d [%s])\n",
ldb_dn_get_linearized(msg->dn),
ret, ldb_errstring(ctx->ldb)));
}
ret = ldb_delete(ldb, orig_dn);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("WARNING: Could not remove entry %s,"
" from old ldb file! (%d [%s])\n",
ldb_dn_get_linearized(orig_dn),
ret, ldb_errstring(ldb)));
}
}
/* now remove the basic containers from local */
/* these were optional so debug at level 9 in case
* of failure just for tracing */
ret = ldb_delete(ldb, groups_dn);
if (ret != LDB_SUCCESS) {
DEBUG(9, ("WARNING: Could not remove entry %s,"
" from old ldb file! (%d [%s])\n",
ldb_dn_get_linearized(groups_dn),
ret, ldb_errstring(ldb)));
}
ret = ldb_delete(ldb, users_dn);
if (ret != LDB_SUCCESS) {
DEBUG(9, ("WARNING: Could not remove entry %s,"
" from old ldb file! (%d [%s])\n",
ldb_dn_get_linearized(users_dn),
ret, ldb_errstring(ldb)));
}
ret = ldb_delete(ldb, domain_dn);
if (ret != LDB_SUCCESS) {
DEBUG(9, ("WARNING: Could not remove entry %s,"
" from old ldb file! (%d [%s])\n",
ldb_dn_get_linearized(domain_dn),
ret, ldb_errstring(ldb)));
}
ret = ldb_transaction_commit(ctx->ldb);
if (ret != LDB_SUCCESS) {
DEBUG(1, ("Failed to commit ldb transaction! (%d)\n", ret));
ret = EIO;
goto done;
}
ctx_trans = false;
talloc_zfree(domain_dn);
talloc_zfree(groups_dn);
talloc_zfree(users_dn);
talloc_zfree(res);
}
/* conversion done, upgrade version number */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, ldb, "cn=sysdb");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_empty(msg, "version", LDB_FLAG_MOD_REPLACE, NULL);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, "version", SYSDB_VERSION_0_3);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
ret = ldb_transaction_commit(ldb);
if (ret != LDB_SUCCESS) {
DEBUG(1, ("Failed to commit ldb transaction! (%d)\n", ret));
ret = EIO;
goto exit;
}
ret = EOK;
done:
if (ret != EOK) {
if (ctx_trans) {
ret = ldb_transaction_cancel(ctx->ldb);
if (ret != LDB_SUCCESS) {
DEBUG(1, ("Failed to cancel ldb transaction! (%d)\n", ret));
}
}
ret = ldb_transaction_cancel(ldb);
if (ret != LDB_SUCCESS) {
DEBUG(1, ("Failed to cancel ldb transaction! (%d)\n", ret));
}
}
exit:
talloc_free(tmp_ctx);
return ret;
}
static int sysdb_upgrade_03(struct sysdb_ctx *ctx, const char **ver)
{
TALLOC_CTX *tmp_ctx;
int ret;
struct ldb_message *msg;
tmp_ctx = talloc_new(ctx);
if (!tmp_ctx) {
return ENOMEM;
}
DEBUG(0, ("UPGRADING DB TO VERSION %s\n", SYSDB_VERSION_0_4));
ret = ldb_transaction_start(ctx->ldb);
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
/* Make this database case-sensitive */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, ctx->ldb, "@ATTRIBUTES");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_empty(msg, "name", LDB_FLAG_MOD_DELETE, NULL);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(ctx->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* conversion done, upgrade version number */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, ctx->ldb, "cn=sysdb");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_empty(msg, "version", LDB_FLAG_MOD_REPLACE, NULL);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, "version", SYSDB_VERSION_0_4);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(ctx->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
ret = EOK;
done:
talloc_zfree(tmp_ctx);
if (ret != EOK) {
ret = ldb_transaction_cancel(ctx->ldb);
} else {
ret = ldb_transaction_commit(ctx->ldb);
*ver = SYSDB_VERSION_0_4;
}
if (ret != LDB_SUCCESS) {
ret = EIO;
}
return ret;
}
static int sysdb_upgrade_04(struct sysdb_ctx *ctx, const char **ver)
{
TALLOC_CTX *tmp_ctx;
int ret;
struct ldb_message *msg;
tmp_ctx = talloc_new(ctx);
if (!tmp_ctx) {
return ENOMEM;
}
DEBUG(0, ("UPGRADING DB TO VERSION %s\n", SYSDB_VERSION_0_5));
ret = ldb_transaction_start(ctx->ldb);
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
/* Add new index */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, ctx->ldb, "@INDEXLIST");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_empty(msg, "@IDXATTR", LDB_FLAG_MOD_ADD, NULL);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, "@IDXATTR", "originalDN");
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(ctx->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* Rebuild memberuid and memberoif attributes */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, ctx->ldb, "@MEMBEROF-REBUILD");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_add(ctx->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* conversion done, upgrade version number */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, ctx->ldb, "cn=sysdb");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_empty(msg, "version", LDB_FLAG_MOD_REPLACE, NULL);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, "version", SYSDB_VERSION_0_5);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(ctx->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
ret = EOK;
done:
talloc_zfree(tmp_ctx);
if (ret != EOK) {
ret = ldb_transaction_cancel(ctx->ldb);
} else {
ret = ldb_transaction_commit(ctx->ldb);
*ver = SYSDB_VERSION_0_5;
}
if (ret != LDB_SUCCESS) {
ret = EIO;
}
return ret;
}
static int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domain,
const char *db_path,
bool allow_upgrade,
struct sysdb_ctx **_ctx)
{
TALLOC_CTX *tmp_ctx = NULL;
struct sysdb_ctx *ctx;
const char *base_ldif;
struct ldb_ldif *ldif;
struct ldb_message *msg;
struct ldb_message_element *el;
struct ldb_result *res;
struct ldb_dn *verdn;
const char *version = NULL;
int ret;
ctx = talloc_zero(mem_ctx, struct sysdb_ctx);
if (!ctx) {
return ENOMEM;
}
ctx->domain = domain;
/* The local provider s the only true MPG,
* for the other domains, the provider actually unrolls MPGs */
if (strcasecmp(domain->provider, "local") == 0) {
ctx->mpg = true;
}
ret = sysdb_get_db_file(ctx, domain->provider,
domain->name, db_path,
&ctx->ldb_file);
if (ret != EOK) {
return ret;
}
DEBUG(5, ("DB File for %s: %s\n", domain->name, ctx->ldb_file));
ctx->ldb = ldb_init(ctx, NULL);
if (!ctx->ldb) {
return EIO;
}
ret = ldb_set_debug(ctx->ldb, ldb_debug_messages, NULL);
if (ret != LDB_SUCCESS) {
return EIO;
}
#ifdef SYSDB_TEST
ldb_set_modules_dir(ctx->ldb, ABS_BUILD_DIR"/.libs");
#endif
ret = ldb_connect(ctx->ldb, ctx->ldb_file, 0, NULL);
if (ret != LDB_SUCCESS) {
return EIO;
}
tmp_ctx = talloc_new(ctx);
if (!tmp_ctx) {
return ENOMEM;
}
verdn = ldb_dn_new(tmp_ctx, ctx->ldb, "cn=sysdb");
if (!verdn) {
ret = EIO;
goto done;
}
ret = ldb_search(ctx->ldb, tmp_ctx, &res,
verdn, LDB_SCOPE_BASE,
NULL, NULL);
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
if (res->count > 1) {
ret = EIO;
goto done;
}
if (res->count == 1) {
el = ldb_msg_find_element(res->msgs[0], "version");
if (el) {
if (el->num_values != 1) {
ret = EINVAL;
goto done;
}
version = talloc_strndup(tmp_ctx,
(char *)(el->values[0].data),
el->values[0].length);
if (!version) {
ret = ENOMEM;
goto done;
}
if (strcmp(version, SYSDB_VERSION) == 0) {
/* all fine, return */
ret = EOK;
goto done;
}
if (!allow_upgrade) {
DEBUG(0, ("Wrong DB version (got %s expected %s)\n",
version, SYSDB_VERSION));
ret = EINVAL;
goto done;
}
DEBUG(4, ("Upgrading DB [%s] from version: %s\n",
domain->name, version));
if (strcmp(version, SYSDB_VERSION_0_3) == 0) {
ret = sysdb_upgrade_03(ctx, &version);
if (ret != EOK) {
goto done;
}
}
if (strcmp(version, SYSDB_VERSION_0_4) == 0) {
ret = sysdb_upgrade_04(ctx, &version);
goto done;
}
}
DEBUG(0,("Unknown DB version [%s], expected [%s] for domain %s!\n",
version?version:"not found", SYSDB_VERSION, domain->name));
ret = EINVAL;
goto done;
}
/* cn=sysdb does not exists, means db is empty, populate */
base_ldif = SYSDB_BASE_LDIF;
while ((ldif = ldb_ldif_read_string(ctx->ldb, &base_ldif))) {
ret = ldb_add(ctx->ldb, ldif->msg);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("Failed to initialize DB (%d, [%s]) for domain %s!\n",
ret, ldb_errstring(ctx->ldb), domain->name));
ret = EIO;
goto done;
}
ldb_ldif_read_free(ctx->ldb, ldif);
}
/* == create base domain object == */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new_fmt(msg, ctx->ldb, SYSDB_DOM_BASE, domain->name);
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_fmt(msg, "cn", "%s", domain->name);
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
/* do a synchronous add */
ret = ldb_add(ctx->ldb, msg);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("Failed to initialize DB (%d, [%s]) for domain %s!\n",
ret, ldb_errstring(ctx->ldb), domain->name));
ret = EIO;
goto done;
}
talloc_zfree(msg);
/* == create Users tree == */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new_fmt(msg, ctx->ldb,
SYSDB_TMPL_USER_BASE, domain->name);
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_fmt(msg, "cn", "Users");
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
/* do a synchronous add */
ret = ldb_add(ctx->ldb, msg);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("Failed to initialize DB (%d, [%s]) for domain %s!\n",
ret, ldb_errstring(ctx->ldb), domain->name));
ret = EIO;
goto done;
}
talloc_zfree(msg);
/* == create Groups tree == */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new_fmt(msg, ctx->ldb,
SYSDB_TMPL_GROUP_BASE, domain->name);
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_fmt(msg, "cn", "Groups");
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
/* do a synchronous add */
ret = ldb_add(ctx->ldb, msg);
if (ret != LDB_SUCCESS) {
DEBUG(0, ("Failed to initialize DB (%d, [%s]) for domain %s!\n",
ret, ldb_errstring(ctx->ldb), domain->name));
ret = EIO;
goto done;
}
talloc_zfree(msg);
ret = EOK;
done:
if (ret == EOK) {
*_ctx = ctx;
}
talloc_free(tmp_ctx);
return ret;
}
int sysdb_init(TALLOC_CTX *mem_ctx,
struct confdb_ctx *cdb,
const char *alt_db_path,
bool allow_upgrade,
struct sysdb_ctx_list **_ctx_list)
{
struct sysdb_ctx_list *ctx_list;
struct sss_domain_info *domains, *dom;
struct sysdb_ctx *ctx;
int ret;
ctx_list = talloc_zero(mem_ctx, struct sysdb_ctx_list);
if (!ctx_list) {
return ENOMEM;
}
if (alt_db_path) {
ctx_list->db_path = talloc_strdup(ctx_list, alt_db_path);
} else {
ctx_list->db_path = talloc_strdup(ctx_list, DB_PATH);
}
if (!ctx_list->db_path) {
talloc_zfree(ctx_list);
return ENOMEM;
}
/* open a db for each backend */
ret = confdb_get_domains(cdb, &domains);
if (ret != EOK) {
talloc_zfree(ctx_list);
return ret;
}
if (allow_upgrade) {
/* check if we have an old sssd.ldb to upgrade */
ret = sysdb_check_upgrade_02(ctx_list, domains,
ctx_list->db_path);
if (ret != EOK) {
talloc_zfree(ctx_list);
return ret;
}
}
for (dom = domains; dom; dom = dom->next) {
ctx_list->dbs = talloc_realloc(ctx_list, ctx_list->dbs,
struct sysdb_ctx *,
ctx_list->num_dbs + 1);
if (!ctx_list->dbs) {
talloc_zfree(ctx_list);
return ENOMEM;
}
ret = sysdb_domain_init_internal(ctx_list, dom,
ctx_list->db_path,
allow_upgrade, &ctx);
if (ret != EOK) {
talloc_zfree(ctx_list);
return ret;
}
ctx_list->dbs[ctx_list->num_dbs] = ctx;
ctx_list->num_dbs++;
}
if (ctx_list->num_dbs == 0) {
/* what? .. */
talloc_zfree(ctx_list);
return ENOENT;
}
*_ctx_list = ctx_list;
return EOK;
}
int sysdb_domain_init(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domain,
const char *db_path,
struct sysdb_ctx **_ctx)
{
return sysdb_domain_init_internal(mem_ctx, domain,
db_path, false, _ctx);
}
int sysdb_get_ctx_from_list(struct sysdb_ctx_list *ctx_list,
struct sss_domain_info *domain,
struct sysdb_ctx **ctx)
{
int i;
for (i = 0; i < ctx_list->num_dbs; i++) {
if (ctx_list->dbs[i]->domain == domain) {
*ctx = ctx_list->dbs[i];
return EOK;
}
if (strcasecmp(ctx_list->dbs[i]->domain->name, domain->name) == 0) {
*ctx = ctx_list->dbs[i];
return EOK;
}
}
/* definitely not found */
return ENOENT;
}
int compare_ldb_dn_comp_num(const void *m1, const void *m2)
{
struct ldb_message *msg1 = talloc_get_type(*(void **) discard_const(m1),
struct ldb_message);
struct ldb_message *msg2 = talloc_get_type(*(void **) discard_const(m2),
struct ldb_message);
return ldb_dn_get_comp_num(msg2->dn) - ldb_dn_get_comp_num(msg1->dn);
}
int sysdb_attrs_replace_name(struct sysdb_attrs *attrs, const char *oldname,
const char *newname)
{
struct ldb_message_element *e = NULL;
int i;
const char *dummy;
if (attrs == NULL || oldname == NULL || newname == NULL) return EINVAL;
for (i = 0; i < attrs->num; i++) {
if (strcasecmp(oldname, attrs->a[i].name) == 0) {
e = &(attrs->a[i]);
}
if (strcasecmp(newname, attrs->a[i].name) == 0) {
DEBUG(3, ("New attribute name [%s] already exists.\n", newname));
return EEXIST;
}
}
if (e != NULL) {
dummy = talloc_strdup(attrs, newname);
if (dummy == NULL) {
DEBUG(1, ("talloc_strdup failed.\n"));
return ENOMEM;
}
talloc_free(discard_const(e->name));
e->name = dummy;
}
return EOK;
}