sysdb_upgrade.c revision e7d1cd14ce1a427007b1a7e9789dcb7e9619f84f
842ae4bd224140319ae7feec1872b93dfd491143fielding/*
842ae4bd224140319ae7feec1872b93dfd491143fielding SSSD
842ae4bd224140319ae7feec1872b93dfd491143fielding
842ae4bd224140319ae7feec1872b93dfd491143fielding Authors:
842ae4bd224140319ae7feec1872b93dfd491143fielding Simo Sorce <ssorce@redhat.com>
842ae4bd224140319ae7feec1872b93dfd491143fielding Stephen Gallagher <sgallagh@redhat.com>
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd Copyright (C) 2008-2011 Simo Sorce <ssorce@redhat.com>
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding Copyright (C) 2008-2011 Stephen Gallagher
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd This program is free software; you can redistribute it and/or modify
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd it under the terms of the GNU General Public License as published by
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd the Free Software Foundation; either version 3 of the License, or
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd (at your option) any later version.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding This program is distributed in the hope that it will be useful,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding but WITHOUT ANY WARRANTY; without even the implied warranty of
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e8f95a682820a599fe41b22977010636be5c2717jim GNU General Public License for more details.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding You should have received a copy of the GNU General Public License
e8f95a682820a599fe41b22977010636be5c2717jim along with this program. If not, see <http://www.gnu.org/licenses/>.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding*/
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
b6055b7832a0e4d0818416252fff5925aaebae4brbb#include "util/util.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "db/sysdb_private.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "db/sysdb_autofs.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gsteinstruct upgrade_ctx {
1b21d7b3d97def358b2e923655edeb16613a1c31gstein struct ldb_context *ldb;
2d71630471d1c23f0137309e3c3957c633ecbfd6rbb const char *new_version;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding};
449efc4dc68e42cc4421d15498a689618aab5dc3coar
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic errno_t commence_upgrade(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding const char *new_ver, struct upgrade_ctx **_ctx)
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin struct upgrade_ctx *ctx;
1b21d7b3d97def358b2e923655edeb16613a1c31gstein int ret;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding DEBUG(SSSDBG_CRIT_FAILURE, ("UPGRADING DB TO VERSION %s\n", new_ver));
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben ctx = talloc(mem_ctx, struct upgrade_ctx);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!ctx) {
ef5650b61a8e35f3cc93ec07e73efc17ea329894jorton return ENOMEM;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ctx->ldb = ldb;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ctx->new_version = new_ver;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ret = ldb_transaction_start(ldb);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret != LDB_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = EIO;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
61c688e070e72a669cb2426f85960b9e9a1f9a03minfrin }
61c688e070e72a669cb2426f85960b9e9a1f9a03minfrin
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ret = EOK;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrindone:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != EOK) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin talloc_free(ctx);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding } else {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *_ctx = ctx;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return ret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic errno_t update_version(struct upgrade_ctx *ctx)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding struct ldb_message *msg = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding errno_t ret;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding msg = ldb_msg_new(ctx);
066877f1a045103acfdd376d48cdd473c33f409bdougm if (!msg) {
066877f1a045103acfdd376d48cdd473c33f409bdougm ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding msg->dn = ldb_dn_new(msg, ctx->ldb, SYSDB_BASE);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (!msg->dn) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
066877f1a045103acfdd376d48cdd473c33f409bdougm
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ldb_msg_add_empty(msg, "version", LDB_FLAG_MOD_REPLACE, NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret != LDB_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ENOMEM;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ret = ldb_msg_add_string(msg, "version", ctx->new_version);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (ret != LDB_SUCCESS) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
066877f1a045103acfdd376d48cdd473c33f409bdougm }
066877f1a045103acfdd376d48cdd473c33f409bdougm
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ldb_modify(ctx->ldb, msg);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret != LDB_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = sysdb_error_to_errno(ret);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ret = EOK;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantzdone:
291eb44b3adaf8247425286615b4f4b69fbea274minfrin talloc_free(msg);
066877f1a045103acfdd376d48cdd473c33f409bdougm return ret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrinstatic int finish_upgrade(int ret, struct upgrade_ctx **ctx, const char **ver)
291eb44b3adaf8247425286615b4f4b69fbea274minfrin{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin int lret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret == EOK) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin lret = ldb_transaction_commit((*ctx)->ldb);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = sysdb_error_to_errno(lret);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret == EOK) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *ver = (*ctx)->new_version;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
4d7e28c869788fb00bffda29a67f1b10e19f159dnd }
4d7e28c869788fb00bffda29a67f1b10e19f159dnd
4d7e28c869788fb00bffda29a67f1b10e19f159dnd if (ret != EOK) {
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben lret = ldb_transaction_cancel((*ctx)->ldb);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (lret != LDB_SUCCESS) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz DEBUG(SSSDBG_CRIT_FAILURE,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ("Could not cancel transaction! [%s]\n",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ldb_strerror(lret)));
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein /* Do not overwrite ret here, we want to return
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein * the original failure, not the failure of the
066877f1a045103acfdd376d48cdd473c33f409bdougm * transaction cancellation.
4d7e28c869788fb00bffda29a67f1b10e19f159dnd */
4d7e28c869788fb00bffda29a67f1b10e19f159dnd }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
18413e519b6e5d05ff00d18efd784307442ca2f3igalic
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding talloc_zfree(*ctx);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return ret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin}
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin/* serach all groups that have a memberUid attribute.
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * change it into a member attribute for a user of same domain.
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * remove the memberUid attribute
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * add the new member attribute
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf * finally stop indexing memberUid
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz * upgrade version to 0.2
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz */
36d38d22e0d385db01f5773a579f44b8f02e4b1fsfint sysdb_upgrade_01(struct ldb_context *ldb, const char **ver)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe struct ldb_message_element *el;
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe struct ldb_result *res;
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe struct ldb_dn *basedn;
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe struct ldb_dn *mem_dn;
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe struct ldb_message *msg;
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe const struct ldb_val *val;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf const char *filter = "(&(memberUid=*)(objectclass=group))";
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe const char *attrs[] = { "memberUid", NULL };
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf const char *mdn;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *domain;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int ret, i, j;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd TALLOC_CTX *tmp_ctx;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd struct upgrade_ctx *ctx;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd
4d7e28c869788fb00bffda29a67f1b10e19f159dnd tmp_ctx = talloc_new(NULL);
4d7e28c869788fb00bffda29a67f1b10e19f159dnd if (!tmp_ctx) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf return ENOMEM;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd }
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf ret = commence_upgrade(tmp_ctx, ldb, SYSDB_VERSION_0_2, &ctx);
185aa71728867671e105178b4c66fbc22b65ae26sf if (ret) {
1f0ca94141196628ecadf1a91f2b60a33349872fnd talloc_free(tmp_ctx);
1f0ca94141196628ecadf1a91f2b60a33349872fnd return ret;
1f0ca94141196628ecadf1a91f2b60a33349872fnd }
1f0ca94141196628ecadf1a91f2b60a33349872fnd
1f0ca94141196628ecadf1a91f2b60a33349872fnd basedn = ldb_dn_new(tmp_ctx, ldb, SYSDB_BASE);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (!basedn) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf ret = EIO;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd goto done;
4d7e28c869788fb00bffda29a67f1b10e19f159dnd }
4d7e28c869788fb00bffda29a67f1b10e19f159dnd
4d7e28c869788fb00bffda29a67f1b10e19f159dnd ret = ldb_search(ldb, tmp_ctx, &res,
4d7e28c869788fb00bffda29a67f1b10e19f159dnd basedn, LDB_SCOPE_SUBTREE,
4d7e28c869788fb00bffda29a67f1b10e19f159dnd attrs, "%s", filter);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret != LDB_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = EIO;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding for (i = 0; i < res->count; i++) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin el = ldb_msg_find_element(res->msgs[i], "memberUid");
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!el) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin DEBUG(1, ("memberUid is missing from message [%s], skipping\n",
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ldb_dn_get_linearized(res->msgs[i]->dn)));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin continue;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* create modification message */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin msg = ldb_msg_new(tmp_ctx);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!msg) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ENOMEM;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto done;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin msg->dn = res->msgs[i]->dn;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_msg_add_empty(msg, "memberUid", LDB_FLAG_MOD_DELETE, NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ENOMEM;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto done;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_msg_add_empty(msg, SYSDB_MEMBER, LDB_FLAG_MOD_ADD, NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ENOMEM;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto done;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* get domain name component value */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin val = ldb_dn_get_component_val(res->msgs[i]->dn, 2);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin domain = talloc_strndup(tmp_ctx, (const char *)val->data, val->length);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!domain) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ENOMEM;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf for (j = 0; j < el->num_values; j++) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf mem_dn = ldb_dn_new_fmt(tmp_ctx, ldb, SYSDB_TMPL_USER,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (const char *)el->values[j].data, domain);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (!mem_dn) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
e8f95a682820a599fe41b22977010636be5c2717jim mdn = talloc_strdup(msg, ldb_dn_get_linearized(mem_dn));
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick if (!mdn) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein ret = ldb_msg_add_string(msg, SYSDB_MEMBER, mdn);
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein if (ret != LDB_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ENOMEM;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim goto done;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf }
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben talloc_zfree(mem_dn);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim /* ok now we are ready to modify the entry */
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim ret = ldb_modify(ldb, msg);
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim if (ret != LDB_SUCCESS) {
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim ret = sysdb_error_to_errno(ret);
afadb90f859c4987505e745edee656850d7f8d49jim goto done;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim }
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim talloc_zfree(msg);
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim }
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim /* conversion done, update version number */
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim ret = update_version(ctx);
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim
172e83c0f024fe6396dd1f3ca3492fd83c304db5jimdone:
66a73d4405f9d941672c0343b36f6c494413a6b5rpluem ret = finish_upgrade(ret, &ctx, ver);
66a73d4405f9d941672c0343b36f6c494413a6b5rpluem talloc_free(tmp_ctx);
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim return ret;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim}
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjimint sysdb_check_upgrade_02(struct sss_domain_info *domains,
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim const char *db_path)
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim{
291eb44b3adaf8247425286615b4f4b69fbea274minfrin TALLOC_CTX *tmp_ctx = NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin struct ldb_context *ldb;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin char *ldb_file;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin struct sysdb_ctx *sysdb;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin struct sss_domain_info *dom;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin struct ldb_message_element *el;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin struct ldb_message *msg;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin struct ldb_result *res;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin struct ldb_dn *verdn;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin const char *version = NULL;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin bool do_02_upgrade = false;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin bool ctx_trans = false;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin int ret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin tmp_ctx = talloc_new(NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!tmp_ctx) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return ENOMEM;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = sysdb_get_db_file(tmp_ctx,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin "local", "UPGRADE",
291eb44b3adaf8247425286615b4f4b69fbea274minfrin db_path, &ldb_file);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != EOK) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto exit;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = sysdb_ldb_connect(tmp_ctx, ldb_file, &ldb);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != EOK) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin DEBUG(1, ("sysdb_ldb_connect failed.\n"));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return ret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin verdn = ldb_dn_new(tmp_ctx, ldb, SYSDB_BASE);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!verdn) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = EIO;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto exit;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_search(ldb, tmp_ctx, &res,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin verdn, LDB_SCOPE_BASE,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin NULL, NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = EIO;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto exit;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (res->count > 1) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = EIO;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto exit;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (res->count == 1) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin el = ldb_msg_find_element(res->msgs[0], "version");
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (el) {
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim if (el->num_values != 1) {
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim ret = EINVAL;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim goto exit;
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim }
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim version = talloc_strndup(tmp_ctx,
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim (char *)(el->values[0].data),
f23a64b4e8a5f213b2aafb2bf6775e883e21f05fjim el->values[0].length);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (!version) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto exit;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (strcmp(version, SYSDB_VERSION) == 0) {
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf /* all fine, return */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = EOK;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto exit;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz DEBUG(4, ("Upgrading DB from version: %s\n", version));
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq if (strcmp(version, SYSDB_VERSION_0_1) == 0) {
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq /* convert database */
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq ret = sysdb_upgrade_01(ldb, &version);
c7ab5a433d38d5eae5fc0bb76be80ffab6e4f71dniq if (ret != EOK) goto exit;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (strcmp(version, SYSDB_VERSION_0_2) == 0) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* need to convert database to split files */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz do_02_upgrade = true;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (!do_02_upgrade) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* not a v2 upgrade, return and let the normal code take over any
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf * further upgrade */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = EOK;
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf goto exit;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* == V2->V3 UPGRADE == */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben DEBUG(0, ("UPGRADING DB TO VERSION %s\n", SYSDB_VERSION_0_3));
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* ldb uses posix locks,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * posix is stupid and kills all locks when you close *any* file
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * descriptor associated to the same file.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Therefore we must close and reopen the ldb file here */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben /* == Backup and reopen ldb == */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben /* close */
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben talloc_zfree(ldb);
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben /* backup*/
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben ret = backup_file(ldb_file, 0);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (ret != EOK) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz goto exit;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* reopen */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = sysdb_ldb_connect(tmp_ctx, ldb_file, &ldb);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret != EOK) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding DEBUG(1, ("sysdb_ldb_connect failed.\n"));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return ret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* open a transaction */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_transaction_start(ldb);
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick if (ret != LDB_SUCCESS) {
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick DEBUG(1, ("Failed to start ldb transaction! (%d)\n", ret));
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick ret = EIO;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick goto exit;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick }
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick /* == Upgrade contents == */
e8f95a682820a599fe41b22977010636be5c2717jim
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick for (dom = domains; dom; dom = dom->next) {
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick struct ldb_dn *domain_dn;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick struct ldb_dn *users_dn;
a7ad08f37d876bde1a32f0cf793f4799536ab1a5ben struct ldb_dn *groups_dn;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz int i;
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick
e8f95a682820a599fe41b22977010636be5c2717jim /* skip local */
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick if (strcasecmp(dom->provider, "local") == 0) {
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick continue;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* create new dom db */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = sysdb_domain_init_internal(tmp_ctx, dom,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding db_path, false, &sysdb);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret != EOK) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
418c12fdca30a401f7921cf66d82f7061dc67149brianp }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = ldb_transaction_start(sysdb->ldb);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (ret != LDB_SUCCESS) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz DEBUG(1, ("Failed to start ldb transaction! (%d)\n", ret));
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = EIO;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz goto done;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
fd7cb2b590294250e5b219512992cd5747289fbbbrianp ctx_trans = true;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
fd7cb2b590294250e5b219512992cd5747289fbbbrianp /* search all entries for this domain in local,
fd7cb2b590294250e5b219512992cd5747289fbbbrianp * copy them all in the new database,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz * then remove them from local */
fd7cb2b590294250e5b219512992cd5747289fbbbrianp
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz domain_dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz SYSDB_DOM_BASE, dom->name);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (!domain_dn) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = ENOMEM;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz goto done;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ldb_search(ldb, tmp_ctx, &res,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding domain_dn, LDB_SCOPE_SUBTREE,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding NULL, NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret != LDB_SUCCESS) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = EIO;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding users_dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding SYSDB_TMPL_USER_BASE, dom->name);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!users_dn) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin groups_dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin SYSDB_TMPL_GROUP_BASE, dom->name);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!groups_dn) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ENOMEM;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto done;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin for (i = 0; i < res->count; i++) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin struct ldb_dn *orig_dn;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
337489fc0f19fa2ccac530ca0e2de9fe60f28e09ylavic msg = res->msgs[i];
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* skip pre-created congtainers */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if ((ldb_dn_compare(msg->dn, domain_dn) == 0) ||
291eb44b3adaf8247425286615b4f4b69fbea274minfrin (ldb_dn_compare(msg->dn, users_dn) == 0) ||
291eb44b3adaf8247425286615b4f4b69fbea274minfrin (ldb_dn_compare(msg->dn, groups_dn) == 0)) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin continue;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin /* regenerate the DN against the new ldb as it may have different
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * casefolding rules (example: name changing from case insensitive
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * to case sensitive) */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin orig_dn = msg->dn;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin msg->dn = ldb_dn_new(msg, sysdb->ldb,
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ldb_dn_get_linearized(orig_dn));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (!msg->dn) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ENOMEM;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin goto done;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_add(sysdb->ldb, msg);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin DEBUG(0, ("WARNING: Could not add entry %s,"
291eb44b3adaf8247425286615b4f4b69fbea274minfrin " to new ldb file! (%d [%s])\n",
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ldb_dn_get_linearized(msg->dn),
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret, ldb_errstring(sysdb->ldb)));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_delete(ldb, orig_dn);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin DEBUG(0, ("WARNING: Could not remove entry %s,"
291eb44b3adaf8247425286615b4f4b69fbea274minfrin " from old ldb file! (%d [%s])\n",
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ldb_dn_get_linearized(orig_dn),
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret, ldb_errstring(ldb)));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
337489fc0f19fa2ccac530ca0e2de9fe60f28e09ylavic /* now remove the basic containers from local */
337489fc0f19fa2ccac530ca0e2de9fe60f28e09ylavic /* these were optional so debug at level 9 in case
291eb44b3adaf8247425286615b4f4b69fbea274minfrin * of failure just for tracing */
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_delete(ldb, groups_dn);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin DEBUG(9, ("WARNING: Could not remove entry %s,"
291eb44b3adaf8247425286615b4f4b69fbea274minfrin " from old ldb file! (%d [%s])\n",
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ldb_dn_get_linearized(groups_dn),
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret, ldb_errstring(ldb)));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_delete(ldb, users_dn);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin DEBUG(9, ("WARNING: Could not remove entry %s,"
291eb44b3adaf8247425286615b4f4b69fbea274minfrin " from old ldb file! (%d [%s])\n",
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ldb_dn_get_linearized(users_dn),
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret, ldb_errstring(ldb)));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_delete(ldb, domain_dn);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin DEBUG(9, ("WARNING: Could not remove entry %s,"
291eb44b3adaf8247425286615b4f4b69fbea274minfrin " from old ldb file! (%d [%s])\n",
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ldb_dn_get_linearized(domain_dn),
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret, ldb_errstring(ldb)));
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
4c138e467a7d384582aa6f8c9809af589e572feesf ret = ldb_transaction_commit(sysdb->ldb);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (ret != LDB_SUCCESS) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf DEBUG(1, ("Failed to commit ldb transaction! (%d)\n", ret));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = EIO;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
ef5650b61a8e35f3cc93ec07e73efc17ea329894jorton }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ctx_trans = false;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding talloc_zfree(domain_dn);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding talloc_zfree(groups_dn);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf talloc_zfree(users_dn);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz talloc_zfree(res);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf /* conversion done, upgrade version number */
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf msg = ldb_msg_new(tmp_ctx);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (!msg) {
bbe046d7cbb950ab3e372e4119ae001a5fe52ed4striker ret = ENOMEM;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf goto done;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf }
33510984c759eb3da154ceb0db9b75fa0031d3b4sf msg->dn = ldb_dn_new(tmp_ctx, ldb, SYSDB_BASE);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (!msg->dn) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf ret = ENOMEM;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf goto done;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf }
33510984c759eb3da154ceb0db9b75fa0031d3b4sf
33510984c759eb3da154ceb0db9b75fa0031d3b4sf ret = ldb_msg_add_empty(msg, "version", LDB_FLAG_MOD_REPLACE, NULL);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (ret != LDB_SUCCESS) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf ret = ENOMEM;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf goto done;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf }
33510984c759eb3da154ceb0db9b75fa0031d3b4sf ret = ldb_msg_add_string(msg, "version", SYSDB_VERSION_0_3);
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (ret != LDB_SUCCESS) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf ret = ENOMEM;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf goto done;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
4c138e467a7d384582aa6f8c9809af589e572feesf
185aa71728867671e105178b4c66fbc22b65ae26sf ret = ldb_modify(ldb, msg);
4c138e467a7d384582aa6f8c9809af589e572feesf if (ret != LDB_SUCCESS) {
4c138e467a7d384582aa6f8c9809af589e572feesf ret = sysdb_error_to_errno(ret);
4c138e467a7d384582aa6f8c9809af589e572feesf goto done;
4c138e467a7d384582aa6f8c9809af589e572feesf }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = ldb_transaction_commit(ldb);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (ret != LDB_SUCCESS) {
1d9e72aaaebdda86c3afc856c35872edf19c69bbsf DEBUG(1, ("Failed to commit ldb transaction! (%d)\n", ret));
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = EIO;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz goto exit;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf ret = EOK;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantzdone:
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (ret != EOK) {
33510984c759eb3da154ceb0db9b75fa0031d3b4sf if (ctx_trans) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = ldb_transaction_cancel(sysdb->ldb);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (ret != LDB_SUCCESS) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz DEBUG(1, ("Failed to cancel ldb transaction! (%d)\n", ret));
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = ldb_transaction_cancel(ldb);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (ret != LDB_SUCCESS) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz DEBUG(1, ("Failed to cancel ldb transaction! (%d)\n", ret));
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
36d38d22e0d385db01f5773a579f44b8f02e4b1fsfexit:
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf talloc_free(tmp_ctx);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return ret;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz}
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe
9bf4319b4fc7b31295b945215a55e2a92ba57903wroweint sysdb_upgrade_03(struct sysdb_ctx *sysdb, const char **ver)
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe{
32c4bc04f89b16521718145dc731f750144d7b38wrowe TALLOC_CTX *tmp_ctx;
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe int ret;
33510984c759eb3da154ceb0db9b75fa0031d3b4sf struct ldb_message *msg;
9bf4319b4fc7b31295b945215a55e2a92ba57903wrowe struct upgrade_ctx *ctx;
32c4bc04f89b16521718145dc731f750144d7b38wrowe
32c4bc04f89b16521718145dc731f750144d7b38wrowe tmp_ctx = talloc_new(NULL);
36d38d22e0d385db01f5773a579f44b8f02e4b1fsf if (!tmp_ctx) {
32c4bc04f89b16521718145dc731f750144d7b38wrowe return ENOMEM;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_4, &ctx);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return ret;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Make this database case-sensitive */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding msg = ldb_msg_new(tmp_ctx);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!msg) {
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein ret = ENOMEM;
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding msg->dn = ldb_dn_new(tmp_ctx, sysdb->ldb, "@ATTRIBUTES");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!msg->dn) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = ENOMEM;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz goto done;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_msg_add_empty(msg, "name", LDB_FLAG_MOD_DELETE, NULL);
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ENOMEM;
4c138e467a7d384582aa6f8c9809af589e572feesf goto done;
4c138e467a7d384582aa6f8c9809af589e572feesf }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
30e3e760b737f13ce800fa02c5930ade7659ba66niq ret = ldb_modify(sysdb->ldb, msg);
30e3e760b737f13ce800fa02c5930ade7659ba66niq if (ret != LDB_SUCCESS) {
30e3e760b737f13ce800fa02c5930ade7659ba66niq ret = sysdb_error_to_errno(ret);
30e3e760b737f13ce800fa02c5930ade7659ba66niq goto done;
185aa71728867671e105178b4c66fbc22b65ae26sf }
30e3e760b737f13ce800fa02c5930ade7659ba66niq
30e3e760b737f13ce800fa02c5930ade7659ba66niq /* conversion done, update version number */
30e3e760b737f13ce800fa02c5930ade7659ba66niq ret = update_version(ctx);
30e3e760b737f13ce800fa02c5930ade7659ba66niq
30e3e760b737f13ce800fa02c5930ade7659ba66niqdone:
30e3e760b737f13ce800fa02c5930ade7659ba66niq ret = finish_upgrade(ret, &ctx, ver);
185aa71728867671e105178b4c66fbc22b65ae26sf talloc_free(tmp_ctx);
30e3e760b737f13ce800fa02c5930ade7659ba66niq return ret;
30e3e760b737f13ce800fa02c5930ade7659ba66niq}
30e3e760b737f13ce800fa02c5930ade7659ba66niq
30e3e760b737f13ce800fa02c5930ade7659ba66niqint sysdb_upgrade_04(struct sysdb_ctx *sysdb, const char **ver)
30e3e760b737f13ce800fa02c5930ade7659ba66niq{
30e3e760b737f13ce800fa02c5930ade7659ba66niq TALLOC_CTX *tmp_ctx;
30e3e760b737f13ce800fa02c5930ade7659ba66niq int ret;
30e3e760b737f13ce800fa02c5930ade7659ba66niq struct ldb_message *msg;
30e3e760b737f13ce800fa02c5930ade7659ba66niq struct upgrade_ctx *ctx;
30e3e760b737f13ce800fa02c5930ade7659ba66niq
30e3e760b737f13ce800fa02c5930ade7659ba66niq tmp_ctx = talloc_new(NULL);
30e3e760b737f13ce800fa02c5930ade7659ba66niq if (!tmp_ctx) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return ENOMEM;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_5, &ctx);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin return ret;
291eb44b3adaf8247425286615b4f4b69fbea274minfrin }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz /* Add new index */
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz msg = ldb_msg_new(tmp_ctx);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!msg) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding msg->dn = ldb_dn_new(tmp_ctx, sysdb->ldb, "@INDEXLIST");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!msg->dn) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ldb_msg_add_empty(msg, "@IDXATTR", LDB_FLAG_MOD_ADD, NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ret != LDB_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = ENOMEM;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding goto done;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ldb_msg_add_string(msg, "@IDXATTR", "originalDN");
291eb44b3adaf8247425286615b4f4b69fbea274minfrin if (ret != LDB_SUCCESS) {
291eb44b3adaf8247425286615b4f4b69fbea274minfrin ret = ENOMEM;
4c138e467a7d384582aa6f8c9809af589e572feesf goto done;
4c138e467a7d384582aa6f8c9809af589e572feesf }
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar
449efc4dc68e42cc4421d15498a689618aab5dc3coar ret = ldb_modify(sysdb->ldb, msg);
449efc4dc68e42cc4421d15498a689618aab5dc3coar if (ret != LDB_SUCCESS) {
449efc4dc68e42cc4421d15498a689618aab5dc3coar ret = sysdb_error_to_errno(ret);
449efc4dc68e42cc4421d15498a689618aab5dc3coar goto done;
185aa71728867671e105178b4c66fbc22b65ae26sf }
449efc4dc68e42cc4421d15498a689618aab5dc3coar
449efc4dc68e42cc4421d15498a689618aab5dc3coar /* Rebuild memberuid and memberoif attributes */
449efc4dc68e42cc4421d15498a689618aab5dc3coar msg = ldb_msg_new(tmp_ctx);
449efc4dc68e42cc4421d15498a689618aab5dc3coar if (!msg) {
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar ret = ENOMEM;
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar goto done;
185aa71728867671e105178b4c66fbc22b65ae26sf }
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar msg->dn = ldb_dn_new(tmp_ctx, sysdb->ldb, "@MEMBEROF-REBUILD");
449efc4dc68e42cc4421d15498a689618aab5dc3coar if (!msg->dn) {
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar ret = ENOMEM;
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar goto done;
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar }
affb82a2d7fc07c1a862d800ef47af966b898768nd
affb82a2d7fc07c1a862d800ef47af966b898768nd ret = ldb_add(sysdb->ldb, msg);
affb82a2d7fc07c1a862d800ef47af966b898768nd if (ret != LDB_SUCCESS) {
affb82a2d7fc07c1a862d800ef47af966b898768nd ret = sysdb_error_to_errno(ret);
affb82a2d7fc07c1a862d800ef47af966b898768nd goto done;
affb82a2d7fc07c1a862d800ef47af966b898768nd }
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar /* conversion done, update version number */
a877b7d5d03f91d6c93076d9ccf14469c70c648dcoar ret = update_version(ctx);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingdone:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ret = finish_upgrade(ret, &ctx, ver);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding talloc_free(tmp_ctx);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return ret;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowe
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fieldingint sysdb_upgrade_05(struct sysdb_ctx *sysdb, const char **ver)
e8c95302287c86cd1f984eeb25cf3bfa9e2d33bbslive{
e8c95302287c86cd1f984eeb25cf3bfa9e2d33bbslive TALLOC_CTX *tmp_ctx;
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding int ret;
1376737cb6afa24d3e12f3a223318fe1bd71bb1fslive struct ldb_message *msg;
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe struct upgrade_ctx *ctx;
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding tmp_ctx = talloc_new(NULL);
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf if (!tmp_ctx) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return ENOMEM;
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_6, &ctx);
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz if (ret) {
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz return ret;
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz }
076ae4ad21f0b3f25e2feabd9886b9500929eb2ejerenkrantz
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Add new indexes */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, sysdb->ldb, "@INDEXLIST");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
/* Add Index for dataExpireTimestamp */
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", "dataExpireTimestamp");
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
/* Add index to speed up ONELEVEL searches */
ret = ldb_msg_add_empty(msg, "@IDXONE", LDB_FLAG_MOD_ADD, NULL);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, "@IDXONE", "1");
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
talloc_free(tmp_ctx);
return ret;
}
int sysdb_upgrade_06(struct sysdb_ctx *sysdb, const char **ver)
{
TALLOC_CTX *tmp_ctx;
int ret;
struct ldb_message *msg;
struct upgrade_ctx *ctx;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_7, &ctx);
if (ret) {
return ret;
}
/* Add new indexes */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, sysdb->ldb, "@ATTRIBUTES");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
/* Case insensitive search for originalDN */
ret = ldb_msg_add_empty(msg, SYSDB_ORIG_DN, LDB_FLAG_MOD_ADD, NULL);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, SYSDB_ORIG_DN, "CASE_INSENSITIVE");
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
talloc_free(tmp_ctx);
return ret;
}
int sysdb_upgrade_07(struct sysdb_ctx *sysdb, const char **ver)
{
TALLOC_CTX *tmp_ctx;
int ret;
struct ldb_message *msg;
struct upgrade_ctx *ctx;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_8, &ctx);
if (ret) {
return ret;
}
/* Add new indexes */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, sysdb->ldb, "@INDEXLIST");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
/* Add Index for nameAlias */
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", "nameAlias");
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
talloc_free(tmp_ctx);
return ret;
}
int sysdb_upgrade_08(struct sysdb_ctx *sysdb, const char **ver)
{
TALLOC_CTX *tmp_ctx;
int ret;
struct ldb_message *msg;
struct upgrade_ctx *ctx;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_9, &ctx);
if (ret) {
return ret;
}
/* Add new indexes */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, sysdb->ldb, "@INDEXLIST");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
/* Add Index for servicePort and serviceProtocol */
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", "servicePort");
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, "@IDXATTR", "serviceProtocol");
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
talloc_free(tmp_ctx);
return ret;
}
int sysdb_upgrade_09(struct sysdb_ctx *sysdb, const char **ver)
{
TALLOC_CTX *tmp_ctx;
int ret;
struct ldb_message *msg;
struct upgrade_ctx *ctx;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_10, &ctx);
if (ret) {
return ret;
}
/* Add new indexes */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, sysdb->ldb, "@INDEXLIST");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
/* Add Index for servicePort and serviceProtocol */
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", "sudoUser");
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
talloc_free(tmp_ctx);
return ret;
}
int sysdb_upgrade_10(struct sysdb_ctx *sysdb, struct sss_domain_info *domain,
const char **ver)
{
TALLOC_CTX *tmp_ctx;
int ret;
struct ldb_result *res;
struct ldb_message *msg;
struct ldb_message *user;
struct ldb_message_element *memberof_el;
const char *name;
struct ldb_dn *basedn;
const char *filter = "(&(objectClass=user)(!(uidNumber=*))(memberOf=*))";
const char *attrs[] = { "name", "memberof", NULL };
struct upgrade_ctx *ctx;
int i, j;
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
return ENOMEM;
}
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_11, &ctx);
if (ret) {
return ret;
}
basedn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
SYSDB_TMPL_USER_BASE, domain->name);
if (basedn == NULL) {
ret = EIO;
goto done;
}
ret = ldb_search(sysdb->ldb, tmp_ctx, &res, basedn, LDB_SCOPE_SUBTREE,
attrs, "%s", filter);
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
for (i = 0; i < res->count; i++) {
user = res->msgs[i];
memberof_el = ldb_msg_find_element(user, "memberof");
name = ldb_msg_find_attr_as_string(user, "name", NULL);
if (name == NULL) {
ret = EIO;
goto done;
}
DEBUG(SSSDBG_TRACE_LIBS, ("User [%s] is a member of %d groups\n",
name, memberof_el->num_values));
for (j = 0; j < memberof_el->num_values; j++) {
msg = ldb_msg_new(tmp_ctx);
if (msg == NULL) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_from_ldb_val(tmp_ctx, sysdb->ldb, &memberof_el->values[j]);
if (msg->dn == NULL) {
ret = ENOMEM;
goto done;
}
if (!ldb_dn_validate(msg->dn)) {
DEBUG(SSSDBG_MINOR_FAILURE, ("DN validation failed during "
"upgrade: [%s]\n",
memberof_el->values[j].data));
talloc_zfree(msg);
continue;
}
ret = ldb_msg_add_empty(msg, "ghost", LDB_FLAG_MOD_ADD, NULL);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, "ghost", name);
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
DEBUG(SSSDBG_TRACE_FUNC, ("Adding ghost [%s] to entry [%s]\n",
name, ldb_dn_get_linearized(msg->dn)));
ret = sss_ldb_modify_permissive(sysdb->ldb, msg);
talloc_zfree(msg);
if (ret == LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
/* If we failed adding the ghost user(s) because the values already
* exist, they were probably propagated from a parent that was
* upgraded before us. Mark the group as expired so that it is
* refreshed on next request.
*/
msg = ldb_msg_new(tmp_ctx);
if (msg == NULL) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_from_ldb_val(tmp_ctx, sysdb->ldb, &memberof_el->values[j]);
if (msg->dn == NULL) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_empty(msg, SYSDB_CACHE_EXPIRE,
LDB_FLAG_MOD_REPLACE, NULL);
if (ret != LDB_SUCCESS) {
goto done;
}
ret = ldb_msg_add_string(msg, SYSDB_CACHE_EXPIRE, "1");
if (ret != LDB_SUCCESS) {
goto done;
}
ret = sss_ldb_modify_permissive(sysdb->ldb, msg);
talloc_zfree(msg);
if (ret != LDB_SUCCESS) {
goto done;
}
} else if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
}
DEBUG(SSSDBG_TRACE_FUNC, ("Removing fake user [%s]\n",
ldb_dn_get_linearized(user->dn)));
ret = ldb_delete(sysdb->ldb, user->dn);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
}
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
talloc_free(tmp_ctx);
return ret;
}
int sysdb_upgrade_11(struct sysdb_ctx *sysdb, struct sss_domain_info *domain,
const char **ver)
{
TALLOC_CTX *tmp_ctx;
errno_t ret;
struct ldb_result *res;
struct ldb_message *entry;
const char *key;
const char *value;
struct ldb_message_element *memberof_el;
struct ldb_dn *memberof_dn;
struct ldb_dn *basedn;
const struct ldb_val *val;
const char *attrs[] = { SYSDB_AUTOFS_ENTRY_KEY,
SYSDB_AUTOFS_ENTRY_VALUE,
SYSDB_MEMBEROF,
NULL };
struct upgrade_ctx *ctx;
size_t i, j;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_12, &ctx);
if (ret) {
return ret;
}
basedn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_TMPL_CUSTOM_SUBTREE,
AUTOFS_ENTRY_SUBDIR, domain->name);
if (basedn == NULL) {
ret = ENOMEM;
goto done;
}
ret = ldb_search(sysdb->ldb, tmp_ctx, &res, basedn, LDB_SCOPE_SUBTREE,
attrs, "(objectClass=%s)", SYSDB_AUTOFS_ENTRY_OC);
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
DEBUG(SSSDBG_TRACE_LIBS, ("Found %d autofs entries\n", res->count));
for (i = 0; i < res->count; i++) {
entry = res->msgs[i];
key = ldb_msg_find_attr_as_string(entry,
SYSDB_AUTOFS_ENTRY_KEY, NULL);
value = ldb_msg_find_attr_as_string(entry,
SYSDB_AUTOFS_ENTRY_VALUE, NULL);
memberof_el = ldb_msg_find_element(entry, SYSDB_MEMBEROF);
if (key && value && memberof_el) {
for (j = 0; j < memberof_el->num_values; j++) {
memberof_dn = ldb_dn_from_ldb_val(tmp_ctx, sysdb->ldb,
&(memberof_el->values[j]));
if (!memberof_dn) {
DEBUG(SSSDBG_OP_FAILURE, ("Cannot convert memberof into DN, skipping\n"));
continue;
}
val = ldb_dn_get_rdn_val(memberof_dn);
if (!val) {
DEBUG(SSSDBG_OP_FAILURE, ("Cannot get map name from map DN\n"));
continue;
}
ret = sysdb_save_autofsentry(domain,
(const char *) val->data,
key, value, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE,
("Cannot save autofs entry [%s]-[%s] into map %s\n",
key, value, val->data));
continue;
}
}
}
/* Delete the old entry if it was either processed or incomplete */
DEBUG(SSSDBG_TRACE_LIBS, ("Deleting [%s]\n",
ldb_dn_get_linearized(entry->dn)));
ret = ldb_delete(sysdb->ldb, entry->dn);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("Cannot delete old autofs entry %s\n",
ldb_dn_get_linearized(entry->dn)));
continue;
}
}
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
talloc_free(tmp_ctx);
return ret;
}
int sysdb_upgrade_12(struct sysdb_ctx *sysdb, const char **ver)
{
TALLOC_CTX *tmp_ctx;
int ret;
struct ldb_message *msg;
struct upgrade_ctx *ctx;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_13, &ctx);
if (ret) {
return ret;
}
/* add new indexes */
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(tmp_ctx, sysdb->ldb, "@INDEXLIST");
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
/* add index for sshKnownHostsExpire */
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", "sshKnownHostsExpire");
if (ret != LDB_SUCCESS) {
ret = ENOMEM;
goto done;
}
ret = ldb_modify(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
talloc_free(tmp_ctx);
return ret;
}
int sysdb_upgrade_13(struct sysdb_ctx *sysdb, const char **ver)
{
struct upgrade_ctx *ctx;
struct ldb_result *dom_res;
struct ldb_result *res;
struct ldb_dn *basedn;
const char *attrs[] = { "cn", "name", NULL };
const char *tmp_str;
errno_t ret;
int i, j, l, n;
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_14, &ctx);
if (ret) {
return ret;
}
basedn = ldb_dn_new(ctx, sysdb->ldb, SYSDB_BASE);
if (!basedn) {
DEBUG(SSSDBG_OP_FAILURE, ("Failed to build base dn\n"));
ret = EIO;
goto done;
}
ret = ldb_search(sysdb->ldb, ctx, &dom_res,
basedn, LDB_SCOPE_ONELEVEL,
attrs, "objectclass=%s", SYSDB_SUBDOMAIN_CLASS);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("Failed to search subdomains\n"));
ret = EIO;
goto done;
}
for (i = 0; i < dom_res->count; i++) {
tmp_str = ldb_msg_find_attr_as_string(dom_res->msgs[i], "cn", NULL);
if (tmp_str == NULL) {
DEBUG(SSSDBG_MINOR_FAILURE,
("The object [%s] doesn't have a name\n",
ldb_dn_get_linearized(dom_res->msgs[i]->dn)));
continue;
}
basedn = ldb_dn_new_fmt(ctx, sysdb->ldb, SYSDB_DOM_BASE, tmp_str);
if (!basedn) {
DEBUG(SSSDBG_OP_FAILURE,
("Failed to build base dn for subdomain %s\n", tmp_str));
continue;
}
ret = ldb_search(sysdb->ldb, ctx, &res,
basedn, LDB_SCOPE_SUBTREE, attrs, NULL);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE,
("Failed to search subdomain %s\n", tmp_str));
talloc_free(basedn);
continue;
}
l = ldb_dn_get_comp_num(basedn);
for (j = 0; j < res->count; j++) {
n = ldb_dn_get_comp_num(res->msgs[j]->dn);
if (n <= l + 1) {
/* Do not remove subdomain containers, only their contents */
continue;
}
ret = ldb_delete(sysdb->ldb, res->msgs[j]->dn);
if (ret) {
DEBUG(SSSDBG_OP_FAILURE,
("Failed to delete %s\n",
ldb_dn_get_linearized(res->msgs[j]->dn)));
continue;
}
}
talloc_free(basedn);
talloc_free(res);
}
talloc_free(dom_res);
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
return ret;
}
int sysdb_upgrade_14(struct sysdb_ctx *sysdb, const char **ver)
{
struct upgrade_ctx *ctx;
struct ldb_message *msg;
struct ldb_result *res;
struct ldb_dn *basedn;
struct ldb_dn *newdn;
const char *attrs[] = { SYSDB_NAME, NULL };
const char *tmp_str;
errno_t ret;
int i;
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_15, &ctx);
if (ret) {
return ret;
}
basedn = ldb_dn_new(ctx, sysdb->ldb, SYSDB_BASE);
if (!basedn) {
DEBUG(SSSDBG_OP_FAILURE, ("Failed to build base dn\n"));
ret = EIO;
goto done;
}
/* create base ranges container */
msg = ldb_msg_new(ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = ldb_dn_new(msg, sysdb->ldb, SYSDB_TMPL_RANGE_BASE);
if (!msg->dn) {
ret = ENOMEM;
goto done;
}
ret = ldb_msg_add_string(msg, "cn", "ranges");
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
/* do a synchronous add */
ret = ldb_add(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_FATAL_FAILURE,
("Failed to upgrade DB (%d, [%s])!\n",
ret, ldb_errstring(sysdb->ldb)));
ret = EIO;
goto done;
}
talloc_zfree(msg);
ret = ldb_search(sysdb->ldb, ctx, &res,
basedn, LDB_SCOPE_SUBTREE, attrs,
"objectclass=%s", SYSDB_ID_RANGE_CLASS);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("Failed to search range objects\n"));
ret = EIO;
goto done;
}
/* Failure to convert any range is not fatal. As long as there are no
* left-over objects we can fail to move them around, as they will be
* recreated on the next online access */
for (i = 0; i < res->count; i++) {
tmp_str = ldb_msg_find_attr_as_string(res->msgs[i], SYSDB_NAME, NULL);
if (tmp_str == NULL) {
DEBUG(SSSDBG_OP_FAILURE,
("The object [%s] doesn't have a name\n",
ldb_dn_get_linearized(res->msgs[i]->dn)));
ret = ldb_delete(sysdb->ldb, res->msgs[i]->dn);
if (ret) {
DEBUG(SSSDBG_OP_FAILURE,
("Failed to delete %s\n",
ldb_dn_get_linearized(res->msgs[i]->dn)));
ret = EIO;
goto done;
}
continue;
}
newdn = ldb_dn_new_fmt(ctx, sysdb->ldb, SYSDB_TMPL_RANGE, tmp_str);
if (!newdn) {
DEBUG(SSSDBG_CRIT_FAILURE,
("Failed to create new DN to move [%s]\n",
ldb_dn_get_linearized(res->msgs[i]->dn)));
ret = ENOMEM;
goto done;
}
ret = ldb_rename(sysdb->ldb, res->msgs[i]->dn, newdn);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_CRIT_FAILURE,
("Failed to move [%s] to [%s]\n",
ldb_dn_get_linearized(res->msgs[i]->dn),
ldb_dn_get_linearized(newdn)));
ret = ldb_delete(sysdb->ldb, res->msgs[i]->dn);
if (ret) {
DEBUG(SSSDBG_OP_FAILURE,
("Failed to delete %s\n",
ldb_dn_get_linearized(res->msgs[i]->dn)));
ret = EIO;
goto done;
}
}
talloc_zfree(newdn);
}
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
return ret;
}
/*
* Example template for future upgrades.
* Copy and change version numbers as appropriate.
*/
#if 0
int sysdb_upgrade_13(struct sysdb_ctx *sysdb, const char **ver)
{
struct upgrade_ctx *ctx;
errno_t ret;
ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_14, &ctx);
if (ret) {
return ret;
}
/* DO STUFF HERE (use ctx, as the local temporary memory context) */
/* conversion done, update version number */
ret = update_version(ctx);
done:
ret = finish_upgrade(ret, &ctx, ver);
return ret;
}
#endif