4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb message component utility functions
30 * Description: functions for manipulating ldb_message structures
32 * Author: Andrew Tridgell
36 #include "ldb/include/includes.h"
39 create a new ldb_message in a given memory context (NULL for top level)
41 struct ldb_message *ldb_msg_new(void *mem_ctx)
43 return talloc_zero(mem_ctx, struct ldb_message);
47 find an element in a message by attribute name
49 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg,
50 const char *attr_name)
53 for (i=0;i<msg->num_elements;i++) {
54 if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
55 return &msg->elements[i];
62 see if two ldb_val structures contain exactly the same data
63 return 1 for a match, 0 for a mis-match
65 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
67 if (v1->length != v2->length) return 0;
69 if (v1->length == 0) return 1;
71 if (memcmp(v1->data, v2->data, v1->length) == 0) {
79 find a value in an element
80 assumes case sensitive comparison
82 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
86 for (i=0;i<el->num_values;i++) {
87 if (ldb_val_equal_exact(val, &el->values[i])) {
88 return &el->values[i];
95 duplicate a ldb_val structure
97 struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v)
100 v2.length = v->length;
101 if (v->data == NULL) {
106 /* the +1 is to cope with buggy C library routines like strndup
107 that look one byte beyond */
108 v2.data = talloc_array(mem_ctx, uint8_t, v->length+1);
114 memcpy(v2.data, v->data, v->length);
115 ((char *)v2.data)[v->length] = 0;
120 add an empty element to a message
122 int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
124 struct ldb_message_element *els;
126 if (! ldb_valid_attr_name(attr_name)) {
130 els = talloc_realloc(msg, msg->elements,
131 struct ldb_message_element, msg->num_elements+1);
137 els[msg->num_elements].values = NULL;
138 els[msg->num_elements].num_values = 0;
139 els[msg->num_elements].flags = flags;
140 els[msg->num_elements].name = talloc_strdup(els, attr_name);
141 if (!els[msg->num_elements].name) {
153 add an empty element to a message
155 int ldb_msg_add(struct ldb_message *msg,
156 const struct ldb_message_element *el,
159 if (ldb_msg_add_empty(msg, el->name, flags) != 0) {
163 msg->elements[msg->num_elements-1] = *el;
164 msg->elements[msg->num_elements-1].flags = flags;
170 add a value to a message
172 int ldb_msg_add_value(struct ldb_message *msg,
173 const char *attr_name,
174 const struct ldb_val *val)
176 struct ldb_message_element *el;
177 struct ldb_val *vals;
179 el = ldb_msg_find_element(msg, attr_name);
181 ldb_msg_add_empty(msg, attr_name, 0);
182 el = ldb_msg_find_element(msg, attr_name);
188 vals = talloc_realloc(msg, el->values, struct ldb_val, el->num_values+1);
194 el->values[el->num_values] = *val;
202 add a value to a message, stealing it into the 'right' place
204 int ldb_msg_add_steal_value(struct ldb_message *msg,
205 const char *attr_name,
209 ret = ldb_msg_add_value(msg, attr_name, val);
210 if (ret == LDB_SUCCESS) {
211 struct ldb_message_element *el;
212 el = ldb_msg_find_element(msg, attr_name);
213 talloc_steal(el->values, val->data);
220 add a string element to a message
222 int ldb_msg_add_string(struct ldb_message *msg,
223 const char *attr_name, const char *str)
227 val.data = discard_const_p(uint8_t, str);
228 val.length = strlen(str);
230 return ldb_msg_add_value(msg, attr_name, &val);
234 add a string element to a message, stealing it into the 'right' place
236 int ldb_msg_add_steal_string(struct ldb_message *msg,
237 const char *attr_name, char *str)
241 val.data = (uint8_t *)str;
242 val.length = strlen(str);
244 return ldb_msg_add_steal_value(msg, attr_name, &val);
248 add a printf formatted element to a message
250 int ldb_msg_add_fmt(struct ldb_message *msg,
251 const char *attr_name, const char *fmt, ...)
258 str = talloc_vasprintf(msg, fmt, ap);
261 if (str == NULL) return -1;
263 val.data = (uint8_t *)str;
264 val.length = strlen(str);
266 return ldb_msg_add_steal_value(msg, attr_name, &val);
270 compare two ldb_message_element structures
271 assumes case senistive comparison
273 int ldb_msg_element_compare(struct ldb_message_element *el1,
274 struct ldb_message_element *el2)
278 if (el1->num_values != el2->num_values) {
279 return el1->num_values - el2->num_values;
282 for (i=0;i<el1->num_values;i++) {
283 if (!ldb_msg_find_val(el2, &el1->values[i])) {
292 compare two ldb_message_element structures
293 comparing by element name
295 int ldb_msg_element_compare_name(struct ldb_message_element *el1,
296 struct ldb_message_element *el2)
298 return ldb_attr_cmp(el1->name, el2->name);
302 convenience functions to return common types from a message
303 these return the first value if the attribute is multi-valued
305 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
307 struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
308 if (!el || el->num_values == 0) {
311 return &el->values[0];
314 int ldb_msg_find_int(const struct ldb_message *msg,
315 const char *attr_name,
318 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
319 if (!v || !v->data) {
320 return default_value;
322 return strtol((const char *)v->data, NULL, 0);
325 unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
326 const char *attr_name,
327 unsigned int default_value)
329 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
330 if (!v || !v->data) {
331 return default_value;
333 return strtoul((const char *)v->data, NULL, 0);
336 int64_t ldb_msg_find_int64(const struct ldb_message *msg,
337 const char *attr_name,
338 int64_t default_value)
340 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
341 if (!v || !v->data) {
342 return default_value;
344 return strtoll((const char *)v->data, NULL, 0);
347 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
348 const char *attr_name,
349 uint64_t default_value)
351 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
352 if (!v || !v->data) {
353 return default_value;
355 return strtoull((const char *)v->data, NULL, 0);
358 double ldb_msg_find_double(const struct ldb_message *msg,
359 const char *attr_name,
360 double default_value)
362 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
363 if (!v || !v->data) {
364 return default_value;
366 return strtod((const char *)v->data, NULL);
369 const char *ldb_msg_find_string(const struct ldb_message *msg,
370 const char *attr_name,
371 const char *default_value)
373 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
374 if (!v || !v->data) {
375 return default_value;
377 return (const char *)v->data;
381 sort the elements of a message by name
383 void ldb_msg_sort_elements(struct ldb_message *msg)
385 qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element),
386 (comparison_fn_t)ldb_msg_element_compare_name);
390 shallow copy a message - copying only the elements array so that the caller
391 can safely add new elements without changing the message
393 struct ldb_message *ldb_msg_copy_shallow(TALLOC_CTX *mem_ctx,
394 const struct ldb_message *msg)
396 struct ldb_message *msg2;
399 msg2 = talloc(mem_ctx, struct ldb_message);
400 if (msg2 == NULL) return NULL;
403 msg2->private_data = NULL;
405 msg2->elements = talloc_array(msg2, struct ldb_message_element,
407 if (msg2->elements == NULL) goto failed;
409 for (i=0;i<msg2->num_elements;i++) {
410 msg2->elements[i] = msg->elements[i];
422 copy a message, allocating new memory for all parts
424 struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx,
425 const struct ldb_message *msg)
427 struct ldb_message *msg2;
430 msg2 = ldb_msg_copy_shallow(mem_ctx, msg);
431 if (msg2 == NULL) return NULL;
433 msg2->dn = ldb_dn_copy(msg2, msg2->dn);
434 if (msg2->dn == NULL) goto failed;
436 for (i=0;i<msg2->num_elements;i++) {
437 struct ldb_message_element *el = &msg2->elements[i];
438 struct ldb_val *values = el->values;
439 el->name = talloc_strdup(msg2->elements, el->name);
440 if (el->name == NULL) goto failed;
441 el->values = talloc_array(msg2->elements, struct ldb_val, el->num_values);
442 for (j=0;j<el->num_values;j++) {
443 el->values[j] = ldb_val_dup(el->values, &values[j]);
444 if (el->values[j].data == NULL && values[j].length != 0) {
459 canonicalise a message, merging elements of the same name
461 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
462 const struct ldb_message *msg)
465 struct ldb_message *msg2;
467 msg2 = ldb_msg_copy(ldb, msg);
468 if (msg2 == NULL) return NULL;
470 ldb_msg_sort_elements(msg2);
472 for (i=1;i<msg2->num_elements;i++) {
473 struct ldb_message_element *el1 = &msg2->elements[i-1];
474 struct ldb_message_element *el2 = &msg2->elements[i];
475 if (ldb_msg_element_compare_name(el1, el2) == 0) {
476 el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val,
477 el1->num_values + el2->num_values);
478 if (el1->values == NULL) {
481 memcpy(el1->values + el1->num_values,
483 sizeof(struct ldb_val) * el2->num_values);
484 el1->num_values += el2->num_values;
485 talloc_free(discard_const_p(char, el2->name));
486 if (i+1<msg2->num_elements) {
487 memmove(el2, el2+1, sizeof(struct ldb_message_element) *
488 (msg2->num_elements - (i+1)));
490 msg2->num_elements--;
500 return a ldb_message representing the differences between msg1 and msg2. If you
501 then use this in a ldb_modify() call it can be used to save edits to a message
503 struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
504 struct ldb_message *msg1,
505 struct ldb_message *msg2)
507 struct ldb_message *mod;
508 struct ldb_message_element *el;
511 mod = ldb_msg_new(ldb);
514 mod->num_elements = 0;
515 mod->elements = NULL;
517 msg2 = ldb_msg_canonicalize(ldb, msg2);
522 /* look in msg2 to find elements that need to be added
524 for (i=0;i<msg2->num_elements;i++) {
525 el = ldb_msg_find_element(msg1, msg2->elements[i].name);
527 if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
533 el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
538 /* look in msg1 to find elements that need to be deleted */
539 for (i=0;i<msg1->num_elements;i++) {
540 el = ldb_msg_find_element(msg2, msg1->elements[i].name);
542 if (ldb_msg_add_empty(mod,
543 msg1->elements[i].name,
544 LDB_FLAG_MOD_DELETE) != 0) {
553 int ldb_msg_sanity_check(struct ldb_context *ldb,
554 const struct ldb_message *msg)
558 /* basic check on DN */
559 if (msg->dn == NULL) {
560 /* TODO: return also an error string */
561 ldb_set_errstring(ldb, talloc_strdup(ldb, "ldb message lacks a DN!"));
562 return LDB_ERR_INVALID_DN_SYNTAX;
564 if (msg->dn->comp_num == 0) {
565 /* root dse has empty dn */
566 ldb_set_errstring(ldb, talloc_strdup(ldb, "DN on new ldb message is '' (not permitted)!"));
567 return LDB_ERR_ENTRY_ALREADY_EXISTS;
570 /* basic syntax checks */
571 for (i = 0; i < msg->num_elements; i++) {
572 for (j = 0; j < msg->elements[i].num_values; j++) {
573 if (msg->elements[i].values[j].length == 0) {
574 TALLOC_CTX *mem_ctx = talloc_new(ldb);
575 /* an attribute cannot be empty */
576 /* TODO: return also an error string */
577 ldb_set_errstring(ldb, talloc_asprintf(mem_ctx, "Element %s has empty attribute in ldb message (%s)!",
578 msg->elements[i].name,
579 ldb_dn_linearize(mem_ctx, msg->dn)));
580 talloc_free(mem_ctx);
581 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
593 copy an attribute list. This only copies the array, not the elements
594 (ie. the elements are left as the same pointers)
596 const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs)
600 for (i=0;attrs[i];i++) /* noop */ ;
601 ret = talloc_array(mem_ctx, const char *, i+1);
605 for (i=0;attrs[i];i++) {
614 copy an attribute list. This only copies the array, not the elements
615 (ie. the elements are left as the same pointers)
617 const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *attrs, const char *new_attr)
621 for (i=0;attrs[i];i++) /* noop */ ;
622 ret = talloc_array(mem_ctx, const char *, i+2);
626 for (i=0;attrs[i];i++) {
636 return 1 if an attribute is in a list of attributes, or 0 otherwise
638 int ldb_attr_in_list(const char * const *attrs, const char *attr)
641 for (i=0;attrs[i];i++) {
642 if (ldb_attr_cmp(attrs[i], attr) == 0) {
651 rename the specified attribute in a search result
653 int ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *replace)
655 struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
659 el->name = talloc_strdup(msg->elements, replace);
660 if (el->name == NULL) {
668 copy the specified attribute in a search result to a new attribute
670 int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *replace)
672 struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
676 if (ldb_msg_add(msg, el, 0) != 0) {
679 return ldb_msg_rename_attr(msg, attr, replace);
684 remove the specified attribute in a search result
686 void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr)
688 struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
690 int n = (el - msg->elements);
691 if (n != msg->num_elements-1) {
692 memmove(el, el+1, ((msg->num_elements-1) - n)*sizeof(*el));
699 return a LDAP formatted time string
701 char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
703 struct tm *tm = gmtime(&t);
709 /* formatted like: 20040408072012.0Z */
710 return talloc_asprintf(mem_ctx,
711 "%04u%02u%02u%02u%02u%02u.0Z",
712 tm->tm_year+1900, tm->tm_mon+1,
713 tm->tm_mday, tm->tm_hour, tm->tm_min,
719 convert a LDAP time string to a time_t. Return 0 if unable to convert
721 time_t ldb_string_to_time(const char *s)
725 if (s == NULL) return 0;
727 memset(&tm, 0, sizeof(tm));
728 if (sscanf(s, "%04u%02u%02u%02u%02u%02u",
729 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
730 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
741 dump a set of results to a file. Useful from within gdb
743 void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *f)
747 for (i = 0; i < result->count; i++) {
748 struct ldb_ldif ldif;
749 fprintf(f, "# record %d\n", i+1);
750 ldif.changetype = LDB_CHANGETYPE_NONE;
751 ldif.msg = result->msgs[i];
752 ldb_ldif_write_file(ldb, f, &ldif);
756 int ldb_msg_check_string_attribute(const struct ldb_message *msg, const char *name, const char *value)
758 struct ldb_message_element *el;
761 el = ldb_msg_find_element(msg, name);
765 val.data = discard_const(value);
766 val.length = strlen(value);
768 if (ldb_msg_find_val(el, &val))