wmi-1.3.16 from opsview.com

This commit is contained in:
Are Casilla
2019-02-16 00:16:52 +01:00
parent 163fdd3d1b
commit 17b3af2911
2146 changed files with 678824 additions and 0 deletions
+550
View File
@@ -0,0 +1,550 @@
/*
ldb database library
Copyright (C) Andrew Tridgell 2004
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Name: ldb
*
* Component: ldb tdb cache functions
*
* Description: cache special records in a ldb/tdb
*
* Author: Andrew Tridgell
*/
#include "includes.h"
#include "ldb/include/includes.h"
#include "ldb/ldb_tdb/ldb_tdb.h"
#define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
#define LTDB_FLAG_INTEGER (1<<1)
#define LTDB_FLAG_HIDDEN (1<<2)
#define LTDB_FLAG_OBJECTCLASS (1<<3)
/* valid attribute flags */
static const struct {
const char *name;
int value;
} ltdb_valid_attr_flags[] = {
{ "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
{ "INTEGER", LTDB_FLAG_INTEGER },
{ "HIDDEN", LTDB_FLAG_HIDDEN },
{ "NONE", 0 },
{ NULL, 0 }
};
/*
de-register any special handlers for @ATTRIBUTES
*/
static void ltdb_attributes_unload(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
struct ldb_message *msg;
int i;
if (ltdb->cache->attributes == NULL) {
/* no previously loaded attributes */
return;
}
msg = ltdb->cache->attributes;
for (i=0;i<msg->num_elements;i++) {
ldb_remove_attrib_handler(module->ldb, msg->elements[i].name);
}
talloc_free(ltdb->cache->attributes);
ltdb->cache->attributes = NULL;
}
/*
add up the attrib flags for a @ATTRIBUTES element
*/
static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
{
int i;
unsigned value = 0;
for (i=0;i<el->num_values;i++) {
int j;
for (j=0;ltdb_valid_attr_flags[j].name;j++) {
if (strcmp(ltdb_valid_attr_flags[j].name,
(char *)el->values[i].data) == 0) {
value |= ltdb_valid_attr_flags[j].value;
break;
}
}
if (ltdb_valid_attr_flags[j].name == NULL) {
return -1;
}
}
*v = value;
return 0;
}
/*
register any special handlers from @ATTRIBUTES
*/
static int ltdb_attributes_load(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
struct ldb_message *msg = ltdb->cache->attributes;
struct ldb_dn *dn;
int i;
dn = ldb_dn_new(module, module->ldb, LTDB_ATTRIBUTES);
if (dn == NULL) goto failed;
if (ltdb_search_dn1(module, dn, msg) == -1) {
talloc_free(dn);
goto failed;
}
talloc_free(dn);
/* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
but its close enough for now */
for (i=0;i<msg->num_elements;i++) {
unsigned flags;
const char *syntax;
const struct ldb_attrib_handler *h;
struct ldb_attrib_handler h2;
if (ltdb_attributes_flags(&msg->elements[i], &flags) != 0) {
ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Invalid @ATTRIBUTES element for '%s'\n", msg->elements[i].name);
goto failed;
}
switch (flags & ~LTDB_FLAG_HIDDEN) {
case 0:
syntax = LDB_SYNTAX_OCTET_STRING;
break;
case LTDB_FLAG_CASE_INSENSITIVE:
syntax = LDB_SYNTAX_DIRECTORY_STRING;
break;
case LTDB_FLAG_INTEGER:
syntax = LDB_SYNTAX_INTEGER;
break;
default:
ldb_debug(module->ldb, LDB_DEBUG_ERROR,
"Invalid flag combination 0x%x for '%s' in @ATTRIBUTES\n",
flags, msg->elements[i].name);
goto failed;
}
h = ldb_attrib_handler_syntax(module->ldb, syntax);
if (h == NULL) {
ldb_debug(module->ldb, LDB_DEBUG_ERROR,
"Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES\n",
syntax, msg->elements[i].name);
goto failed;
}
h2 = *h;
h2.attr = msg->elements[i].name;
h2.flags |= LDB_ATTR_FLAG_ALLOCATED;
if (ldb_set_attrib_handlers(module->ldb, &h2, 1) != 0) {
goto failed;
}
}
return 0;
failed:
return -1;
}
/*
register any subclasses from @SUBCLASSES
*/
static int ltdb_subclasses_load(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
struct ldb_message *msg = ltdb->cache->subclasses;
struct ldb_dn *dn;
int i, j;
dn = ldb_dn_new(module, module->ldb, LTDB_SUBCLASSES);
if (dn == NULL) goto failed;
if (ltdb_search_dn1(module, dn, msg) == -1) {
talloc_free(dn);
goto failed;
}
talloc_free(dn);
for (i=0;i<msg->num_elements;i++) {
struct ldb_message_element *el = &msg->elements[i];
for (j=0;j<el->num_values;j++) {
if (ldb_subclass_add(module->ldb, el->name,
(char *)el->values[j].data) != 0) {
goto failed;
}
}
}
return 0;
failed:
return -1;
}
/*
de-register any @SUBCLASSES
*/
static void ltdb_subclasses_unload(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
struct ldb_message *msg;
int i;
if (ltdb->cache->subclasses == NULL) {
/* no previously loaded subclasses */
return;
}
msg = ltdb->cache->subclasses;
for (i=0;i<msg->num_elements;i++) {
ldb_subclass_remove(module->ldb, msg->elements[i].name);
}
talloc_free(ltdb->cache->subclasses);
ltdb->cache->subclasses = NULL;
}
/*
initialise the baseinfo record
*/
static int ltdb_baseinfo_init(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
struct ldb_message *msg;
struct ldb_message_element el;
struct ldb_val val;
int ret;
/* the initial sequence number must be different from the one
set in ltdb_cache_free(). Thanks to Jon for pointing this
out. */
const char *initial_sequence_number = "1";
ltdb->sequence_number = atof(initial_sequence_number);
msg = talloc(ltdb, struct ldb_message);
if (msg == NULL) {
goto failed;
}
msg->num_elements = 1;
msg->elements = &el;
msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
if (!msg->dn) {
goto failed;
}
el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
if (!el.name) {
goto failed;
}
el.values = &val;
el.num_values = 1;
el.flags = 0;
val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
if (!val.data) {
goto failed;
}
val.length = 1;
ret = ltdb_store(module, msg, TDB_INSERT);
talloc_free(msg);
return ret;
failed:
talloc_free(msg);
errno = ENOMEM;
return -1;
}
/*
free any cache records
*/
static void ltdb_cache_free(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
ltdb->sequence_number = 0;
talloc_free(ltdb->cache);
ltdb->cache = NULL;
}
/*
force a cache reload
*/
int ltdb_cache_reload(struct ldb_module *module)
{
ltdb_attributes_unload(module);
ltdb_subclasses_unload(module);
ltdb_cache_free(module);
return ltdb_cache_load(module);
}
/*
load the cache records
*/
int ltdb_cache_load(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
struct ldb_dn *baseinfo_dn = NULL;
struct ldb_dn *indexlist_dn = NULL;
uint64_t seq;
struct ldb_message *baseinfo;
/* a very fast check to avoid extra database reads */
if (ltdb->cache != NULL &&
tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
return 0;
}
if (ltdb->cache == NULL) {
ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
if (ltdb->cache == NULL) goto failed;
ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
if (ltdb->cache->indexlist == NULL ||
ltdb->cache->subclasses == NULL ||
ltdb->cache->attributes == NULL) {
goto failed;
}
}
baseinfo = talloc(ltdb->cache, struct ldb_message);
if (baseinfo == NULL) goto failed;
baseinfo_dn = ldb_dn_new(module, module->ldb, LTDB_BASEINFO);
if (baseinfo_dn == NULL) goto failed;
if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) == -1) {
goto failed;
}
/* possibly initialise the baseinfo */
if (!baseinfo->dn) {
if (ltdb_baseinfo_init(module) != 0) {
goto failed;
}
if (ltdb_search_dn1(module, baseinfo_dn, baseinfo) != 1) {
goto failed;
}
}
ltdb->tdb_seqnum = tdb_get_seqnum(ltdb->tdb);
/* if the current internal sequence number is the same as the one
in the database then assume the rest of the cache is OK */
seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
if (seq == ltdb->sequence_number) {
goto done;
}
ltdb->sequence_number = seq;
talloc_free(ltdb->cache->last_attribute.name);
memset(&ltdb->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
ltdb_attributes_unload(module);
ltdb_subclasses_unload(module);
talloc_free(ltdb->cache->indexlist);
talloc_free(ltdb->cache->subclasses);
ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
if (ltdb->cache->indexlist == NULL ||
ltdb->cache->subclasses == NULL ||
ltdb->cache->attributes == NULL) {
goto failed;
}
indexlist_dn = ldb_dn_new(module, module->ldb, LTDB_INDEXLIST);
if (indexlist_dn == NULL) goto failed;
if (ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist) == -1) {
goto failed;
}
if (ltdb_attributes_load(module) == -1) {
goto failed;
}
if (ltdb_subclasses_load(module) == -1) {
goto failed;
}
done:
talloc_free(baseinfo);
talloc_free(baseinfo_dn);
talloc_free(indexlist_dn);
return 0;
failed:
talloc_free(baseinfo);
talloc_free(baseinfo_dn);
talloc_free(indexlist_dn);
return -1;
}
/*
increase the sequence number to indicate a database change
*/
int ltdb_increase_sequence_number(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
struct ldb_message *msg;
struct ldb_message_element el[2];
struct ldb_val val;
struct ldb_val val_time;
time_t t = time(NULL);
char *s = NULL;
int ret;
msg = talloc(ltdb, struct ldb_message);
if (msg == NULL) {
errno = ENOMEM;
return -1;
}
s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
if (!s) {
errno = ENOMEM;
return -1;
}
msg->num_elements = ARRAY_SIZE(el);
msg->elements = el;
msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
if (msg->dn == NULL) {
talloc_free(msg);
errno = ENOMEM;
return -1;
}
el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
if (el[0].name == NULL) {
talloc_free(msg);
errno = ENOMEM;
return -1;
}
el[0].values = &val;
el[0].num_values = 1;
el[0].flags = LDB_FLAG_MOD_REPLACE;
val.data = (uint8_t *)s;
val.length = strlen(s);
el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
if (el[1].name == NULL) {
talloc_free(msg);
errno = ENOMEM;
return -1;
}
el[1].values = &val_time;
el[1].num_values = 1;
el[1].flags = LDB_FLAG_MOD_REPLACE;
s = ldb_timestring(msg, t);
if (s == NULL) {
return -1;
}
val_time.data = (uint8_t *)s;
val_time.length = strlen(s);
ret = ltdb_modify_internal(module, msg);
talloc_free(msg);
if (ret == 0) {
ltdb->sequence_number += 1;
}
return ret;
}
/*
return the attribute flags from the @ATTRIBUTES record
for the given attribute
*/
int ltdb_attribute_flags(struct ldb_module *module, const char *attr_name)
{
struct ltdb_private *ltdb = module->private_data;
const struct ldb_message_element *attr_el;
int i, j, ret=0;
if (ltdb->cache->last_attribute.name &&
ldb_attr_cmp(ltdb->cache->last_attribute.name, attr_name) == 0) {
return ltdb->cache->last_attribute.flags;
}
/* objectclass is a special default case */
if (ldb_attr_cmp(attr_name, LTDB_OBJECTCLASS) == 0) {
ret = LTDB_FLAG_OBJECTCLASS | LTDB_FLAG_CASE_INSENSITIVE;
}
attr_el = ldb_msg_find_element(ltdb->cache->attributes, attr_name);
if (!attr_el) {
/* check if theres a wildcard attribute */
attr_el = ldb_msg_find_element(ltdb->cache->attributes, "*");
if (!attr_el) {
return ret;
}
}
for (i = 0; i < attr_el->num_values; i++) {
for (j=0; ltdb_valid_attr_flags[j].name; j++) {
if (strcmp(ltdb_valid_attr_flags[j].name,
(char *)attr_el->values[i].data) == 0) {
ret |= ltdb_valid_attr_flags[j].value;
}
}
}
talloc_free(ltdb->cache->last_attribute.name);
ltdb->cache->last_attribute.name = talloc_strdup(ltdb->cache, attr_name);
ltdb->cache->last_attribute.flags = ret;
return ret;
}
int ltdb_check_at_attributes_values(const struct ldb_val *value)
{
int i;
for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {
return 0;
}
}
return -1;
}
File diff suppressed because it is too large Load Diff
+292
View File
@@ -0,0 +1,292 @@
/*
ldb database library
Copyright (C) Andrew Tridgell 2004
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Name: ldb
*
* Component: ldb pack/unpack
*
* Description: pack/unpack routines for ldb messages as key/value blobs
*
* Author: Andrew Tridgell
*/
#include "includes.h"
#include "ldb/include/includes.h"
#include "ldb/ldb_tdb/ldb_tdb.h"
/* change this if the data format ever changes */
#define LTDB_PACKING_FORMAT 0x26011967
/* old packing formats */
#define LTDB_PACKING_FORMAT_NODN 0x26011966
/* use a portable integer format */
static void put_uint32(uint8_t *p, int ofs, unsigned int val)
{
p += ofs;
p[0] = val&0xFF;
p[1] = (val>>8) & 0xFF;
p[2] = (val>>16) & 0xFF;
p[3] = (val>>24) & 0xFF;
}
static unsigned int pull_uint32(uint8_t *p, int ofs)
{
p += ofs;
return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
}
static int attribute_storable_values(const struct ldb_message_element *el)
{
if (el->num_values == 0) return 0;
if (ldb_attr_cmp(el->name, "dn") == 0) return 0;
if (ldb_attr_cmp(el->name, "distinguishedName") == 0) return 0;
return el->num_values;
}
/*
pack a ldb message into a linear buffer in a TDB_DATA
note that this routine avoids saving elements with zero values,
as these are equivalent to having no element
caller frees the data buffer after use
*/
int ltdb_pack_data(struct ldb_module *module,
const struct ldb_message *message,
struct TDB_DATA *data)
{
struct ldb_context *ldb = module->ldb;
unsigned int i, j, real_elements=0;
size_t size;
const char *dn;
uint8_t *p;
size_t len;
dn = ldb_dn_get_linearized(message->dn);
if (dn == NULL) {
errno = ENOMEM;
return -1;
}
/* work out how big it needs to be */
size = 8;
size += 1 + strlen(dn);
for (i=0;i<message->num_elements;i++) {
if (attribute_storable_values(&message->elements[i]) == 0) {
continue;
}
real_elements++;
size += 1 + strlen(message->elements[i].name) + 4;
for (j=0;j<message->elements[i].num_values;j++) {
size += 4 + message->elements[i].values[j].length + 1;
}
}
/* allocate it */
data->dptr = talloc_array(ldb, uint8_t, size);
if (!data->dptr) {
errno = ENOMEM;
return -1;
}
data->dsize = size;
p = data->dptr;
put_uint32(p, 0, LTDB_PACKING_FORMAT);
put_uint32(p, 4, real_elements);
p += 8;
/* the dn needs to be packed so we can be case preserving
while hashing on a case folded dn */
len = strlen(dn);
memcpy(p, dn, len+1);
p += len + 1;
for (i=0;i<message->num_elements;i++) {
if (attribute_storable_values(&message->elements[i]) == 0) {
continue;
}
len = strlen(message->elements[i].name);
memcpy(p, message->elements[i].name, len+1);
p += len + 1;
put_uint32(p, 0, message->elements[i].num_values);
p += 4;
for (j=0;j<message->elements[i].num_values;j++) {
put_uint32(p, 0, message->elements[i].values[j].length);
memcpy(p+4, message->elements[i].values[j].data,
message->elements[i].values[j].length);
p[4+message->elements[i].values[j].length] = 0;
p += 4 + message->elements[i].values[j].length + 1;
}
}
return 0;
}
/*
unpack a ldb message from a linear buffer in TDB_DATA
Free with ltdb_unpack_data_free()
*/
int ltdb_unpack_data(struct ldb_module *module,
const struct TDB_DATA *data,
struct ldb_message *message)
{
struct ldb_context *ldb = module->ldb;
uint8_t *p;
unsigned int remaining;
unsigned int i, j;
unsigned format;
size_t len;
message->elements = NULL;
p = data->dptr;
if (data->dsize < 8) {
errno = EIO;
goto failed;
}
format = pull_uint32(p, 0);
message->num_elements = pull_uint32(p, 4);
p += 8;
remaining = data->dsize - 8;
switch (format) {
case LTDB_PACKING_FORMAT_NODN:
message->dn = NULL;
break;
case LTDB_PACKING_FORMAT:
len = strnlen((char *)p, remaining);
if (len == remaining) {
errno = EIO;
goto failed;
}
message->dn = ldb_dn_new(message, ldb, (char *)p);
if (message->dn == NULL) {
errno = ENOMEM;
goto failed;
}
remaining -= len + 1;
p += len + 1;
break;
default:
errno = EIO;
goto failed;
}
if (message->num_elements == 0) {
message->elements = NULL;
return 0;
}
if (message->num_elements > remaining / 6) {
errno = EIO;
goto failed;
}
message->elements = talloc_array(message, struct ldb_message_element, message->num_elements);
if (!message->elements) {
errno = ENOMEM;
goto failed;
}
memset(message->elements, 0,
message->num_elements * sizeof(struct ldb_message_element));
for (i=0;i<message->num_elements;i++) {
if (remaining < 10) {
errno = EIO;
goto failed;
}
len = strnlen((char *)p, remaining-6);
if (len == remaining-6) {
errno = EIO;
goto failed;
}
message->elements[i].flags = 0;
message->elements[i].name = talloc_strndup(message->elements, (char *)p, len);
if (message->elements[i].name == NULL) {
errno = ENOMEM;
goto failed;
}
remaining -= len + 1;
p += len + 1;
message->elements[i].num_values = pull_uint32(p, 0);
message->elements[i].values = NULL;
if (message->elements[i].num_values != 0) {
message->elements[i].values = talloc_array(message->elements,
struct ldb_val,
message->elements[i].num_values);
if (!message->elements[i].values) {
errno = ENOMEM;
goto failed;
}
}
p += 4;
remaining -= 4;
for (j=0;j<message->elements[i].num_values;j++) {
len = pull_uint32(p, 0);
if (len > remaining-5) {
errno = EIO;
goto failed;
}
message->elements[i].values[j].length = len;
message->elements[i].values[j].data = talloc_size(message->elements[i].values, len+1);
if (message->elements[i].values[j].data == NULL) {
errno = ENOMEM;
goto failed;
}
memcpy(message->elements[i].values[j].data, p+4, len);
message->elements[i].values[j].data[len] = 0;
remaining -= len+4+1;
p += len+4+1;
}
}
if (remaining != 0) {
ldb_debug(ldb, LDB_DEBUG_ERROR,
"Error: %d bytes unread in ltdb_unpack_data\n", remaining);
}
return 0;
failed:
talloc_free(message->elements);
return -1;
}
+525
View File
@@ -0,0 +1,525 @@
/*
ldb database library
Copyright (C) Andrew Tridgell 2004
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Name: ldb
*
* Component: ldb search functions
*
* Description: functions to search ldb+tdb databases
*
* Author: Andrew Tridgell
*/
#include "includes.h"
#include "ldb/include/includes.h"
#include "ldb/ldb_tdb/ldb_tdb.h"
/*
add one element to a message
*/
static int msg_add_element(struct ldb_message *ret,
const struct ldb_message_element *el,
int check_duplicates)
{
unsigned int i;
struct ldb_message_element *e2, *elnew;
if (check_duplicates && ldb_msg_find_element(ret, el->name)) {
/* its already there */
return 0;
}
e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
if (!e2) {
return -1;
}
ret->elements = e2;
elnew = &e2[ret->num_elements];
elnew->name = talloc_strdup(ret->elements, el->name);
if (!elnew->name) {
return -1;
}
if (el->num_values) {
elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
if (!elnew->values) {
return -1;
}
} else {
elnew->values = NULL;
}
for (i=0;i<el->num_values;i++) {
elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
if (elnew->values[i].length != el->values[i].length) {
return -1;
}
}
elnew->num_values = el->num_values;
ret->num_elements++;
return 0;
}
/*
add the special distinguishedName element
*/
static int msg_add_distinguished_name(struct ldb_message *msg)
{
struct ldb_message_element el;
struct ldb_val val;
int ret;
el.flags = 0;
el.name = "distinguishedName";
el.num_values = 1;
el.values = &val;
val.data = (uint8_t *)ldb_dn_alloc_linearized(msg, msg->dn);
val.length = strlen((char *)val.data);
ret = msg_add_element(msg, &el, 1);
return ret;
}
/*
add all elements from one message into another
*/
static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
const struct ldb_message *msg)
{
struct ldb_context *ldb = module->ldb;
unsigned int i;
int check_duplicates = (ret->num_elements != 0);
if (msg_add_distinguished_name(ret) != 0) {
return -1;
}
for (i=0;i<msg->num_elements;i++) {
const struct ldb_attrib_handler *h;
h = ldb_attrib_handler(ldb, msg->elements[i].name);
if (h->flags & LDB_ATTR_FLAG_HIDDEN) {
continue;
}
if (msg_add_element(ret, &msg->elements[i],
check_duplicates) != 0) {
return -1;
}
}
return 0;
}
/*
pull the specified list of attributes from a message
*/
static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
TALLOC_CTX *mem_ctx,
const struct ldb_message *msg,
const char * const *attrs)
{
struct ldb_message *ret;
int i;
ret = talloc(mem_ctx, struct ldb_message);
if (!ret) {
return NULL;
}
ret->dn = ldb_dn_copy(ret, msg->dn);
if (!ret->dn) {
talloc_free(ret);
return NULL;
}
ret->num_elements = 0;
ret->elements = NULL;
if (!attrs) {
if (msg_add_all_elements(module, ret, msg) != 0) {
talloc_free(ret);
return NULL;
}
return ret;
}
for (i=0;attrs[i];i++) {
struct ldb_message_element *el;
if (strcmp(attrs[i], "*") == 0) {
if (msg_add_all_elements(module, ret, msg) != 0) {
talloc_free(ret);
return NULL;
}
continue;
}
if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
if (msg_add_distinguished_name(ret) != 0) {
return NULL;
}
continue;
}
el = ldb_msg_find_element(msg, attrs[i]);
if (!el) {
continue;
}
if (msg_add_element(ret, el, 1) != 0) {
talloc_free(ret);
return NULL;
}
}
return ret;
}
/*
search the database for a single simple dn, returning all attributes
in a single message
return 1 on success, 0 on record-not-found and -1 on error
*/
int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg)
{
struct ltdb_private *ltdb = module->private_data;
int ret;
TDB_DATA tdb_key, tdb_data;
memset(msg, 0, sizeof(*msg));
/* form the key */
tdb_key = ltdb_key(module, dn);
if (!tdb_key.dptr) {
return -1;
}
tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
talloc_free(tdb_key.dptr);
if (!tdb_data.dptr) {
return 0;
}
msg->num_elements = 0;
msg->elements = NULL;
ret = ltdb_unpack_data(module, &tdb_data, msg);
free(tdb_data.dptr);
if (ret == -1) {
return -1;
}
if (!msg->dn) {
msg->dn = ldb_dn_copy(msg, dn);
}
if (!msg->dn) {
return -1;
}
return 1;
}
/*
lock the database for read - use by ltdb_search
*/
static int ltdb_lock_read(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
return tdb_lockall_read(ltdb->tdb);
}
/*
unlock the database after a ltdb_lock_read()
*/
static int ltdb_unlock_read(struct ldb_module *module)
{
struct ltdb_private *ltdb = module->private_data;
return tdb_unlockall_read(ltdb->tdb);
}
/*
add a set of attributes from a record to a set of results
return 0 on success, -1 on failure
*/
int ltdb_add_attr_results(struct ldb_module *module,
TALLOC_CTX *mem_ctx,
struct ldb_message *msg,
const char * const attrs[],
unsigned int *count,
struct ldb_message ***res)
{
struct ldb_message *msg2;
struct ldb_message **res2;
/* pull the attributes that the user wants */
msg2 = ltdb_pull_attrs(module, mem_ctx, msg, attrs);
if (!msg2) {
return -1;
}
/* add to the results list */
res2 = talloc_realloc(mem_ctx, *res, struct ldb_message *, (*count)+2);
if (!res2) {
talloc_free(msg2);
return -1;
}
(*res) = res2;
(*res)[*count] = talloc_move(*res, &msg2);
(*res)[(*count)+1] = NULL;
(*count)++;
return 0;
}
/*
filter the specified list of attributes from a message
removing not requested attrs.
*/
int ltdb_filter_attrs(struct ldb_message *msg, const char * const *attrs)
{
int i, keep_all = 0;
if (attrs) {
/* check for special attrs */
for (i = 0; attrs[i]; i++) {
if (strcmp(attrs[i], "*") == 0) {
keep_all = 1;
break;
}
if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
if (msg_add_distinguished_name(msg) != 0) {
return -1;
}
}
}
} else {
keep_all = 1;
}
if (keep_all) {
if (msg_add_distinguished_name(msg) != 0) {
return -1;
}
return 0;
}
for (i = 0; i < msg->num_elements; i++) {
int j, found;
for (j = 0, found = 0; attrs[j]; j++) {
if (ldb_attr_cmp(msg->elements[i].name, attrs[j]) == 0) {
found = 1;
break;
}
}
if (!found) {
ldb_msg_remove_attr(msg, msg->elements[i].name);
i--;
}
}
return 0;
}
/*
search function for a non-indexed search
*/
static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
{
struct ldb_handle *handle = talloc_get_type(state, struct ldb_handle);
struct ltdb_context *ac = talloc_get_type(handle->private_data, struct ltdb_context);
struct ldb_reply *ares = NULL;
int ret;
if (key.dsize < 4 ||
strncmp((char *)key.dptr, "DN=", 3) != 0) {
return 0;
}
ares = talloc_zero(ac, struct ldb_reply);
if (!ares) {
handle->status = LDB_ERR_OPERATIONS_ERROR;
handle->state = LDB_ASYNC_DONE;
return -1;
}
ares->message = ldb_msg_new(ares);
if (!ares->message) {
handle->status = LDB_ERR_OPERATIONS_ERROR;
handle->state = LDB_ASYNC_DONE;
talloc_free(ares);
return -1;
}
/* unpack the record */
ret = ltdb_unpack_data(ac->module, &data, ares->message);
if (ret == -1) {
talloc_free(ares);
return -1;
}
if (!ares->message->dn) {
ares->message->dn = ldb_dn_new(ares->message, ac->module->ldb, (char *)key.dptr + 3);
if (ares->message->dn == NULL) {
handle->status = LDB_ERR_OPERATIONS_ERROR;
handle->state = LDB_ASYNC_DONE;
talloc_free(ares);
return -1;
}
}
/* see if it matches the given expression */
if (!ldb_match_msg(ac->module->ldb, ares->message, ac->tree,
ac->base, ac->scope)) {
talloc_free(ares);
return 0;
}
/* filter the attributes that the user wants */
ret = ltdb_filter_attrs(ares->message, ac->attrs);
if (ret == -1) {
handle->status = LDB_ERR_OPERATIONS_ERROR;
handle->state = LDB_ASYNC_DONE;
talloc_free(ares);
return -1;
}
ares->type = LDB_REPLY_ENTRY;
handle->state = LDB_ASYNC_PENDING;
handle->status = ac->callback(ac->module->ldb, ac->context, ares);
if (handle->status != LDB_SUCCESS) {
/* don't try to free ares here, the callback is in charge of that */
return -1;
}
return 0;
}
/*
search the database with a LDAP-like expression.
this is the "full search" non-indexed variant
*/
static int ltdb_search_full(struct ldb_handle *handle)
{
struct ltdb_context *ac = talloc_get_type(handle->private_data, struct ltdb_context);
struct ltdb_private *ltdb = talloc_get_type(ac->module->private_data, struct ltdb_private);
int ret;
ret = tdb_traverse_read(ltdb->tdb, search_func, handle);
if (ret == -1) {
handle->status = LDB_ERR_OPERATIONS_ERROR;
}
handle->state = LDB_ASYNC_DONE;
return LDB_SUCCESS;
}
/*
search the database with a LDAP-like expression.
choses a search method
*/
int ltdb_search(struct ldb_module *module, struct ldb_request *req)
{
struct ltdb_private *ltdb = talloc_get_type(module->private_data, struct ltdb_private);
struct ltdb_context *ltdb_ac;
struct ldb_reply *ares;
int ret;
if ((( ! ldb_dn_is_valid(req->op.search.base)) || ldb_dn_is_null(req->op.search.base)) &&
(req->op.search.scope == LDB_SCOPE_BASE || req->op.search.scope == LDB_SCOPE_ONELEVEL))
return LDB_ERR_OPERATIONS_ERROR;
if (ltdb_lock_read(module) != 0) {
return LDB_ERR_OPERATIONS_ERROR;
}
if (ltdb_cache_load(module) != 0) {
ltdb_unlock_read(module);
return LDB_ERR_OPERATIONS_ERROR;
}
if (req->op.search.tree == NULL) {
ltdb_unlock_read(module);
return LDB_ERR_OPERATIONS_ERROR;
}
req->handle = init_ltdb_handle(ltdb, module, req);
if (req->handle == NULL) {
ltdb_unlock_read(module);
return LDB_ERR_OPERATIONS_ERROR;
}
ltdb_ac = talloc_get_type(req->handle->private_data, struct ltdb_context);
ltdb_ac->tree = req->op.search.tree;
ltdb_ac->scope = req->op.search.scope;
ltdb_ac->base = req->op.search.base;
ltdb_ac->attrs = req->op.search.attrs;
ret = ltdb_search_indexed(req->handle);
if (ret == -1) {
ret = ltdb_search_full(req->handle);
}
if (ret != LDB_SUCCESS) {
ldb_set_errstring(module->ldb, "Indexed and full searches both failed!\n");
req->handle->state = LDB_ASYNC_DONE;
req->handle->status = ret;
}
/* Finally send an LDB_REPLY_DONE packet when searching is finished */
ares = talloc_zero(req, struct ldb_reply);
if (!ares) {
ltdb_unlock_read(module);
return LDB_ERR_OPERATIONS_ERROR;
}
req->handle->state = LDB_ASYNC_DONE;
ares->type = LDB_REPLY_DONE;
ret = req->callback(module->ldb, req->context, ares);
req->handle->status = ret;
ltdb_unlock_read(module);
return LDB_SUCCESS;
}
File diff suppressed because it is too large Load Diff
+129
View File
@@ -0,0 +1,129 @@
#ifdef _SAMBA_BUILD_
#include "system/filesys.h"
#endif
#if (_SAMBA_BUILD_ >= 4)
#include "lib/tdb/include/tdb.h"
#elif defined(_SAMBA_BUILD_)
#include "tdb/include/tdb.h"
#else
#include "tdb.h"
#endif
/* this private structure is used by the ltdb backend in the
ldb_context */
struct ltdb_private {
TDB_CONTEXT *tdb;
unsigned int connect_flags;
/* a double is used for portability and ease of string
handling. It has plenty of digits of precision */
unsigned long long sequence_number;
/* the low level tdb seqnum - used to avoid loading BASEINFO when
possible */
int tdb_seqnum;
struct ltdb_cache {
struct ldb_message *indexlist;
struct ldb_message *attributes;
struct ldb_message *subclasses;
struct {
char *name;
int flags;
} last_attribute;
} *cache;
};
/*
the async local context
holds also internal search state during a full db search
*/
struct ltdb_context {
struct ldb_module *module;
/* search stuff */
const struct ldb_parse_tree *tree;
struct ldb_dn *base;
enum ldb_scope scope;
const char * const *attrs;
/* async stuff */
void *context;
int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
};
/* special record types */
#define LTDB_INDEX "@INDEX"
#define LTDB_INDEXLIST "@INDEXLIST"
#define LTDB_IDX "@IDX"
#define LTDB_IDXATTR "@IDXATTR"
#define LTDB_BASEINFO "@BASEINFO"
#define LTDB_ATTRIBUTES "@ATTRIBUTES"
#define LTDB_SUBCLASSES "@SUBCLASSES"
/* special attribute types */
#define LTDB_SEQUENCE_NUMBER "sequenceNumber"
#define LTDB_MOD_TIMESTAMP "whenChanged"
#define LTDB_OBJECTCLASS "objectClass"
/* The following definitions come from lib/ldb/ldb_tdb/ldb_cache.c */
int ltdb_cache_reload(struct ldb_module *module);
int ltdb_cache_load(struct ldb_module *module);
int ltdb_increase_sequence_number(struct ldb_module *module);
int ltdb_check_at_attributes_values(const struct ldb_val *value);
/* The following definitions come from lib/ldb/ldb_tdb/ldb_index.c */
struct ldb_parse_tree;
int ltdb_search_indexed(struct ldb_handle *handle);
int ltdb_index_add(struct ldb_module *module, const struct ldb_message *msg);
int ltdb_index_del(struct ldb_module *module, const struct ldb_message *msg);
int ltdb_reindex(struct ldb_module *module);
/* The following definitions come from lib/ldb/ldb_tdb/ldb_pack.c */
int ltdb_pack_data(struct ldb_module *module,
const struct ldb_message *message,
struct TDB_DATA *data);
void ltdb_unpack_data_free(struct ldb_module *module,
struct ldb_message *message);
int ltdb_unpack_data(struct ldb_module *module,
const struct TDB_DATA *data,
struct ldb_message *message);
/* The following definitions come from lib/ldb/ldb_tdb/ldb_search.c */
int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name,
const struct ldb_val *val);
void ltdb_search_dn1_free(struct ldb_module *module, struct ldb_message *msg);
int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg);
int ltdb_add_attr_results(struct ldb_module *module,
TALLOC_CTX *mem_ctx,
struct ldb_message *msg,
const char * const attrs[],
unsigned int *count,
struct ldb_message ***res);
int ltdb_filter_attrs(struct ldb_message *msg, const char * const *attrs);
int ltdb_search(struct ldb_module *module, struct ldb_request *req);
/* The following definitions come from lib/ldb/ldb_tdb/ldb_tdb.c */
struct ldb_handle *init_ltdb_handle(struct ltdb_private *ltdb, struct ldb_module *module,
struct ldb_request *req);
struct TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn);
int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs);
int ltdb_delete_noindex(struct ldb_module *module, struct ldb_dn *dn);
int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg);
int ltdb_index_del_value(struct ldb_module *module, const char *dn,
struct ldb_message_element *el, int v_idx);
struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
const char *path, int hash_size, int tdb_flags,
int open_flags, mode_t mode,
struct ldb_context *ldb);
+176
View File
@@ -0,0 +1,176 @@
/*
ldb database library
Copyright (C) Andrew Tridgell 2005
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "includes.h"
#include "ldb/include/includes.h"
#include "ldb/ldb_tdb/ldb_tdb.h"
/*
the purpose of this code is to work around the braindead posix locking
rules, to allow us to have a ldb open more than once while allowing
locking to work
*/
struct ltdb_wrap {
struct ltdb_wrap *next, *prev;
struct tdb_context *tdb;
dev_t device;
ino_t inode;
};
static struct ltdb_wrap *tdb_list;
/* destroy the last connection to a tdb */
static int ltdb_wrap_destructor(struct ltdb_wrap *w)
{
tdb_close(w->tdb);
if (w->next) {
w->next->prev = w->prev;
}
if (w->prev) {
w->prev->next = w->next;
}
if (w == tdb_list) {
tdb_list = w->next;
}
return 0;
}
#if defined(_SAMBA_BUILD_) && (_SAMBA_BUILD_ <= 3)
static void ltdb_log_fn(struct tdb_context *tdb, int level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
static void ltdb_log_fn(struct tdb_context *tdb, int level, const char *fmt, ...)
{
/* until we merge the tdb debug changes into samba3, we don't know
how serious the error is, and we can't go via the ldb loggin code */
va_list ap;
const char *name = tdb_name(tdb);
char *message;
va_start(ap, fmt);
message = talloc_vasprintf(NULL, fmt, ap);
va_end(ap);
DEBUG(3, ("ltdb: tdb(%s): %s", name, message));
talloc_free(message);
}
#else
static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
{
va_list ap;
const char *name = tdb_name(tdb);
struct ldb_context *ldb = talloc_get_type(tdb_get_logging_private(tdb), struct ldb_context);
enum ldb_debug_level ldb_level;
char *message;
va_start(ap, fmt);
message = talloc_vasprintf(ldb, fmt, ap);
va_end(ap);
switch (level) {
case TDB_DEBUG_FATAL:
ldb_level = LDB_DEBUG_FATAL;
break;
case TDB_DEBUG_ERROR:
ldb_level = LDB_DEBUG_ERROR;
break;
case TDB_DEBUG_WARNING:
ldb_level = LDB_DEBUG_WARNING;
break;
case TDB_DEBUG_TRACE:
ldb_level = LDB_DEBUG_TRACE;
break;
default:
ldb_level = LDB_DEBUG_FATAL;
}
ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
talloc_free(message);
}
#endif
/*
wrapped connection to a tdb database. The caller should _not_ free
this as it is not a talloc structure (as tdb does not use talloc
yet). It will auto-close when the caller frees the mem_ctx that is
passed to this call
*/
struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
const char *path, int hash_size,
int tdb_flags,
int open_flags, mode_t mode,
struct ldb_context *ldb)
{
struct ltdb_wrap *w;
struct stat st;
#if defined(_SAMBA_BUILD_) && (_SAMBA_BUILD_ <= 3)
tdb_log_func log_ctx_p = ltdb_log_fn;
#else
struct tdb_logging_context log_ctx;
const struct tdb_logging_context *log_ctx_p = &log_ctx;
log_ctx.log_fn = ltdb_log_fn;
log_ctx.log_private = ldb;
#endif
if (stat(path, &st) == 0) {
for (w=tdb_list;w;w=w->next) {
if (st.st_dev == w->device && st.st_ino == w->inode) {
if (!talloc_reference(mem_ctx, w)) {
return NULL;
}
return w->tdb;
}
}
}
w = talloc(mem_ctx, struct ltdb_wrap);
if (w == NULL) {
return NULL;
}
w->tdb = tdb_open_ex(path, hash_size, tdb_flags, open_flags, mode, log_ctx_p, NULL);
if (w->tdb == NULL) {
talloc_free(w);
return NULL;
}
if (fstat(tdb_fd(w->tdb), &st) != 0) {
tdb_close(w->tdb);
talloc_free(w);
return NULL;
}
w->device = st.st_dev;
w->inode = st.st_ino;
talloc_set_destructor(w, ltdb_wrap_destructor);
w->next = tdb_list;
w->prev = NULL;
if (tdb_list) {
tdb_list->prev = w;
}
tdb_list = w;
return w->tdb;
}