r8373: New wildcard matching code.
[metze/samba/wip.git] / source4 / lib / ldb / common / attrib_handlers.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2005
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   attribute handlers for well known attribute types, selected by syntax OID
26   see rfc2252
27 */
28
29 #include "includes.h"
30 #include "ldb/include/ldb.h"
31 #include "ldb/include/ldb_private.h"
32 #include <ctype.h>
33
34 /*
35   default handler that just copies a ldb_val.
36 */
37 int ldb_handler_copy(struct ldb_context *ldb, void *mem_ctx,
38                      const struct ldb_val *in, struct ldb_val *out)
39 {
40         *out = ldb_val_dup(mem_ctx, in);
41         if (out->data == NULL) {
42                 ldb_oom(ldb);
43                 return -1;
44         }
45         return 0;
46 }
47
48 /*
49   a case folding copy handler, removing leading and trailing spaces and
50   multiple internal spaces
51 */
52 static int ldb_handler_fold(struct ldb_context *ldb, void *mem_ctx,
53                             const struct ldb_val *in, struct ldb_val *out)
54 {
55         uint8_t *s1, *s2;
56         out->data = talloc_size(mem_ctx, strlen(in->data)+1);
57         if (out->data == NULL) {
58                 ldb_oom(ldb);
59                 return -1;
60         }
61         s1 = in->data;
62         s2 = out->data;
63         while (*s1 == ' ') s1++;
64         while (*s1) {
65                 *s2 = toupper(*s1);
66                 if (s1[0] == ' ') {
67                         while (s1[0] == s1[1]) s1++;
68                 }
69                 s2++; s1++;
70         }
71         *s2 = 0;
72         out->length = strlen(out->data);
73         return 0;
74 }
75
76
77 /*
78   canonicalise a ldap Integer
79   rfc2252 specifies it should be in decimal form
80 */
81 static int ldb_canonicalise_Integer(struct ldb_context *ldb, void *mem_ctx,
82                                     const struct ldb_val *in, struct ldb_val *out)
83 {
84         char *end;
85         long long i = strtoll(in->data, &end, 0);
86         if (*end != 0) {
87                 return -1;
88         }
89         out->data = talloc_asprintf(mem_ctx, "%lld", i);
90         if (out->data == NULL) {
91                 return -1;
92         }
93         out->length = strlen(out->data);
94         return 0;
95 }
96
97 /*
98   compare two Integers
99 */
100 static int ldb_comparison_Integer(struct ldb_context *ldb, void *mem_ctx,
101                                   const struct ldb_val *v1, const struct ldb_val *v2)
102 {
103         return strtoll(v1->data, NULL, 0) - strtoll(v2->data, NULL, 0);
104 }
105
106 /*
107   compare two binary blobs
108 */
109 int ldb_comparison_binary(struct ldb_context *ldb, void *mem_ctx,
110                           const struct ldb_val *v1, const struct ldb_val *v2)
111 {
112         if (v1->length != v2->length) {
113                 return v1->length - v2->length;
114         }
115         return memcmp(v1->data, v2->data, v1->length);
116 }
117
118 /*
119   compare two case insensitive strings, ignoring multiple whitespace
120   and leading and trailing whitespace
121   see rfc2252 section 8.1
122 */
123 static int ldb_comparison_fold(struct ldb_context *ldb, void *mem_ctx,
124                                const struct ldb_val *v1, const struct ldb_val *v2)
125 {
126         const char *s1=v1->data, *s2=v2->data;
127         while (*s1 == ' ') s1++;
128         while (*s2 == ' ') s2++;
129         /* TODO: make utf8 safe, possibly with helper function from application */
130         while (*s1 && *s2) {
131                 if (toupper(*s1) != toupper(*s2)) break;
132                 if (*s1 == ' ') {
133                         while (s1[0] == s1[1]) s1++;
134                         while (s2[0] == s2[1]) s2++;
135                 }
136                 s1++; s2++;
137         }
138         while (*s1 == ' ') s1++;
139         while (*s2 == ' ') s2++;
140         return (int)(*s1) - (int)(*s2);
141 }
142
143 /*
144   canonicalise a attribute in DN format
145 */
146 static int ldb_canonicalise_dn(struct ldb_context *ldb, void *mem_ctx,
147                                const struct ldb_val *in, struct ldb_val *out)
148 {
149         struct ldb_dn *dn;
150         int ret = -1;
151
152         out->length = 0;
153         out->data = NULL;
154
155         dn = ldb_dn_explode_casefold(ldb, in->data);
156         if (dn == NULL) {
157                 return -1;
158         }
159
160         out->data = ldb_dn_linearize(mem_ctx, dn);
161         if (out->data == NULL) {
162                 goto done;
163         }
164         out->length = strlen(out->data);
165
166         ret = 0;
167
168 done:
169         talloc_free(dn);
170
171         return ret;
172 }
173
174 /*
175   compare two dns
176 */
177 static int ldb_comparison_dn(struct ldb_context *ldb, void *mem_ctx,
178                              const struct ldb_val *v1, const struct ldb_val *v2)
179 {
180         struct ldb_dn *dn1 = NULL, *dn2 = NULL;
181         int ret;
182
183         dn1 = ldb_dn_explode_casefold(mem_ctx, v1->data);
184         if (dn1 == NULL) return -1;
185
186         dn2 = ldb_dn_explode_casefold(mem_ctx, v2->data);
187         if (dn2 == NULL) {
188                 talloc_free(dn1);
189                 return -1;
190         } 
191
192         ret = ldb_dn_compare(ldb, dn1, dn2);
193
194         talloc_free(dn1);
195         talloc_free(dn2);
196         return ret;
197 }
198
199 /*
200   compare two objectclasses, looking at subclasses
201 */
202 static int ldb_comparison_objectclass(struct ldb_context *ldb, void *mem_ctx,
203                                       const struct ldb_val *v1, const struct ldb_val *v2)
204 {
205         int ret, i;
206         const char **subclasses;
207         ret = ldb_comparison_fold(ldb, mem_ctx, v1, v2);
208         if (ret == 0) {
209                 return 0;
210         }
211         subclasses = ldb_subclass_list(ldb, v1->data);
212         if (subclasses == NULL) {
213                 return ret;
214         }
215         for (i=0;subclasses[i];i++) {
216                 struct ldb_val vs;
217                 vs.data = discard_const(subclasses[i]);
218                 vs.length = strlen(subclasses[i]);
219                 if (ldb_comparison_objectclass(ldb, mem_ctx, &vs, v2) == 0) {
220                         return 0;
221                 }
222         }
223         return ret;
224 }
225
226 /*
227   table of standard attribute handlers
228 */
229 static const struct ldb_attrib_handler ldb_standard_attribs[] = {
230         { 
231                 .attr            = LDB_SYNTAX_INTEGER,
232                 .flags           = 0,
233                 .ldif_read_fn    = ldb_handler_copy,
234                 .ldif_write_fn   = ldb_handler_copy,
235                 .canonicalise_fn = ldb_canonicalise_Integer,
236                 .comparison_fn   = ldb_comparison_Integer
237         },
238         { 
239                 .attr            = LDB_SYNTAX_OCTET_STRING,
240                 .flags           = 0,
241                 .ldif_read_fn    = ldb_handler_copy,
242                 .ldif_write_fn   = ldb_handler_copy,
243                 .canonicalise_fn = ldb_handler_copy,
244                 .comparison_fn   = ldb_comparison_binary
245         },
246         { 
247                 .attr            = LDB_SYNTAX_DIRECTORY_STRING,
248                 .flags           = 0,
249                 .ldif_read_fn    = ldb_handler_copy,
250                 .ldif_write_fn   = ldb_handler_copy,
251                 .canonicalise_fn = ldb_handler_fold,
252                 .comparison_fn   = ldb_comparison_fold
253         },
254         { 
255                 .attr            = LDB_SYNTAX_DN,
256                 .flags           = 0,
257                 .ldif_read_fn    = ldb_handler_copy,
258                 .ldif_write_fn   = ldb_handler_copy,
259                 .canonicalise_fn = ldb_canonicalise_dn,
260                 .comparison_fn   = ldb_comparison_dn
261         },
262         { 
263                 .attr            = LDB_SYNTAX_OBJECTCLASS,
264                 .flags           = 0,
265                 .ldif_read_fn    = ldb_handler_copy,
266                 .ldif_write_fn   = ldb_handler_copy,
267                 .canonicalise_fn = ldb_handler_fold,
268                 .comparison_fn   = ldb_comparison_objectclass
269         }
270 };
271
272
273 /*
274   return the attribute handlers for a given syntax name
275 */
276 const struct ldb_attrib_handler *ldb_attrib_handler_syntax(struct ldb_context *ldb,
277                                                            const char *syntax)
278 {
279         int i;
280         unsigned num_handlers = sizeof(ldb_standard_attribs)/sizeof(ldb_standard_attribs[0]);
281         /* TODO: should be replaced with a binary search */
282         for (i=0;i<num_handlers;i++) {
283                 if (strcmp(ldb_standard_attribs[i].attr, syntax) == 0) {
284                         return &ldb_standard_attribs[i];
285                 }
286         }
287         return NULL;
288 }
289