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/ldb.h"
37 #include "ldb/include/ldb_errors.h"
38 #include "ldb/include/ldb_private.h"
42 create a new ldb_message in a given memory context (NULL for top level)
44 struct ldb_message *ldb_msg_new(void *mem_ctx)
46 return talloc_zero(mem_ctx, struct ldb_message);
50 find an element in a message by attribute name
52 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg,
53 const char *attr_name)
56 for (i=0;i<msg->num_elements;i++) {
57 if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
58 return &msg->elements[i];
65 see if two ldb_val structures contain exactly the same data
66 return 1 for a match, 0 for a mis-match
68 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
70 if (v1->length != v2->length) return 0;
72 if (v1->length == 0) return 1;
74 if (memcmp(v1->data, v2->data, v1->length) == 0) {
82 find a value in an element
83 assumes case sensitive comparison
85 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
89 for (i=0;i<el->num_values;i++) {
90 if (ldb_val_equal_exact(val, &el->values[i])) {
91 return &el->values[i];
98 duplicate a ldb_val structure
100 struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v)
103 v2.length = v->length;
104 if (v->data == NULL) {
109 /* the +1 is to cope with buggy C library routines like strndup
110 that look one byte beyond */
111 v2.data = talloc_array(mem_ctx, uint8_t, v->length+1);
117 memcpy(v2.data, v->data, v->length);
118 ((char *)v2.data)[v->length] = 0;
123 add an empty element to a message
125 int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
127 struct ldb_message_element *els;
129 els = talloc_realloc(msg, msg->elements,
130 struct ldb_message_element, msg->num_elements+1);
136 els[msg->num_elements].values = NULL;
137 els[msg->num_elements].num_values = 0;
138 els[msg->num_elements].flags = flags;
139 els[msg->num_elements].name = talloc_strdup(els, attr_name);
140 if (!els[msg->num_elements].name) {
151 add an empty element to a message
153 int ldb_msg_add(struct ldb_message *msg,
154 const struct ldb_message_element *el,
157 if (ldb_msg_add_empty(msg, el->name, flags) != 0) {
161 msg->elements[msg->num_elements-1] = *el;
162 msg->elements[msg->num_elements-1].flags = flags;
168 add a value to a message
170 int ldb_msg_add_value(struct ldb_message *msg,
171 const char *attr_name,
172 const struct ldb_val *val)
174 struct ldb_message_element *el;
175 struct ldb_val *vals;
177 el = ldb_msg_find_element(msg, attr_name);
179 ldb_msg_add_empty(msg, attr_name, 0);
180 el = ldb_msg_find_element(msg, attr_name);
186 vals = talloc_realloc(msg, el->values, struct ldb_val, el->num_values+1);
192 el->values[el->num_values] = *val;
200 add a string element to a message
202 int ldb_msg_add_string(struct ldb_message *msg,
203 const char *attr_name, const char *str)
207 val.data = discard_const_p(uint8_t, str);
208 val.length = strlen(str);
210 return ldb_msg_add_value(msg, attr_name, &val);
214 add a printf formatted element to a message
216 int ldb_msg_add_fmt(struct ldb_message *msg,
217 const char *attr_name, const char *fmt, ...)
224 str = talloc_vasprintf(msg, fmt, ap);
227 if (str == NULL) return -1;
229 val.data = (uint8_t *)str;
230 val.length = strlen(str);
232 return ldb_msg_add_value(msg, attr_name, &val);
236 compare two ldb_message_element structures
237 assumes case senistive comparison
239 int ldb_msg_element_compare(struct ldb_message_element *el1,
240 struct ldb_message_element *el2)
244 if (el1->num_values != el2->num_values) {
245 return el1->num_values - el2->num_values;
248 for (i=0;i<el1->num_values;i++) {
249 if (!ldb_msg_find_val(el2, &el1->values[i])) {
258 compare two ldb_message_element structures
259 comparing by element name
261 int ldb_msg_element_compare_name(struct ldb_message_element *el1,
262 struct ldb_message_element *el2)
264 return ldb_attr_cmp(el1->name, el2->name);
268 convenience functions to return common types from a message
269 these return the first value if the attribute is multi-valued
271 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
273 struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
274 if (!el || el->num_values == 0) {
277 return &el->values[0];
280 int ldb_msg_find_int(const struct ldb_message *msg,
281 const char *attr_name,
284 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
285 if (!v || !v->data) {
286 return default_value;
288 return strtol((const char *)v->data, NULL, 0);
291 unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
292 const char *attr_name,
293 unsigned int default_value)
295 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
296 if (!v || !v->data) {
297 return default_value;
299 return strtoul((const char *)v->data, NULL, 0);
302 int64_t ldb_msg_find_int64(const struct ldb_message *msg,
303 const char *attr_name,
304 int64_t default_value)
306 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
307 if (!v || !v->data) {
308 return default_value;
310 return strtoll((const char *)v->data, NULL, 0);
313 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
314 const char *attr_name,
315 uint64_t default_value)
317 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
318 if (!v || !v->data) {
319 return default_value;
321 return strtoull((const char *)v->data, NULL, 0);
324 double ldb_msg_find_double(const struct ldb_message *msg,
325 const char *attr_name,
326 double default_value)
328 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
329 if (!v || !v->data) {
330 return default_value;
332 return strtod((const char *)v->data, NULL);
335 const char *ldb_msg_find_string(const struct ldb_message *msg,
336 const char *attr_name,
337 const char *default_value)
339 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
340 if (!v || !v->data) {
341 return default_value;
343 return (const char *)v->data;
347 sort the elements of a message by name
349 void ldb_msg_sort_elements(struct ldb_message *msg)
351 qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element),
352 (comparison_fn_t)ldb_msg_element_compare_name);
356 shallow copy a message - copying only the elements array so that the caller
357 can safely add new elements without changing the message
359 struct ldb_message *ldb_msg_copy_shallow(TALLOC_CTX *mem_ctx,
360 const struct ldb_message *msg)
362 struct ldb_message *msg2;
365 msg2 = talloc(mem_ctx, struct ldb_message);
366 if (msg2 == NULL) return NULL;
369 msg2->private_data = NULL;
371 msg2->elements = talloc_array(msg2, struct ldb_message_element,
373 if (msg2->elements == NULL) goto failed;
375 for (i=0;i<msg2->num_elements;i++) {
376 msg2->elements[i] = msg->elements[i];
388 copy a message, allocating new memory for all parts
390 struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx,
391 const struct ldb_message *msg)
393 struct ldb_message *msg2;
396 msg2 = ldb_msg_copy_shallow(mem_ctx, msg);
397 if (msg2 == NULL) return NULL;
399 msg2->dn = ldb_dn_copy(msg2, msg2->dn);
400 if (msg2->dn == NULL) goto failed;
402 for (i=0;i<msg2->num_elements;i++) {
403 struct ldb_message_element *el = &msg2->elements[i];
404 struct ldb_val *values = el->values;
405 el->name = talloc_strdup(msg2->elements, el->name);
406 if (el->name == NULL) goto failed;
407 el->values = talloc_array(msg2->elements, struct ldb_val, el->num_values);
408 for (j=0;j<el->num_values;j++) {
409 el->values[j] = ldb_val_dup(el->values, &values[j]);
410 if (el->values[j].data == NULL && values[j].length != 0) {
425 canonicalise a message, merging elements of the same name
427 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
428 const struct ldb_message *msg)
431 struct ldb_message *msg2;
433 msg2 = ldb_msg_copy(ldb, msg);
434 if (msg2 == NULL) return NULL;
436 ldb_msg_sort_elements(msg2);
438 for (i=1;i<msg2->num_elements;i++) {
439 struct ldb_message_element *el1 = &msg2->elements[i-1];
440 struct ldb_message_element *el2 = &msg2->elements[i];
441 if (ldb_msg_element_compare_name(el1, el2) == 0) {
442 el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val,
443 el1->num_values + el2->num_values);
444 if (el1->values == NULL) {
447 memcpy(el1->values + el1->num_values,
449 sizeof(struct ldb_val) * el2->num_values);
450 el1->num_values += el2->num_values;
451 talloc_free(discard_const_p(char, el2->name));
452 if (i+1<msg2->num_elements) {
453 memmove(el2, el2+1, sizeof(struct ldb_message_element) *
454 (msg2->num_elements - (i+1)));
456 msg2->num_elements--;
466 return a ldb_message representing the differences between msg1 and msg2. If you
467 then use this in a ldb_modify() call it can be used to save edits to a message
469 struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
470 struct ldb_message *msg1,
471 struct ldb_message *msg2)
473 struct ldb_message *mod;
474 struct ldb_message_element *el;
477 mod = ldb_msg_new(ldb);
480 mod->num_elements = 0;
481 mod->elements = NULL;
483 msg2 = ldb_msg_canonicalize(ldb, msg2);
488 /* look in msg2 to find elements that need to be added
490 for (i=0;i<msg2->num_elements;i++) {
491 el = ldb_msg_find_element(msg1, msg2->elements[i].name);
493 if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
499 el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
504 /* look in msg1 to find elements that need to be deleted */
505 for (i=0;i<msg1->num_elements;i++) {
506 el = ldb_msg_find_element(msg2, msg1->elements[i].name);
508 if (ldb_msg_add_empty(mod,
509 msg1->elements[i].name,
510 LDB_FLAG_MOD_DELETE) != 0) {
519 int ldb_msg_sanity_check(const struct ldb_message *msg)
523 /* basic check on DN */
524 if (msg->dn == NULL) {
525 /* TODO: return also an error string */
526 return LDB_ERR_INVALID_DN_SYNTAX;
528 if (msg->dn->comp_num == 0) {
529 /* root dse has empty dn */
530 /* TODO: return also an error string */
531 return LDB_ERR_ENTRY_ALREADY_EXISTS;
534 /* basic syntax checks */
535 for (i = 0; i < msg->num_elements; i++) {
536 for (j = 0; j < msg->elements[i].num_values; j++) {
537 if (msg->elements[i].values[j].length == 0) {
538 /* an attribute cannot be empty */
539 /* TODO: return also an error string */
540 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
552 copy an attribute list. This only copies the array, not the elements
553 (ie. the elements are left as the same pointers)
555 const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs)
559 for (i=0;attrs[i];i++) /* noop */ ;
560 ret = talloc_array(mem_ctx, const char *, i+1);
564 for (i=0;attrs[i];i++) {
573 return 1 if an attribute is in a list of attributes, or 0 otherwise
575 int ldb_attr_in_list(const char * const *attrs, const char *attr)
578 for (i=0;attrs[i];i++) {
579 if (ldb_attr_cmp(attrs[i], attr) == 0) {
588 rename the specified attribute in a search result
590 void ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *replace)
592 struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
600 copy the specified attribute in a search result to a new attribute
602 int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *replace)
604 struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
608 if (ldb_msg_add(msg, el, 0) != 0) {
611 ldb_msg_rename_attr(msg, attr, replace);
617 return a LDAP formatted time string
619 char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
621 struct tm *tm = gmtime(&t);
627 /* formatted like: 20040408072012.0Z */
628 return talloc_asprintf(mem_ctx,
629 "%04u%02u%02u%02u%02u%02u.0Z",
630 tm->tm_year+1900, tm->tm_mon+1,
631 tm->tm_mday, tm->tm_hour, tm->tm_min,
637 convert a LDAP time string to a time_t. Return 0 if unable to convert
639 time_t ldb_string_to_time(const char *s)
643 if (s == NULL) return 0;
646 if (sscanf(s, "%04u%02u%02u%02u%02u%02u",
647 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
648 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {