/*
SSSD
System Database
Copyright (C) 2008-2011 Simo Sorce <ssorce@redhat.com>
Copyright (C) 2008-2011 Stephen Gallagher <ssorce@redhat.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "util/util.h"
#include "util/strtonum.h"
#include "util/sss_utf8.h"
#include "db/sysdb_private.h"
#include "confdb/confdb.h"
#include <time.h>
#define LDB_MODULES_PATH "LDB_MODULES_PATH"
errno_t sysdb_ldb_connect(TALLOC_CTX *mem_ctx, const char *filename,
struct ldb_context **_ldb)
{
int ret;
struct ldb_context *ldb;
const char *mod_path;
if (_ldb == NULL) {
return EINVAL;
}
ldb = ldb_init(mem_ctx, NULL);
if (!ldb) {
return EIO;
}
ret = ldb_set_debug(ldb, ldb_debug_messages, NULL);
if (ret != LDB_SUCCESS) {
return EIO;
}
mod_path = getenv(LDB_MODULES_PATH);
if (mod_path != NULL) {
DEBUG(9, ("Setting ldb module path to [%s].\n", mod_path));
ldb_set_modules_dir(ldb, mod_path);
}
ret = ldb_connect(ldb, filename, 0, NULL);
if (ret != LDB_SUCCESS) {
return EIO;
}
*_ldb = ldb;
return EOK;
}
errno_t sysdb_dn_sanitize(TALLOC_CTX *mem_ctx, const char *input,
char **sanitized)
{
struct ldb_val val;
errno_t ret = EOK;
val.data = (uint8_t *)talloc_strdup(mem_ctx, input);
if (!val.data) {
return ENOMEM;
}
/* We can't include the trailing NULL because it would
* be escaped and result in an unterminated string
*/
val.length = strlen(input);
*sanitized = ldb_dn_escape_value(mem_ctx, val);
if (!*sanitized) {
ret = ENOMEM;
}
talloc_free(val.data);
return ret;
}
struct ldb_dn *sysdb_custom_subtree_dn(TALLOC_CTX *mem_ctx,
struct sss_domain_info *dom,
const char *subtree_name)
{
errno_t ret;
char *clean_subtree;
struct ldb_dn *dn = NULL;
TALLOC_CTX *tmp_ctx;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) return NULL;
ret = sysdb_dn_sanitize(tmp_ctx, subtree_name, &clean_subtree);
if (ret != EOK) {
talloc_free(tmp_ctx);
return NULL;
}
dn = ldb_dn_new_fmt(tmp_ctx, dom->sysdb->ldb, SYSDB_TMPL_CUSTOM_SUBTREE,
clean_subtree, dom->name);
if (dn) {
talloc_steal(mem_ctx, dn);
}
talloc_free(tmp_ctx);
return dn;
}
struct ldb_dn *sysdb_custom_dn(TALLOC_CTX *mem_ctx,
struct sss_domain_info *dom,
const char *object_name,
const char *subtree_name)
{
errno_t ret;
TALLOC_CTX *tmp_ctx;
char *clean_name;
char *clean_subtree;
struct ldb_dn *dn = NULL;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return NULL;
}
ret = sysdb_dn_sanitize(tmp_ctx, object_name, &clean_name);
if (ret != EOK) {
goto done;
}
ret = sysdb_dn_sanitize(tmp_ctx, subtree_name, &clean_subtree);
if (ret != EOK) {
goto done;
}
dn = ldb_dn_new_fmt(mem_ctx, dom->sysdb->ldb, SYSDB_TMPL_CUSTOM, clean_name,
clean_subtree, dom->name);
done:
talloc_free(tmp_ctx);
return dn;
}
|