s4-dsdb: change dsdb_search_dn_with_deleted() to dsdb_search_dn() with dsdb_flags
[ira/wip.git] / source4 / dsdb / common / dsdb_dn.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4
5    Copyright (C) Andrew Tridgell 2009
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "lib/ldb/include/ldb_module.h"
25
26 enum dsdb_dn_format dsdb_dn_oid_to_format(const char *oid) 
27 {
28         if (strcmp(oid, LDB_SYNTAX_DN) == 0) {
29                 return DSDB_NORMAL_DN;
30         } else if (strcmp(oid, DSDB_SYNTAX_BINARY_DN) == 0) {
31                 return DSDB_BINARY_DN;
32         } else if (strcmp(oid, DSDB_SYNTAX_STRING_DN) == 0) {
33                 return DSDB_STRING_DN;
34         } else if (strcmp(oid, DSDB_SYNTAX_OR_NAME) == 0) {
35                 return DSDB_NORMAL_DN;
36         } else {
37                 return DSDB_INVALID_DN;
38         }
39 }
40
41 static struct dsdb_dn *dsdb_dn_construct_internal(TALLOC_CTX *mem_ctx, 
42                                                   struct ldb_dn *dn, 
43                                                   DATA_BLOB extra_part, 
44                                                   enum dsdb_dn_format dn_format, 
45                                                   const char *oid) 
46 {
47         struct dsdb_dn *dsdb_dn = talloc(mem_ctx, struct dsdb_dn);
48         if (!dsdb_dn) {
49                 return NULL;
50         }
51         dsdb_dn->dn = talloc_steal(dsdb_dn, dn);
52         dsdb_dn->extra_part = extra_part;
53         dsdb_dn->dn_format = dn_format;
54         /* Look to see if this attributeSyntax is a DN */
55         if (dsdb_dn->dn_format == DSDB_INVALID_DN) {
56                 talloc_free(dsdb_dn);
57                 return NULL;
58         }
59
60         dsdb_dn->oid = oid;
61         talloc_steal(dsdb_dn, extra_part.data);
62         return dsdb_dn;
63 }
64
65 struct dsdb_dn *dsdb_dn_construct(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, DATA_BLOB extra_part, 
66                                   const char *oid) 
67 {
68         enum dsdb_dn_format dn_format = dsdb_dn_oid_to_format(oid);
69         return dsdb_dn_construct_internal(mem_ctx, dn, extra_part, dn_format, oid);
70 }
71
72 struct dsdb_dn *dsdb_dn_parse(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, 
73                               const struct ldb_val *dn_blob, const char *dn_oid)
74 {
75         struct dsdb_dn *dsdb_dn;
76         struct ldb_dn *dn;
77         const char *data;
78         size_t len;
79         TALLOC_CTX *tmp_ctx;
80         char *p1;
81         char *p2;
82         uint32_t blen;
83         struct ldb_val bval;
84         struct ldb_val dval;
85         char *dn_str;
86
87         enum dsdb_dn_format dn_format = dsdb_dn_oid_to_format(dn_oid);
88         switch (dn_format) {
89         case DSDB_INVALID_DN:
90                 return NULL;
91         case DSDB_NORMAL_DN:
92         {
93                 dn = ldb_dn_from_ldb_val(mem_ctx, ldb, dn_blob);
94                 if (!dn || !ldb_dn_validate(dn)) {
95                         talloc_free(dn);
96                         return NULL;
97                 }
98                 return dsdb_dn_construct_internal(mem_ctx, dn, data_blob_null, dn_format, dn_oid);
99         }
100         case DSDB_BINARY_DN:
101                 if (dn_blob->length < 2 || dn_blob->data[0] != 'B' || dn_blob->data[1] != ':') {
102                         return NULL;
103                 }
104                 break;
105         case DSDB_STRING_DN:
106                 if (dn_blob->length < 2 || dn_blob->data[0] != 'S' || dn_blob->data[1] != ':') {
107                         return NULL;
108                 }
109                 break;
110         default:
111                 return NULL;
112         }
113
114         if (dn_blob && dn_blob->data
115             && (strlen((const char*)dn_blob->data) != dn_blob->length)) {
116                 /* The RDN must not contain a character with value 0x0 */
117                 return NULL;
118         }
119                 
120         if (!dn_blob->data || dn_blob->length == 0) {
121                 return NULL;
122         }
123                 
124         tmp_ctx = talloc_new(mem_ctx);
125         if (tmp_ctx == NULL) {
126                 return NULL;
127         }
128                 
129         data = (const char *)dn_blob->data;
130
131         len = dn_blob->length - 2;
132         p1 = talloc_strndup(tmp_ctx, (const char *)dn_blob->data + 2, len);
133         if (!p1) {
134                 goto failed;
135         }
136
137         errno = 0;
138         blen = strtoul(p1, &p2, 10);
139         if (errno != 0) {
140                 DEBUG(10, (__location__ ": failed\n"));
141                 goto failed;
142         }
143         if (p2 == NULL) {
144                 DEBUG(10, (__location__ ": failed\n"));
145                 goto failed;
146         }
147         if (p2[0] != ':') {
148                 DEBUG(10, (__location__ ": failed\n"));
149                 goto failed;
150         }
151         len -= PTR_DIFF(p2,p1);//???
152         p1 = p2+1;
153         len--;
154                 
155         if (blen >= len) {
156                 DEBUG(10, (__location__ ": blen=%u len=%u\n", (unsigned)blen, (unsigned)len));
157                 goto failed;
158         }
159                 
160         p2 = p1 + blen;
161         if (p2[0] != ':') {
162                 DEBUG(10, (__location__ ": %s", p2));
163                 goto failed;
164         }
165         dn_str = p2+1;
166                 
167                 
168         switch (dn_format) {
169         case DSDB_BINARY_DN:
170                 if ((blen % 2 != 0)) {
171                         DEBUG(10, (__location__ ": blen=%u - not an even number\n", (unsigned)blen));
172                         goto failed;
173                 }
174                 
175                 if (blen >= 2) {
176                         bval.length = (blen/2)+1;
177                         bval.data = talloc_size(tmp_ctx, bval.length);
178                         if (bval.data == NULL) {
179                                 DEBUG(10, (__location__ ": err\n"));
180                                 goto failed;
181                         }
182                         bval.data[bval.length-1] = 0;
183                 
184                         bval.length = strhex_to_str((char *)bval.data, bval.length,
185                                                     p1, blen);
186                         if (bval.length != (blen / 2)) {
187                                 DEBUG(10, (__location__ ": non hexidecimal characters found in binary prefix\n"));
188                                 goto failed;
189                         }
190                 } else {
191                         bval = data_blob_null;
192                 }
193
194                 break;
195         case DSDB_STRING_DN:
196                 bval = data_blob(p1, blen);
197                 break;
198         default:
199                 /* never reached */
200                 return NULL;
201         }
202         
203
204         dval.data = (uint8_t *)dn_str;
205         dval.length = strlen(dn_str);
206                 
207         dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &dval);
208         if (!dn || !ldb_dn_validate(dn)) {
209                 DEBUG(10, (__location__ ": err\n"));
210                 goto failed;
211         }
212                 
213         dsdb_dn = dsdb_dn_construct(mem_ctx, dn, bval, dn_oid);
214                 
215         return dsdb_dn;
216
217 failed:
218         talloc_free(tmp_ctx);
219         return NULL;
220 }
221
222
223 static char *dsdb_dn_get_with_postfix(TALLOC_CTX *mem_ctx, 
224                                      struct dsdb_dn *dsdb_dn,
225                                      const char *postfix)
226 {
227         if (!postfix) {
228                 return NULL;
229         }
230
231         switch (dsdb_dn->dn_format) {
232         case DSDB_NORMAL_DN:
233         {
234                 return talloc_strdup(mem_ctx, postfix);
235         }
236         case DSDB_BINARY_DN:
237         {
238                 char *hexstr = data_blob_hex_string_upper(mem_ctx, &dsdb_dn->extra_part);
239         
240                 char *p = talloc_asprintf(mem_ctx, "B:%u:%s:%s", (unsigned)(dsdb_dn->extra_part.length*2), hexstr, 
241                                           postfix);
242                 talloc_free(hexstr);
243                 return p;
244         }
245         case DSDB_STRING_DN:
246         {
247                 return talloc_asprintf(mem_ctx, "S:%u:%*.*s:%s", 
248                                     (unsigned)(dsdb_dn->extra_part.length), 
249                                     (int)(dsdb_dn->extra_part.length), 
250                                     (int)(dsdb_dn->extra_part.length), 
251                                     (const char *)dsdb_dn->extra_part.data, 
252                                     postfix);
253         }
254         default:
255                 return NULL;
256         }
257 }
258
259 char *dsdb_dn_get_linearized(TALLOC_CTX *mem_ctx, 
260                               struct dsdb_dn *dsdb_dn)
261 {
262         const char *postfix = ldb_dn_get_linearized(dsdb_dn->dn);
263         return dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
264 }
265
266 char *dsdb_dn_get_casefold(TALLOC_CTX *mem_ctx, 
267                            struct dsdb_dn *dsdb_dn) 
268 {
269         const char *postfix = ldb_dn_get_casefold(dsdb_dn->dn);
270         return dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
271 }
272
273 char *dsdb_dn_get_extended_linearized(TALLOC_CTX *mem_ctx, 
274                                       struct dsdb_dn *dsdb_dn,
275                                       int mode)
276 {
277         char *postfix = ldb_dn_get_extended_linearized(mem_ctx, dsdb_dn->dn, mode);
278         char *ret = dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
279         talloc_free(postfix);
280         return ret;
281 }
282
283 int dsdb_dn_binary_canonicalise(struct ldb_context *ldb, void *mem_ctx,
284                                 const struct ldb_val *in, struct ldb_val *out)
285 {
286         struct dsdb_dn *dsdb_dn = dsdb_dn_parse(mem_ctx, ldb, in, DSDB_SYNTAX_BINARY_DN);
287         
288         if (!dsdb_dn) {
289                 return -1;
290         }
291         *out = data_blob_string_const(dsdb_dn_get_casefold(mem_ctx, dsdb_dn));
292         talloc_free(dsdb_dn);
293         if (!out->data) {
294                 return -1;
295         }
296         return 0;
297 }
298
299 int dsdb_dn_binary_comparison(struct ldb_context *ldb, void *mem_ctx,
300                                      const struct ldb_val *v1,
301                                      const struct ldb_val *v2)
302 {
303         return ldb_any_comparison(ldb, mem_ctx, dsdb_dn_binary_canonicalise, v1, v2);
304 }
305
306 int dsdb_dn_string_canonicalise(struct ldb_context *ldb, void *mem_ctx,
307                                 const struct ldb_val *in, struct ldb_val *out)
308 {
309         struct dsdb_dn *dsdb_dn = dsdb_dn_parse(mem_ctx, ldb, in, DSDB_SYNTAX_STRING_DN);
310         
311         if (!dsdb_dn) {
312                 return -1;
313         }
314         *out = data_blob_string_const(dsdb_dn_get_casefold(mem_ctx, dsdb_dn));
315         talloc_free(dsdb_dn);
316         if (!out->data) {
317                 return -1;
318         }
319         return 0;
320 }
321
322 int dsdb_dn_string_comparison(struct ldb_context *ldb, void *mem_ctx,
323                                      const struct ldb_val *v1,
324                                      const struct ldb_val *v2)
325 {
326         return ldb_any_comparison(ldb, mem_ctx, dsdb_dn_string_canonicalise, v1, v2);
327 }
328
329
330 /*
331    convert a dsdb_dn to a linked attribute data blob
332 */
333 WERROR dsdb_dn_la_to_blob(struct ldb_context *sam_ctx,
334                           const struct dsdb_attribute *schema_attrib,
335                           const struct dsdb_schema *schema,
336                           TALLOC_CTX *mem_ctx,
337                           struct dsdb_dn *dsdb_dn, DATA_BLOB **blob)
338 {
339         struct ldb_val v;
340         WERROR werr;
341         struct ldb_message_element val_el;
342         struct drsuapi_DsReplicaAttribute drs;
343
344         /* we need a message_element with just one value in it */
345         v = data_blob_string_const(dsdb_dn_get_extended_linearized(mem_ctx, dsdb_dn, 1));
346
347         val_el.name = schema_attrib->lDAPDisplayName;
348         val_el.values = &v;
349         val_el.num_values = 1;
350
351         werr = schema_attrib->syntax->ldb_to_drsuapi(sam_ctx, schema, schema_attrib, &val_el, mem_ctx, &drs);
352         W_ERROR_NOT_OK_RETURN(werr);
353
354         if (drs.value_ctr.num_values != 1) {
355                 DEBUG(1,(__location__ ": Failed to build DRS blob for linked attribute %s\n",
356                          schema_attrib->lDAPDisplayName));
357                 return WERR_DS_DRA_INTERNAL_ERROR;
358         }
359
360         *blob = drs.value_ctr.values[0].blob;
361         return WERR_OK;
362 }
363
364 /*
365   convert a data blob to a dsdb_dn
366  */
367 WERROR dsdb_dn_la_from_blob(struct ldb_context *sam_ctx,
368                             const struct dsdb_attribute *schema_attrib,
369                             const struct dsdb_schema *schema,
370                             TALLOC_CTX *mem_ctx,
371                             DATA_BLOB *blob,
372                             struct dsdb_dn **dsdb_dn)
373 {
374         WERROR werr;
375         struct ldb_message_element new_el;
376         struct drsuapi_DsReplicaAttribute drs;
377         struct drsuapi_DsAttributeValue val;
378
379         drs.value_ctr.num_values = 1;
380         drs.value_ctr.values = &val;
381         val.blob = blob;
382
383         werr = schema_attrib->syntax->drsuapi_to_ldb(sam_ctx, schema, schema_attrib, &drs, mem_ctx, &new_el);
384         W_ERROR_NOT_OK_RETURN(werr);
385
386         if (new_el.num_values != 1) {
387                 return WERR_INTERNAL_ERROR;
388         }
389
390         *dsdb_dn = dsdb_dn_parse(mem_ctx, sam_ctx, &new_el.values[0], schema_attrib->syntax->ldap_oid);
391         if (!*dsdb_dn) {
392                 return WERR_INTERNAL_ERROR;
393         }
394
395         return WERR_OK;
396 }