r17516: Change helper function names to make more clear what they are meant to do
[kamenim/samba.git] / source4 / lib / ldb / common / ldb_msg.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb message component utility functions
29  *
30  *  Description: functions for manipulating ldb_message structures
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37
38 /*
39   create a new ldb_message in a given memory context (NULL for top level)
40 */
41 struct ldb_message *ldb_msg_new(void *mem_ctx)
42 {
43         return talloc_zero(mem_ctx, struct ldb_message);
44 }
45
46 /*
47   find an element in a message by attribute name
48 */
49 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg, 
50                                                  const char *attr_name)
51 {
52         unsigned int i;
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];
56                 }
57         }
58         return NULL;
59 }
60
61 /*
62   see if two ldb_val structures contain exactly the same data
63   return 1 for a match, 0 for a mis-match
64 */
65 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
66 {
67         if (v1->length != v2->length) return 0;
68
69         if (v1->length == 0) return 1;
70
71         if (memcmp(v1->data, v2->data, v1->length) == 0) {
72                 return 1;
73         }
74
75         return 0;
76 }
77
78 /*
79   find a value in an element
80   assumes case sensitive comparison
81 */
82 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el, 
83                                  struct ldb_val *val)
84 {
85         unsigned int i;
86         for (i=0;i<el->num_values;i++) {
87                 if (ldb_val_equal_exact(val, &el->values[i])) {
88                         return &el->values[i];
89                 }
90         }
91         return NULL;
92 }
93
94 /*
95   duplicate a ldb_val structure
96 */
97 struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v)
98 {
99         struct ldb_val v2;
100         v2.length = v->length;
101         if (v->data == NULL) {
102                 v2.data = NULL;
103                 return v2;
104         }
105
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);
109         if (!v2.data) {
110                 v2.length = 0;
111                 return v2;
112         }
113
114         memcpy(v2.data, v->data, v->length);
115         ((char *)v2.data)[v->length] = 0;
116         return v2;
117 }
118
119 /*
120   add an empty element to a message
121 */
122 int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
123 {
124         struct ldb_message_element *els;
125
126         if (! ldb_valid_attr_name(attr_name)) {
127                 return -1;
128         }
129
130         els = talloc_realloc(msg, msg->elements, 
131                              struct ldb_message_element, msg->num_elements+1);
132         if (!els) {
133                 errno = ENOMEM;
134                 return -1;
135         }
136
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) {
142                 errno = ENOMEM;
143                 return -1;
144         }
145
146         msg->elements = els;
147         msg->num_elements++;
148
149         return 0;
150 }
151
152 /*
153   add an empty element to a message
154 */
155 int ldb_msg_add(struct ldb_message *msg, 
156                 const struct ldb_message_element *el, 
157                 int flags)
158 {
159         if (ldb_msg_add_empty(msg, el->name, flags) != 0) {
160                 return -1;
161         }
162
163         msg->elements[msg->num_elements-1] = *el;
164         msg->elements[msg->num_elements-1].flags = flags;
165
166         return 0;
167 }
168
169 /*
170   add a value to a message
171 */
172 int ldb_msg_add_value(struct ldb_message *msg, 
173                       const char *attr_name,
174                       const struct ldb_val *val)
175 {
176         struct ldb_message_element *el;
177         struct ldb_val *vals;
178
179         el = ldb_msg_find_element(msg, attr_name);
180         if (!el) {
181                 ldb_msg_add_empty(msg, attr_name, 0);
182                 el = ldb_msg_find_element(msg, attr_name);
183         }
184         if (!el) {
185                 return -1;
186         }
187
188         vals = talloc_realloc(msg, el->values, struct ldb_val, el->num_values+1);
189         if (!vals) {
190                 errno = ENOMEM;
191                 return -1;
192         }
193         el->values = vals;
194         el->values[el->num_values] = *val;
195         el->num_values++;
196
197         return 0;
198 }
199
200
201 /*
202   add a value to a message, stealing it into the 'right' place
203 */
204 int ldb_msg_add_steal_value(struct ldb_message *msg, 
205                             const char *attr_name,
206                             struct ldb_val *val)
207 {
208         int ret;
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);
214         }
215         return ret;
216 }
217
218
219 /*
220   add a string element to a message
221 */
222 int ldb_msg_add_string(struct ldb_message *msg, 
223                        const char *attr_name, const char *str)
224 {
225         struct ldb_val val;
226
227         val.data = discard_const_p(uint8_t, str);
228         val.length = strlen(str);
229
230         return ldb_msg_add_value(msg, attr_name, &val);
231 }
232
233 /*
234   add a string element to a message, stealing it into the 'right' place
235 */
236 int ldb_msg_add_steal_string(struct ldb_message *msg, 
237                              const char *attr_name, char *str)
238 {
239         struct ldb_val val;
240
241         val.data = (uint8_t *)str;
242         val.length = strlen(str);
243
244         return ldb_msg_add_steal_value(msg, attr_name, &val);
245 }
246
247 /*
248   add a printf formatted element to a message
249 */
250 int ldb_msg_add_fmt(struct ldb_message *msg, 
251                     const char *attr_name, const char *fmt, ...)
252 {
253         struct ldb_val val;
254         va_list ap;
255         char *str;
256
257         va_start(ap, fmt);
258         str = talloc_vasprintf(msg, fmt, ap);
259         va_end(ap);
260
261         if (str == NULL) return -1;
262
263         val.data   = (uint8_t *)str;
264         val.length = strlen(str);
265
266         return ldb_msg_add_steal_value(msg, attr_name, &val);
267 }
268
269 /*
270   compare two ldb_message_element structures
271   assumes case senistive comparison
272 */
273 int ldb_msg_element_compare(struct ldb_message_element *el1, 
274                             struct ldb_message_element *el2)
275 {
276         unsigned int i;
277
278         if (el1->num_values != el2->num_values) {
279                 return el1->num_values - el2->num_values;
280         }
281
282         for (i=0;i<el1->num_values;i++) {
283                 if (!ldb_msg_find_val(el2, &el1->values[i])) {
284                         return -1;
285                 }
286         }
287
288         return 0;
289 }
290
291 /*
292   compare two ldb_message_element structures
293   comparing by element name
294 */
295 int ldb_msg_element_compare_name(struct ldb_message_element *el1, 
296                                  struct ldb_message_element *el2)
297 {
298         return ldb_attr_cmp(el1->name, el2->name);
299 }
300
301 /*
302   convenience functions to return common types from a message
303   these return the first value if the attribute is multi-valued
304 */
305 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
306 {
307         struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
308         if (!el || el->num_values == 0) {
309                 return NULL;
310         }
311         return &el->values[0];
312 }
313
314 int ldb_msg_find_attr_as_int(const struct ldb_message *msg, 
315                              const char *attr_name,
316                              int default_value)
317 {
318         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
319         if (!v || !v->data) {
320                 return default_value;
321         }
322         return strtol((const char *)v->data, NULL, 0);
323 }
324
325 unsigned int ldb_msg_find_attr_as_uint(const struct ldb_message *msg, 
326                                        const char *attr_name,
327                                        unsigned int default_value)
328 {
329         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
330         if (!v || !v->data) {
331                 return default_value;
332         }
333         return strtoul((const char *)v->data, NULL, 0);
334 }
335
336 int64_t ldb_msg_find_attr_as_int64(const struct ldb_message *msg, 
337                                    const char *attr_name,
338                                    int64_t default_value)
339 {
340         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
341         if (!v || !v->data) {
342                 return default_value;
343         }
344         return strtoll((const char *)v->data, NULL, 0);
345 }
346
347 uint64_t ldb_msg_find_attr_as_uint64(const struct ldb_message *msg, 
348                                      const char *attr_name,
349                                      uint64_t default_value)
350 {
351         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
352         if (!v || !v->data) {
353                 return default_value;
354         }
355         return strtoull((const char *)v->data, NULL, 0);
356 }
357
358 double ldb_msg_find_attr_as_double(const struct ldb_message *msg, 
359                                    const char *attr_name,
360                                    double default_value)
361 {
362         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
363         if (!v || !v->data) {
364                 return default_value;
365         }
366         return strtod((const char *)v->data, NULL);
367 }
368
369 const char *ldb_msg_find_attr_as_string(const struct ldb_message *msg, 
370                                         const char *attr_name,
371                                         const char *default_value)
372 {
373         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
374         if (!v || !v->data) {
375                 return default_value;
376         }
377         return (const char *)v->data;
378 }
379
380 struct ldb_dn *ldb_msg_find_attr_as_dn(void *mem_ctx,
381                                        const struct ldb_message *msg,
382                                        const char *attr_name)
383 {
384         const struct ldb_val *v;
385
386         v = ldb_msg_find_ldb_val(msg, attr_name);
387         if (!v || !v->data) {
388                 return NULL;
389         }
390         return ldb_dn_explode(mem_ctx, (const char *)v->data);
391 }
392
393 /*
394   sort the elements of a message by name
395 */
396 void ldb_msg_sort_elements(struct ldb_message *msg)
397 {
398         qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element), 
399               (comparison_fn_t)ldb_msg_element_compare_name);
400 }
401
402 /*
403   shallow copy a message - copying only the elements array so that the caller
404   can safely add new elements without changing the message
405 */
406 struct ldb_message *ldb_msg_copy_shallow(TALLOC_CTX *mem_ctx, 
407                                          const struct ldb_message *msg)
408 {
409         struct ldb_message *msg2;
410         int i;
411
412         msg2 = talloc(mem_ctx, struct ldb_message);
413         if (msg2 == NULL) return NULL;
414
415         *msg2 = *msg;
416         msg2->private_data = NULL;
417
418         msg2->elements = talloc_array(msg2, struct ldb_message_element, 
419                                       msg2->num_elements);
420         if (msg2->elements == NULL) goto failed;
421
422         for (i=0;i<msg2->num_elements;i++) {
423                 msg2->elements[i] = msg->elements[i];
424         }
425
426         return msg2;
427
428 failed:
429         talloc_free(msg2);
430         return NULL;
431 }
432
433
434 /*
435   copy a message, allocating new memory for all parts
436 */
437 struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx, 
438                                  const struct ldb_message *msg)
439 {
440         struct ldb_message *msg2;
441         int i, j;
442
443         msg2 = ldb_msg_copy_shallow(mem_ctx, msg);
444         if (msg2 == NULL) return NULL;
445
446         msg2->dn = ldb_dn_copy(msg2, msg2->dn);
447         if (msg2->dn == NULL) goto failed;
448
449         for (i=0;i<msg2->num_elements;i++) {
450                 struct ldb_message_element *el = &msg2->elements[i];
451                 struct ldb_val *values = el->values;
452                 el->name = talloc_strdup(msg2->elements, el->name);
453                 if (el->name == NULL) goto failed;
454                 el->values = talloc_array(msg2->elements, struct ldb_val, el->num_values);
455                 for (j=0;j<el->num_values;j++) {
456                         el->values[j] = ldb_val_dup(el->values, &values[j]);
457                         if (el->values[j].data == NULL && values[j].length != 0) {
458                                 goto failed;
459                         }
460                 }
461         }
462
463         return msg2;
464
465 failed:
466         talloc_free(msg2);
467         return NULL;
468 }
469
470
471 /*
472   canonicalise a message, merging elements of the same name
473 */
474 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, 
475                                          const struct ldb_message *msg)
476 {
477         int i;
478         struct ldb_message *msg2;
479
480         msg2 = ldb_msg_copy(ldb, msg);
481         if (msg2 == NULL) return NULL;
482
483         ldb_msg_sort_elements(msg2);
484
485         for (i=1;i<msg2->num_elements;i++) {
486                 struct ldb_message_element *el1 = &msg2->elements[i-1];
487                 struct ldb_message_element *el2 = &msg2->elements[i];
488                 if (ldb_msg_element_compare_name(el1, el2) == 0) {
489                         el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val, 
490                                                        el1->num_values + el2->num_values);
491                         if (el1->values == NULL) {
492                                 return NULL;
493                         }
494                         memcpy(el1->values + el1->num_values,
495                                el2->values,
496                                sizeof(struct ldb_val) * el2->num_values);
497                         el1->num_values += el2->num_values;
498                         talloc_free(discard_const_p(char, el2->name));
499                         if (i+1<msg2->num_elements) {
500                                 memmove(el2, el2+1, sizeof(struct ldb_message_element) * 
501                                         (msg2->num_elements - (i+1)));
502                         }
503                         msg2->num_elements--;
504                         i--;
505                 }
506         }
507
508         return msg2;
509 }
510
511
512 /*
513   return a ldb_message representing the differences between msg1 and msg2. If you
514   then use this in a ldb_modify() call it can be used to save edits to a message
515 */
516 struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, 
517                                  struct ldb_message *msg1,
518                                  struct ldb_message *msg2)
519 {
520         struct ldb_message *mod;
521         struct ldb_message_element *el;
522         unsigned int i;
523
524         mod = ldb_msg_new(ldb);
525
526         mod->dn = msg1->dn;
527         mod->num_elements = 0;
528         mod->elements = NULL;
529
530         msg2 = ldb_msg_canonicalize(ldb, msg2);
531         if (msg2 == NULL) {
532                 return NULL;
533         }
534         
535         /* look in msg2 to find elements that need to be added
536            or modified */
537         for (i=0;i<msg2->num_elements;i++) {
538                 el = ldb_msg_find_element(msg1, msg2->elements[i].name);
539
540                 if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
541                         continue;
542                 }
543
544                 if (ldb_msg_add(mod, 
545                                 &msg2->elements[i],
546                                 el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
547                         return NULL;
548                 }
549         }
550
551         /* look in msg1 to find elements that need to be deleted */
552         for (i=0;i<msg1->num_elements;i++) {
553                 el = ldb_msg_find_element(msg2, msg1->elements[i].name);
554                 if (!el) {
555                         if (ldb_msg_add_empty(mod, 
556                                               msg1->elements[i].name,
557                                               LDB_FLAG_MOD_DELETE) != 0) {
558                                 return NULL;
559                         }
560                 }
561         }
562
563         return mod;
564 }
565
566 int ldb_msg_sanity_check(struct ldb_context *ldb, 
567                          const struct ldb_message *msg)
568 {
569         int i, j;
570
571         /* basic check on DN */
572         if (msg->dn == NULL) {
573                 /* TODO: return also an error string */
574                 ldb_set_errstring(ldb, "ldb message lacks a DN!");
575                 return LDB_ERR_INVALID_DN_SYNTAX;
576         }
577         if (msg->dn->comp_num == 0) {
578                 /* root dse has empty dn */
579                 ldb_set_errstring(ldb, "DN on new ldb message is '' (not permitted)!");
580                 return LDB_ERR_ENTRY_ALREADY_EXISTS;
581         }
582
583         /* basic syntax checks */
584         for (i = 0; i < msg->num_elements; i++) {
585                 for (j = 0; j < msg->elements[i].num_values; j++) {
586                         if (msg->elements[i].values[j].length == 0) {
587                                 TALLOC_CTX *mem_ctx = talloc_new(ldb);
588                                 /* an attribute cannot be empty */
589                                 /* TODO: return also an error string */
590                                 ldb_asprintf_errstring(ldb, "Element %s has empty attribute in ldb message (%s)!",
591                                                             msg->elements[i].name, 
592                                                             ldb_dn_linearize(mem_ctx, msg->dn));
593                                 talloc_free(mem_ctx);
594                                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
595                         }
596                 }
597         }
598
599         return LDB_SUCCESS;
600 }
601
602
603
604
605 /*
606   copy an attribute list. This only copies the array, not the elements
607   (ie. the elements are left as the same pointers)
608 */
609 const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs)
610 {
611         const char **ret;
612         int i;
613         for (i=0;attrs[i];i++) /* noop */ ;
614         ret = talloc_array(mem_ctx, const char *, i+1);
615         if (ret == NULL) {
616                 return NULL;
617         }
618         for (i=0;attrs[i];i++) {
619                 ret[i] = attrs[i];
620         }
621         ret[i] = attrs[i];
622         return ret;
623 }
624
625
626 /*
627   copy an attribute list. This only copies the array, not the elements
628   (ie. the elements are left as the same pointers)
629 */
630 const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *attrs, const char *new_attr)
631 {
632         const char **ret;
633         int i;
634         for (i=0;attrs[i];i++) /* noop */ ;
635         ret = talloc_array(mem_ctx, const char *, i+2);
636         if (ret == NULL) {
637                 return NULL;
638         }
639         for (i=0;attrs[i];i++) {
640                 ret[i] = attrs[i];
641         }
642         ret[i] = new_attr;
643         ret[i+1] = NULL;
644         return ret;
645 }
646
647
648 /*
649   return 1 if an attribute is in a list of attributes, or 0 otherwise
650 */
651 int ldb_attr_in_list(const char * const *attrs, const char *attr)
652 {
653         int i;
654         for (i=0;attrs[i];i++) {
655                 if (ldb_attr_cmp(attrs[i], attr) == 0) {
656                         return 1;
657                 }
658         }
659         return 0;
660 }
661
662
663 /*
664   rename the specified attribute in a search result
665 */
666 int ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *replace)
667 {
668         struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
669         if (el == NULL) {
670                 return 0;
671         }
672         el->name = talloc_strdup(msg->elements, replace);
673         if (el->name == NULL) {
674                 return -1;
675         }
676         return 0;
677 }
678
679
680 /*
681   copy the specified attribute in a search result to a new attribute
682 */
683 int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *replace)
684 {
685         struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
686         if (el == NULL) {
687                 return 0;
688         }
689         if (ldb_msg_add(msg, el, 0) != 0) {
690                 return -1;
691         }
692         return ldb_msg_rename_attr(msg, attr, replace);
693 }
694
695
696 /*
697   remove the specified attribute in a search result
698 */
699 void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr)
700 {
701         struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
702         if (el) {
703                 int n = (el - msg->elements);
704                 if (n != msg->num_elements-1) {
705                         memmove(el, el+1, ((msg->num_elements-1) - n)*sizeof(*el));
706                 }
707                 msg->num_elements--;
708         }
709 }
710
711 /*
712   return a LDAP formatted time string
713 */
714 char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
715 {
716         struct tm *tm = gmtime(&t);
717
718         if (!tm) {
719                 return NULL;
720         }
721
722         /* formatted like: 20040408072012.0Z */
723         return talloc_asprintf(mem_ctx, 
724                                "%04u%02u%02u%02u%02u%02u.0Z",
725                                tm->tm_year+1900, tm->tm_mon+1,
726                                tm->tm_mday, tm->tm_hour, tm->tm_min,
727                                tm->tm_sec);
728 }
729
730
731 /*
732   convert a LDAP time string to a time_t. Return 0 if unable to convert
733 */
734 time_t ldb_string_to_time(const char *s)
735 {
736         struct tm tm;
737         
738         if (s == NULL) return 0;
739         
740         memset(&tm, 0, sizeof(tm));
741         if (sscanf(s, "%04u%02u%02u%02u%02u%02u", 
742                    &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
743                    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
744                 return 0;
745         }
746         tm.tm_year -= 1900;
747         tm.tm_mon -= 1;
748         
749         return timegm(&tm);
750 }
751
752
753 /*
754   dump a set of results to a file. Useful from within gdb
755 */
756 void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *f)
757 {
758         int i;
759
760         for (i = 0; i < result->count; i++) {
761                 struct ldb_ldif ldif;
762                 fprintf(f, "# record %d\n", i+1);
763                 ldif.changetype = LDB_CHANGETYPE_NONE;
764                 ldif.msg = result->msgs[i];
765                 ldb_ldif_write_file(ldb, f, &ldif);
766         }
767 }
768
769 int ldb_msg_check_string_attribute(const struct ldb_message *msg, const char *name, const char *value)
770 {
771         struct ldb_message_element *el;
772         struct ldb_val val;
773         
774         el = ldb_msg_find_element(msg, name);
775         if (el == NULL)
776                 return 0;
777
778         val.data = discard_const(value);
779         val.length = strlen(value);
780
781         if (ldb_msg_find_val(el, &val))
782                 return 1;
783
784         return 0;
785 }