r12746: An initial version of the kludge_acls module.
[jelmer/samba4-debian.git] / source / 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/ldb.h"
37 #include "ldb/include/ldb_errors.h"
38 #include "ldb/include/ldb_private.h"
39 #include <time.h>
40
41 /*
42   create a new ldb_message in a given memory context (NULL for top level)
43 */
44 struct ldb_message *ldb_msg_new(void *mem_ctx)
45 {
46         return talloc_zero(mem_ctx, struct ldb_message);
47 }
48
49 /*
50   find an element in a message by attribute name
51 */
52 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg, 
53                                                  const char *attr_name)
54 {
55         unsigned int i;
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];
59                 }
60         }
61         return NULL;
62 }
63
64 /*
65   see if two ldb_val structures contain exactly the same data
66   return 1 for a match, 0 for a mis-match
67 */
68 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
69 {
70         if (v1->length != v2->length) return 0;
71
72         if (v1->length == 0) return 1;
73
74         if (memcmp(v1->data, v2->data, v1->length) == 0) {
75                 return 1;
76         }
77
78         return 0;
79 }
80
81 /*
82   find a value in an element
83   assumes case sensitive comparison
84 */
85 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el, 
86                                  struct ldb_val *val)
87 {
88         unsigned int i;
89         for (i=0;i<el->num_values;i++) {
90                 if (ldb_val_equal_exact(val, &el->values[i])) {
91                         return &el->values[i];
92                 }
93         }
94         return NULL;
95 }
96
97 /*
98   duplicate a ldb_val structure
99 */
100 struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v)
101 {
102         struct ldb_val v2;
103         v2.length = v->length;
104         if (v->data == NULL) {
105                 v2.data = NULL;
106                 return v2;
107         }
108
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);
112         if (!v2.data) {
113                 v2.length = 0;
114                 return v2;
115         }
116
117         memcpy(v2.data, v->data, v->length);
118         ((char *)v2.data)[v->length] = 0;
119         return v2;
120 }
121
122 /*
123   add an empty element to a message
124 */
125 int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
126 {
127         struct ldb_message_element *els;
128
129         els = talloc_realloc(msg, msg->elements, 
130                              struct ldb_message_element, msg->num_elements+1);
131         if (!els) {
132                 errno = ENOMEM;
133                 return -1;
134         }
135
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) {
141                 return -1;
142         }
143
144         msg->elements = els;
145         msg->num_elements++;
146
147         return 0;
148 }
149
150 /*
151   add an empty element to a message
152 */
153 int ldb_msg_add(struct ldb_message *msg, 
154                 const struct ldb_message_element *el, 
155                 int flags)
156 {
157         if (ldb_msg_add_empty(msg, el->name, flags) != 0) {
158                 return -1;
159         }
160
161         msg->elements[msg->num_elements-1] = *el;
162         msg->elements[msg->num_elements-1].flags = flags;
163
164         return 0;
165 }
166
167 /*
168   add a value to a message
169 */
170 int ldb_msg_add_value(struct ldb_message *msg, 
171                       const char *attr_name,
172                       const struct ldb_val *val)
173 {
174         struct ldb_message_element *el;
175         struct ldb_val *vals;
176
177         el = ldb_msg_find_element(msg, attr_name);
178         if (!el) {
179                 ldb_msg_add_empty(msg, attr_name, 0);
180                 el = ldb_msg_find_element(msg, attr_name);
181         }
182         if (!el) {
183                 return -1;
184         }
185
186         vals = talloc_realloc(msg, el->values, struct ldb_val, el->num_values+1);
187         if (!vals) {
188                 errno = ENOMEM;
189                 return -1;
190         }
191         el->values = vals;
192         el->values[el->num_values] = *val;
193         el->num_values++;
194
195         return 0;
196 }
197
198
199 /*
200   add a string element to a message
201 */
202 int ldb_msg_add_string(struct ldb_message *msg, 
203                        const char *attr_name, const char *str)
204 {
205         struct ldb_val val;
206
207         val.data = discard_const_p(uint8_t, str);
208         val.length = strlen(str);
209
210         return ldb_msg_add_value(msg, attr_name, &val);
211 }
212
213 /*
214   add a printf formatted element to a message
215 */
216 int ldb_msg_add_fmt(struct ldb_message *msg, 
217                     const char *attr_name, const char *fmt, ...)
218 {
219         struct ldb_val val;
220         va_list ap;
221         char *str;
222
223         va_start(ap, fmt);
224         str = talloc_vasprintf(msg, fmt, ap);
225         va_end(ap);
226
227         if (str == NULL) return -1;
228
229         val.data   = (uint8_t *)str;
230         val.length = strlen(str);
231
232         return ldb_msg_add_value(msg, attr_name, &val);
233 }
234
235 /*
236   compare two ldb_message_element structures
237   assumes case senistive comparison
238 */
239 int ldb_msg_element_compare(struct ldb_message_element *el1, 
240                             struct ldb_message_element *el2)
241 {
242         unsigned int i;
243
244         if (el1->num_values != el2->num_values) {
245                 return el1->num_values - el2->num_values;
246         }
247
248         for (i=0;i<el1->num_values;i++) {
249                 if (!ldb_msg_find_val(el2, &el1->values[i])) {
250                         return -1;
251                 }
252         }
253
254         return 0;
255 }
256
257 /*
258   compare two ldb_message_element structures
259   comparing by element name
260 */
261 int ldb_msg_element_compare_name(struct ldb_message_element *el1, 
262                                  struct ldb_message_element *el2)
263 {
264         return ldb_attr_cmp(el1->name, el2->name);
265 }
266
267 /*
268   convenience functions to return common types from a message
269   these return the first value if the attribute is multi-valued
270 */
271 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
272 {
273         struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
274         if (!el || el->num_values == 0) {
275                 return NULL;
276         }
277         return &el->values[0];
278 }
279
280 int ldb_msg_find_int(const struct ldb_message *msg, 
281                      const char *attr_name,
282                      int default_value)
283 {
284         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
285         if (!v || !v->data) {
286                 return default_value;
287         }
288         return strtol((const char *)v->data, NULL, 0);
289 }
290
291 unsigned int ldb_msg_find_uint(const struct ldb_message *msg, 
292                                const char *attr_name,
293                                unsigned int default_value)
294 {
295         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
296         if (!v || !v->data) {
297                 return default_value;
298         }
299         return strtoul((const char *)v->data, NULL, 0);
300 }
301
302 int64_t ldb_msg_find_int64(const struct ldb_message *msg, 
303                            const char *attr_name,
304                            int64_t default_value)
305 {
306         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
307         if (!v || !v->data) {
308                 return default_value;
309         }
310         return strtoll((const char *)v->data, NULL, 0);
311 }
312
313 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg, 
314                              const char *attr_name,
315                              uint64_t default_value)
316 {
317         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
318         if (!v || !v->data) {
319                 return default_value;
320         }
321         return strtoull((const char *)v->data, NULL, 0);
322 }
323
324 double ldb_msg_find_double(const struct ldb_message *msg, 
325                            const char *attr_name,
326                            double default_value)
327 {
328         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
329         if (!v || !v->data) {
330                 return default_value;
331         }
332         return strtod((const char *)v->data, NULL);
333 }
334
335 const char *ldb_msg_find_string(const struct ldb_message *msg, 
336                                 const char *attr_name,
337                                 const char *default_value)
338 {
339         const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
340         if (!v || !v->data) {
341                 return default_value;
342         }
343         return (const char *)v->data;
344 }
345
346 /*
347   sort the elements of a message by name
348 */
349 void ldb_msg_sort_elements(struct ldb_message *msg)
350 {
351         qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element), 
352               (comparison_fn_t)ldb_msg_element_compare_name);
353 }
354
355 /*
356   shallow copy a message - copying only the elements array so that the caller
357   can safely add new elements without changing the message
358 */
359 struct ldb_message *ldb_msg_copy_shallow(TALLOC_CTX *mem_ctx, 
360                                          const struct ldb_message *msg)
361 {
362         struct ldb_message *msg2;
363         int i;
364
365         msg2 = talloc(mem_ctx, struct ldb_message);
366         if (msg2 == NULL) return NULL;
367
368         *msg2 = *msg;
369         msg2->private_data = NULL;
370
371         msg2->elements = talloc_array(msg2, struct ldb_message_element, 
372                                       msg2->num_elements);
373         if (msg2->elements == NULL) goto failed;
374
375         for (i=0;i<msg2->num_elements;i++) {
376                 msg2->elements[i] = msg->elements[i];
377         }
378
379         return msg2;
380
381 failed:
382         talloc_free(msg2);
383         return NULL;
384 }
385
386
387 /*
388   copy a message, allocating new memory for all parts
389 */
390 struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx, 
391                                  const struct ldb_message *msg)
392 {
393         struct ldb_message *msg2;
394         int i, j;
395
396         msg2 = ldb_msg_copy_shallow(mem_ctx, msg);
397         if (msg2 == NULL) return NULL;
398
399         msg2->dn = ldb_dn_copy(msg2, msg2->dn);
400         if (msg2->dn == NULL) goto failed;
401
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) {
411                                 goto failed;
412                         }
413                 }
414         }
415
416         return msg2;
417
418 failed:
419         talloc_free(msg2);
420         return NULL;
421 }
422
423
424 /*
425   canonicalise a message, merging elements of the same name
426 */
427 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, 
428                                          const struct ldb_message *msg)
429 {
430         int i;
431         struct ldb_message *msg2;
432
433         msg2 = ldb_msg_copy(ldb, msg);
434         if (msg2 == NULL) return NULL;
435
436         ldb_msg_sort_elements(msg2);
437
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) {
445                                 return NULL;
446                         }
447                         memcpy(el1->values + el1->num_values,
448                                el2->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)));
455                         }
456                         msg2->num_elements--;
457                         i--;
458                 }
459         }
460
461         return msg2;
462 }
463
464
465 /*
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
468 */
469 struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, 
470                                  struct ldb_message *msg1,
471                                  struct ldb_message *msg2)
472 {
473         struct ldb_message *mod;
474         struct ldb_message_element *el;
475         unsigned int i;
476
477         mod = ldb_msg_new(ldb);
478
479         mod->dn = msg1->dn;
480         mod->num_elements = 0;
481         mod->elements = NULL;
482
483         msg2 = ldb_msg_canonicalize(ldb, msg2);
484         if (msg2 == NULL) {
485                 return NULL;
486         }
487         
488         /* look in msg2 to find elements that need to be added
489            or modified */
490         for (i=0;i<msg2->num_elements;i++) {
491                 el = ldb_msg_find_element(msg1, msg2->elements[i].name);
492
493                 if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
494                         continue;
495                 }
496
497                 if (ldb_msg_add(mod, 
498                                 &msg2->elements[i],
499                                 el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
500                         return NULL;
501                 }
502         }
503
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);
507                 if (!el) {
508                         if (ldb_msg_add_empty(mod, 
509                                               msg1->elements[i].name,
510                                               LDB_FLAG_MOD_DELETE) != 0) {
511                                 return NULL;
512                         }
513                 }
514         }
515
516         return mod;
517 }
518
519 int ldb_msg_sanity_check(const struct ldb_message *msg)
520 {
521         int i, j;
522
523         /* basic check on DN */
524         if (msg->dn == NULL) {
525                 /* TODO: return also an error string */
526                 return LDB_ERR_INVALID_DN_SYNTAX;
527         }
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;
532         }
533
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;
541                         }
542                 }
543         }
544
545         return LDB_SUCCESS;
546 }
547
548
549
550
551 /*
552   copy an attribute list. This only copies the array, not the elements
553   (ie. the elements are left as the same pointers)
554 */
555 const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs)
556 {
557         const char **ret;
558         int i;
559         for (i=0;attrs[i];i++) /* noop */ ;
560         ret = talloc_array(mem_ctx, const char *, i+1);
561         if (ret == NULL) {
562                 return NULL;
563         }
564         for (i=0;attrs[i];i++) {
565                 ret[i] = attrs[i];
566         }
567         ret[i] = attrs[i];
568         return ret;
569 }
570
571
572 /*
573   return 1 if an attribute is in a list of attributes, or 0 otherwise
574 */
575 int ldb_attr_in_list(const char * const *attrs, const char *attr)
576 {
577         int i;
578         for (i=0;attrs[i];i++) {
579                 if (ldb_attr_cmp(attrs[i], attr) == 0) {
580                         return 1;
581                 }
582         }
583         return 0;
584 }
585
586
587 /*
588   rename the specified attribute in a search result
589 */
590 int ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *replace)
591 {
592         struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
593         if (el == NULL) {
594                 return 0;
595         }
596         el->name = talloc_strdup(msg->elements, replace);
597         if (el->name == NULL) {
598                 return -1;
599         }
600         return 0;
601 }
602
603
604 /*
605   copy the specified attribute in a search result to a new attribute
606 */
607 int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *replace)
608 {
609         struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
610         if (el == NULL) {
611                 return 0;
612         }
613         if (ldb_msg_add(msg, el, 0) != 0) {
614                 return -1;
615         }
616         return ldb_msg_rename_attr(msg, attr, replace);
617 }
618
619
620 /*
621   remove the specified attribute in a search result
622 */
623 void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr)
624 {
625         struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
626         if (el) {
627                 int n = (el - msg->elements);
628                 if (n != msg->num_elements-1) {
629                         memmove(el, el+1, ((msg->num_elements-1) - n)*sizeof(*el));
630                 }
631                 msg->num_elements--;
632         }
633 }
634
635 /*
636   return a LDAP formatted time string
637 */
638 char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
639 {
640         struct tm *tm = gmtime(&t);
641
642         if (!tm) {
643                 return NULL;
644         }
645
646         /* formatted like: 20040408072012.0Z */
647         return talloc_asprintf(mem_ctx, 
648                                "%04u%02u%02u%02u%02u%02u.0Z",
649                                tm->tm_year+1900, tm->tm_mon+1,
650                                tm->tm_mday, tm->tm_hour, tm->tm_min,
651                                tm->tm_sec);
652 }
653
654
655 /*
656   convert a LDAP time string to a time_t. Return 0 if unable to convert
657 */
658 time_t ldb_string_to_time(const char *s)
659 {
660         struct tm tm;
661         
662         if (s == NULL) return 0;
663         
664         memset(&tm, 0, sizeof(tm));
665         if (sscanf(s, "%04u%02u%02u%02u%02u%02u", 
666                    &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
667                    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
668                 return 0;
669         }
670         tm.tm_year -= 1900;
671         tm.tm_mon -= 1;
672         
673         return timegm(&tm);
674 }
675
676
677 /*
678   dump a set of results to a file. Useful from within gdb
679 */
680 void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *f)
681 {
682         int i;
683
684         for (i = 0; i < result->count; i++) {
685                 struct ldb_ldif ldif;
686                 fprintf(f, "# record %d\n", i+1);
687                 ldif.changetype = LDB_CHANGETYPE_NONE;
688                 ldif.msg = result->msgs[i];
689                 ldb_ldif_write_file(ldb, f, &ldif);
690         }
691 }
692