*/
#include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_errors.h"
-#include "ldb/include/ldb_private.h"
+#include "ldb/include/includes.h"
/*
create a new ldb_message in a given memory context (NULL for top level)
{
struct ldb_message_element *els;
+ if (! ldb_valid_attr_name(attr_name)) {
+ return -1;
+ }
+
els = talloc_realloc(msg, msg->elements,
struct ldb_message_element, msg->num_elements+1);
if (!els) {
els[msg->num_elements].flags = flags;
els[msg->num_elements].name = talloc_strdup(els, attr_name);
if (!els[msg->num_elements].name) {
+ errno = ENOMEM;
return -1;
}
}
+/*
+ add a value to a message, stealing it into the 'right' place
+*/
+int ldb_msg_add_steal_value(struct ldb_message *msg,
+ const char *attr_name,
+ struct ldb_val *val)
+{
+ int ret;
+ ret = ldb_msg_add_value(msg, attr_name, val);
+ if (ret == LDB_SUCCESS) {
+ struct ldb_message_element *el;
+ el = ldb_msg_find_element(msg, attr_name);
+ talloc_steal(el->values, val->data);
+ }
+ return ret;
+}
+
+
/*
add a string element to a message
*/
return ldb_msg_add_value(msg, attr_name, &val);
}
+/*
+ add a string element to a message, stealing it into the 'right' place
+*/
+int ldb_msg_add_steal_string(struct ldb_message *msg,
+ const char *attr_name, char *str)
+{
+ struct ldb_val val;
+
+ val.data = (uint8_t *)str;
+ val.length = strlen(str);
+
+ return ldb_msg_add_steal_value(msg, attr_name, &val);
+}
+
/*
add a printf formatted element to a message
*/
val.data = (uint8_t *)str;
val.length = strlen(str);
- return ldb_msg_add_value(msg, attr_name, &val);
+ return ldb_msg_add_steal_value(msg, attr_name, &val);
}
/*
return &el->values[0];
}
-int ldb_msg_find_int(const struct ldb_message *msg,
- const char *attr_name,
- int default_value)
+int ldb_msg_find_attr_as_int(const struct ldb_message *msg,
+ const char *attr_name,
+ int default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return strtol((const char *)v->data, NULL, 0);
}
-unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
- const char *attr_name,
- unsigned int default_value)
+unsigned int ldb_msg_find_attr_as_uint(const struct ldb_message *msg,
+ const char *attr_name,
+ unsigned int default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return strtoul((const char *)v->data, NULL, 0);
}
-int64_t ldb_msg_find_int64(const struct ldb_message *msg,
- const char *attr_name,
- int64_t default_value)
+int64_t ldb_msg_find_attr_as_int64(const struct ldb_message *msg,
+ const char *attr_name,
+ int64_t default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return strtoll((const char *)v->data, NULL, 0);
}
-uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
- const char *attr_name,
- uint64_t default_value)
+uint64_t ldb_msg_find_attr_as_uint64(const struct ldb_message *msg,
+ const char *attr_name,
+ uint64_t default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return strtoull((const char *)v->data, NULL, 0);
}
-double ldb_msg_find_double(const struct ldb_message *msg,
- const char *attr_name,
- double default_value)
+double ldb_msg_find_attr_as_double(const struct ldb_message *msg,
+ const char *attr_name,
+ double default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return strtod((const char *)v->data, NULL);
}
-const char *ldb_msg_find_string(const struct ldb_message *msg,
- const char *attr_name,
- const char *default_value)
+int ldb_msg_find_attr_as_bool(const struct ldb_message *msg,
+ const char *attr_name,
+ int default_value)
+{
+ const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
+ if (!v || !v->data) {
+ return default_value;
+ }
+ if (strcasecmp((const char *)v->data, "FALSE") == 0) {
+ return 0;
+ }
+ if (strcasecmp((const char *)v->data, "TRUE") == 0) {
+ return 1;
+ }
+ return default_value;
+}
+
+const char *ldb_msg_find_attr_as_string(const struct ldb_message *msg,
+ const char *attr_name,
+ const char *default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return (const char *)v->data;
}
+struct ldb_dn *ldb_msg_find_attr_as_dn(void *mem_ctx,
+ const struct ldb_message *msg,
+ const char *attr_name)
+{
+ const struct ldb_val *v;
+
+ v = ldb_msg_find_ldb_val(msg, attr_name);
+ if (!v || !v->data) {
+ return NULL;
+ }
+ return ldb_dn_explode(mem_ctx, (const char *)v->data);
+}
+
/*
sort the elements of a message by name
*/
}
/*
- copy a message, allocating new memory for all parts
+ shallow copy a message - copying only the elements array so that the caller
+ can safely add new elements without changing the message
*/
-struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx,
- const struct ldb_message *msg)
+struct ldb_message *ldb_msg_copy_shallow(TALLOC_CTX *mem_ctx,
+ const struct ldb_message *msg)
{
struct ldb_message *msg2;
- int i, j;
+ int i;
msg2 = talloc(mem_ctx, struct ldb_message);
if (msg2 == NULL) return NULL;
- msg2->elements = NULL;
- msg2->num_elements = 0;
+ *msg2 = *msg;
msg2->private_data = NULL;
- msg2->dn = ldb_dn_copy(msg2, msg->dn);
- if (msg2->dn == NULL) goto failed;
-
- msg2->elements = talloc_array(msg2, struct ldb_message_element, msg->num_elements);
+ msg2->elements = talloc_array(msg2, struct ldb_message_element,
+ msg2->num_elements);
if (msg2->elements == NULL) goto failed;
- for (i=0;i<msg->num_elements;i++) {
- struct ldb_message_element *el1 = &msg->elements[i];
- struct ldb_message_element *el2 = &msg2->elements[i];
+ for (i=0;i<msg2->num_elements;i++) {
+ msg2->elements[i] = msg->elements[i];
+ }
+
+ return msg2;
+
+failed:
+ talloc_free(msg2);
+ return NULL;
+}
+
+
+/*
+ copy a message, allocating new memory for all parts
+*/
+struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx,
+ const struct ldb_message *msg)
+{
+ struct ldb_message *msg2;
+ int i, j;
+
+ msg2 = ldb_msg_copy_shallow(mem_ctx, msg);
+ if (msg2 == NULL) return NULL;
+
+ msg2->dn = ldb_dn_copy(msg2, msg2->dn);
+ if (msg2->dn == NULL) goto failed;
- el2->flags = el1->flags;
- el2->num_values = 0;
- el2->values = NULL;
- el2->name = talloc_strdup(msg2->elements, el1->name);
- if (el2->name == NULL) goto failed;
- el2->values = talloc_array(msg2->elements, struct ldb_val, el1->num_values);
- for (j=0;j<el1->num_values;j++) {
- el2->values[j] = ldb_val_dup(el2->values, &el1->values[j]);
- if (el2->values[j].data == NULL &&
- el1->values[j].length != 0) {
+ for (i=0;i<msg2->num_elements;i++) {
+ struct ldb_message_element *el = &msg2->elements[i];
+ struct ldb_val *values = el->values;
+ el->name = talloc_strdup(msg2->elements, el->name);
+ if (el->name == NULL) goto failed;
+ el->values = talloc_array(msg2->elements, struct ldb_val, el->num_values);
+ for (j=0;j<el->num_values;j++) {
+ el->values[j] = ldb_val_dup(el->values, &values[j]);
+ if (el->values[j].data == NULL && values[j].length != 0) {
goto failed;
}
- el2->num_values++;
}
-
- msg2->num_elements++;
}
return msg2;
return mod;
}
-int ldb_msg_sanity_check(const struct ldb_message *msg)
+int ldb_msg_sanity_check(struct ldb_context *ldb,
+ const struct ldb_message *msg)
{
int i, j;
/* basic check on DN */
if (msg->dn == NULL) {
/* TODO: return also an error string */
+ ldb_set_errstring(ldb, "ldb message lacks a DN!");
return LDB_ERR_INVALID_DN_SYNTAX;
}
if (msg->dn->comp_num == 0) {
/* root dse has empty dn */
- /* TODO: return also an error string */
+ ldb_set_errstring(ldb, "DN on new ldb message is '' (not permitted)!");
return LDB_ERR_ENTRY_ALREADY_EXISTS;
}
for (i = 0; i < msg->num_elements; i++) {
for (j = 0; j < msg->elements[i].num_values; j++) {
if (msg->elements[i].values[j].length == 0) {
+ TALLOC_CTX *mem_ctx = talloc_new(ldb);
/* an attribute cannot be empty */
/* TODO: return also an error string */
+ ldb_asprintf_errstring(ldb, "Element %s has empty attribute in ldb message (%s)!",
+ msg->elements[i].name,
+ ldb_dn_linearize(mem_ctx, msg->dn));
+ talloc_free(mem_ctx);
return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
}
}
}
+/*
+ copy an attribute list. This only copies the array, not the elements
+ (ie. the elements are left as the same pointers)
+*/
+const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *attrs, const char *new_attr)
+{
+ const char **ret;
+ int i;
+ for (i=0;attrs[i];i++) /* noop */ ;
+ ret = talloc_array(mem_ctx, const char *, i+2);
+ if (ret == NULL) {
+ return NULL;
+ }
+ for (i=0;attrs[i];i++) {
+ ret[i] = attrs[i];
+ }
+ ret[i] = new_attr;
+ ret[i+1] = NULL;
+ return ret;
+}
+
+
/*
return 1 if an attribute is in a list of attributes, or 0 otherwise
*/
/*
rename the specified attribute in a search result
*/
-void ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *replace)
+int ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *replace)
{
struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
- if (el != NULL) {
- el->name = replace;
+ if (el == NULL) {
+ return 0;
+ }
+ el->name = talloc_strdup(msg->elements, replace);
+ if (el->name == NULL) {
+ return -1;
}
+ return 0;
}
if (ldb_msg_add(msg, el, 0) != 0) {
return -1;
}
- ldb_msg_rename_attr(msg, attr, replace);
- return 0;
+ return ldb_msg_rename_attr(msg, attr, replace);
+}
+
+
+/*
+ remove the specified attribute in a search result
+*/
+void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr)
+{
+ struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
+ if (el) {
+ int n = (el - msg->elements);
+ if (n != msg->num_elements-1) {
+ memmove(el, el+1, ((msg->num_elements-1) - n)*sizeof(*el));
+ }
+ msg->num_elements--;
+ }
+}
+
+/*
+ return a LDAP formatted time string
+*/
+char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
+{
+ struct tm *tm = gmtime(&t);
+
+ if (!tm) {
+ return NULL;
+ }
+
+ /* formatted like: 20040408072012.0Z */
+ return talloc_asprintf(mem_ctx,
+ "%04u%02u%02u%02u%02u%02u.0Z",
+ tm->tm_year+1900, tm->tm_mon+1,
+ tm->tm_mday, tm->tm_hour, tm->tm_min,
+ tm->tm_sec);
+}
+
+
+/*
+ convert a LDAP time string to a time_t. Return 0 if unable to convert
+*/
+time_t ldb_string_to_time(const char *s)
+{
+ struct tm tm;
+
+ if (s == NULL) return 0;
+
+ memset(&tm, 0, sizeof(tm));
+ if (sscanf(s, "%04u%02u%02u%02u%02u%02u",
+ &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+ &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
+ return 0;
+ }
+ tm.tm_year -= 1900;
+ tm.tm_mon -= 1;
+
+ return timegm(&tm);
+}
+
+
+/*
+ dump a set of results to a file. Useful from within gdb
+*/
+void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *f)
+{
+ int i;
+
+ for (i = 0; i < result->count; i++) {
+ struct ldb_ldif ldif;
+ fprintf(f, "# record %d\n", i+1);
+ ldif.changetype = LDB_CHANGETYPE_NONE;
+ ldif.msg = result->msgs[i];
+ ldb_ldif_write_file(ldb, f, &ldif);
+ }
}
+int ldb_msg_check_string_attribute(const struct ldb_message *msg, const char *name, const char *value)
+{
+ struct ldb_message_element *el;
+ struct ldb_val val;
+
+ el = ldb_msg_find_element(msg, name);
+ if (el == NULL)
+ return 0;
+
+ val.data = discard_const(value);
+ val.length = strlen(value);
+
+ if (ldb_msg_find_val(el, &val))
+ return 1;
+
+ return 0;
+}