r10914: moved the ldap time string functions into ldb so they can be used by
[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/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   copy a message, allocating new memory for all parts
357 */
358 struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx, 
359                                  const struct ldb_message *msg)
360 {
361         struct ldb_message *msg2;
362         int i, j;
363
364         msg2 = talloc(mem_ctx, struct ldb_message);
365         if (msg2 == NULL) return NULL;
366
367         msg2->elements = NULL;
368         msg2->num_elements = 0;
369         msg2->private_data = NULL;
370
371         msg2->dn = ldb_dn_copy(msg2, msg->dn);
372         if (msg2->dn == NULL) goto failed;
373
374         msg2->elements = talloc_array(msg2, struct ldb_message_element, msg->num_elements);
375         if (msg2->elements == NULL) goto failed;
376
377         for (i=0;i<msg->num_elements;i++) {
378                 struct ldb_message_element *el1 = &msg->elements[i];
379                 struct ldb_message_element *el2 = &msg2->elements[i];
380
381                 el2->flags = el1->flags;
382                 el2->num_values = 0;
383                 el2->values = NULL;
384                 el2->name = talloc_strdup(msg2->elements, el1->name);
385                 if (el2->name == NULL) goto failed;
386                 el2->values = talloc_array(msg2->elements, struct ldb_val, el1->num_values);
387                 for (j=0;j<el1->num_values;j++) {
388                         el2->values[j] = ldb_val_dup(el2->values, &el1->values[j]);
389                         if (el2->values[j].data == NULL &&
390                             el1->values[j].length != 0) {
391                                 goto failed;
392                         }
393                         el2->num_values++;
394                 }
395
396                 msg2->num_elements++;
397         }
398
399         return msg2;
400
401 failed:
402         talloc_free(msg2);
403         return NULL;
404 }
405
406
407 /*
408   canonicalise a message, merging elements of the same name
409 */
410 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, 
411                                          const struct ldb_message *msg)
412 {
413         int i;
414         struct ldb_message *msg2;
415
416         msg2 = ldb_msg_copy(ldb, msg);
417         if (msg2 == NULL) return NULL;
418
419         ldb_msg_sort_elements(msg2);
420
421         for (i=1;i<msg2->num_elements;i++) {
422                 struct ldb_message_element *el1 = &msg2->elements[i-1];
423                 struct ldb_message_element *el2 = &msg2->elements[i];
424                 if (ldb_msg_element_compare_name(el1, el2) == 0) {
425                         el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val, 
426                                                        el1->num_values + el2->num_values);
427                         if (el1->values == NULL) {
428                                 return NULL;
429                         }
430                         memcpy(el1->values + el1->num_values,
431                                el2->values,
432                                sizeof(struct ldb_val) * el2->num_values);
433                         el1->num_values += el2->num_values;
434                         talloc_free(discard_const_p(char, el2->name));
435                         if (i+1<msg2->num_elements) {
436                                 memmove(el2, el2+1, sizeof(struct ldb_message_element) * 
437                                         (msg2->num_elements - (i+1)));
438                         }
439                         msg2->num_elements--;
440                         i--;
441                 }
442         }
443
444         return msg2;
445 }
446
447
448 /*
449   return a ldb_message representing the differences between msg1 and msg2. If you
450   then use this in a ldb_modify() call it can be used to save edits to a message
451 */
452 struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, 
453                                  struct ldb_message *msg1,
454                                  struct ldb_message *msg2)
455 {
456         struct ldb_message *mod;
457         struct ldb_message_element *el;
458         unsigned int i;
459
460         mod = ldb_msg_new(ldb);
461
462         mod->dn = msg1->dn;
463         mod->num_elements = 0;
464         mod->elements = NULL;
465
466         msg2 = ldb_msg_canonicalize(ldb, msg2);
467         if (msg2 == NULL) {
468                 return NULL;
469         }
470         
471         /* look in msg2 to find elements that need to be added
472            or modified */
473         for (i=0;i<msg2->num_elements;i++) {
474                 el = ldb_msg_find_element(msg1, msg2->elements[i].name);
475
476                 if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
477                         continue;
478                 }
479
480                 if (ldb_msg_add(mod, 
481                                 &msg2->elements[i],
482                                 el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
483                         return NULL;
484                 }
485         }
486
487         /* look in msg1 to find elements that need to be deleted */
488         for (i=0;i<msg1->num_elements;i++) {
489                 el = ldb_msg_find_element(msg2, msg1->elements[i].name);
490                 if (!el) {
491                         if (ldb_msg_add_empty(mod, 
492                                               msg1->elements[i].name,
493                                               LDB_FLAG_MOD_DELETE) != 0) {
494                                 return NULL;
495                         }
496                 }
497         }
498
499         return mod;
500 }
501
502 int ldb_msg_sanity_check(const struct ldb_message *msg)
503 {
504         int i, j;
505
506         /* basic check on DN */
507         if (msg->dn == NULL) {
508                 /* TODO: return also an error string */
509                 return LDB_ERR_INVALID_DN_SYNTAX;
510         }
511         if (msg->dn->comp_num == 0) {
512                 /* root dse has empty dn */
513                 /* TODO: return also an error string */
514                 return LDB_ERR_ENTRY_ALREADY_EXISTS;
515         }
516
517         /* basic syntax checks */
518         for (i = 0; i < msg->num_elements; i++) {
519                 for (j = 0; j < msg->elements[i].num_values; j++) {
520                         if (msg->elements[i].values[j].length == 0) {
521                                 /* an attribute cannot be empty */
522                                 /* TODO: return also an error string */
523                                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
524                         }
525                 }
526         }
527
528         return LDB_SUCCESS;
529 }
530
531
532
533
534 /*
535   copy an attribute list. This only copies the array, not the elements
536   (ie. the elements are left as the same pointers)
537 */
538 const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs)
539 {
540         const char **ret;
541         int i;
542         for (i=0;attrs[i];i++) /* noop */ ;
543         ret = talloc_array(mem_ctx, const char *, i+1);
544         if (ret == NULL) {
545                 return NULL;
546         }
547         for (i=0;attrs[i];i++) {
548                 ret[i] = attrs[i];
549         }
550         ret[i] = attrs[i];
551         return ret;
552 }
553
554
555 /*
556   return 1 if an attribute is in a list of attributes, or 0 otherwise
557 */
558 int ldb_attr_in_list(const char * const *attrs, const char *attr)
559 {
560         int i;
561         for (i=0;attrs[i];i++) {
562                 if (ldb_attr_cmp(attrs[i], attr) == 0) {
563                         return 1;
564                 }
565         }
566         return 0;
567 }
568
569
570 /*
571   rename the specified attribute in a search result
572 */
573 void ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *replace)
574 {
575         struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
576         if (el != NULL) {
577                 el->name = replace;
578         }
579 }
580
581
582 /*
583   copy the specified attribute in a search result to a new attribute
584 */
585 int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *replace)
586 {
587         struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
588         if (el == NULL) {
589                 return 0;
590         }
591         if (ldb_msg_add(msg, el, 0) != 0) {
592                 return -1;
593         }
594         ldb_msg_rename_attr(msg, attr, replace);
595         return 0;
596 }
597
598
599 /*
600   return a LDAP formatted time string
601 */
602 char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
603 {
604         struct tm *tm = gmtime(&t);
605
606         if (!tm) {
607                 return NULL;
608         }
609
610         /* formatted like: 20040408072012.0Z */
611         return talloc_asprintf(mem_ctx, 
612                                "%04u%02u%02u%02u%02u%02u.0Z",
613                                tm->tm_year+1900, tm->tm_mon+1,
614                                tm->tm_mday, tm->tm_hour, tm->tm_min,
615                                tm->tm_sec);
616 }
617
618
619 /*
620   convert a LDAP time string to a time_t. Return 0 if unable to convert
621 */
622 time_t ldb_string_to_time(const char *s)
623 {
624         struct tm tm;
625         
626         if (s == NULL) return 0;
627         
628         ZERO_STRUCT(tm);
629         if (sscanf(s, "%04u%02u%02u%02u%02u%02u.0Z", 
630                    &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
631                    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
632                 return 0;
633         }
634         tm.tm_year -= 1900;
635         tm.tm_mon -= 1;
636         
637         return timegm(&tm);
638 }
639