ab427762261e800dd234ec9468b602445b9be47d
[obnox/samba/samba-obnox.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 <ldb_module.h>
25 #include "librpc/ndr/libndr.h"
26 #include "libcli/security/dom_sid.h"
27
28 enum dsdb_dn_format dsdb_dn_oid_to_format(const char *oid) 
29 {
30         if (strcmp(oid, LDB_SYNTAX_DN) == 0) {
31                 return DSDB_NORMAL_DN;
32         } else if (strcmp(oid, DSDB_SYNTAX_BINARY_DN) == 0) {
33                 return DSDB_BINARY_DN;
34         } else if (strcmp(oid, DSDB_SYNTAX_STRING_DN) == 0) {
35                 return DSDB_STRING_DN;
36         } else if (strcmp(oid, DSDB_SYNTAX_OR_NAME) == 0) {
37                 return DSDB_NORMAL_DN;
38         } else {
39                 return DSDB_INVALID_DN;
40         }
41 }
42
43 static struct dsdb_dn *dsdb_dn_construct_internal(TALLOC_CTX *mem_ctx, 
44                                                   struct ldb_dn *dn, 
45                                                   DATA_BLOB extra_part, 
46                                                   enum dsdb_dn_format dn_format, 
47                                                   const char *oid) 
48 {
49         struct dsdb_dn *dsdb_dn = talloc(mem_ctx, struct dsdb_dn);
50         if (!dsdb_dn) {
51                 return NULL;
52         }
53         dsdb_dn->dn = talloc_steal(dsdb_dn, dn);
54         dsdb_dn->extra_part = extra_part;
55         dsdb_dn->dn_format = dn_format;
56         /* Look to see if this attributeSyntax is a DN */
57         if (dsdb_dn->dn_format == DSDB_INVALID_DN) {
58                 talloc_free(dsdb_dn);
59                 return NULL;
60         }
61
62         dsdb_dn->oid = oid;
63         talloc_steal(dsdb_dn, extra_part.data);
64         return dsdb_dn;
65 }
66
67 struct dsdb_dn *dsdb_dn_construct(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, DATA_BLOB extra_part, 
68                                   const char *oid) 
69 {
70         enum dsdb_dn_format dn_format = dsdb_dn_oid_to_format(oid);
71         return dsdb_dn_construct_internal(mem_ctx, dn, extra_part, dn_format, oid);
72 }
73
74 struct dsdb_dn *dsdb_dn_parse(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, 
75                               const struct ldb_val *dn_blob, const char *dn_oid)
76 {
77         struct dsdb_dn *dsdb_dn;
78         struct ldb_dn *dn;
79         size_t len;
80         TALLOC_CTX *tmp_ctx;
81         char *p1;
82         char *p2;
83         uint32_t blen;
84         struct ldb_val bval;
85         struct ldb_val dval;
86         char *dn_str;
87
88         enum dsdb_dn_format dn_format = dsdb_dn_oid_to_format(dn_oid);
89
90         if (dn_blob == NULL || dn_blob->data == NULL || dn_blob->length == 0) {
91                 return NULL;
92         }
93
94         switch (dn_format) {
95         case DSDB_INVALID_DN:
96                 return NULL;
97         case DSDB_NORMAL_DN:
98         {
99                 dn = ldb_dn_from_ldb_val(mem_ctx, ldb, dn_blob);
100                 if (!dn || !ldb_dn_validate(dn)) {
101                         talloc_free(dn);
102                         return NULL;
103                 }
104                 return dsdb_dn_construct_internal(mem_ctx, dn, data_blob_null, dn_format, dn_oid);
105         }
106         case DSDB_BINARY_DN:
107                 if (dn_blob->length < 2 || dn_blob->data[0] != 'B' || dn_blob->data[1] != ':') {
108                         return NULL;
109                 }
110                 break;
111         case DSDB_STRING_DN:
112                 if (dn_blob->length < 2 || dn_blob->data[0] != 'S' || dn_blob->data[1] != ':') {
113                         return NULL;
114                 }
115                 break;
116         default:
117                 return NULL;
118         }
119
120         if (strlen((const char*)dn_blob->data) != dn_blob->length) {
121                 /* The RDN must not contain a character with value 0x0 */
122                 return NULL;
123         }
124
125         tmp_ctx = talloc_new(mem_ctx);
126         if (tmp_ctx == NULL) {
127                 return NULL;
128         }
129
130         len = dn_blob->length - 2;
131         p1 = talloc_strndup(tmp_ctx, (const char *)dn_blob->data + 2, len);
132         if (!p1) {
133                 goto failed;
134         }
135
136         errno = 0;
137         blen = strtoul(p1, &p2, 10);
138         if (errno != 0) {
139                 DEBUG(10, (__location__ ": failed\n"));
140                 goto failed;
141         }
142         if (p2 == NULL) {
143                 DEBUG(10, (__location__ ": failed\n"));
144                 goto failed;
145         }
146         if (p2[0] != ':') {
147                 DEBUG(10, (__location__ ": failed\n"));
148                 goto failed;
149         }
150         len -= PTR_DIFF(p2,p1);//???
151         p1 = p2+1;
152         len--;
153                 
154         if (blen >= len) {
155                 DEBUG(10, (__location__ ": blen=%u len=%u\n", (unsigned)blen, (unsigned)len));
156                 goto failed;
157         }
158                 
159         p2 = p1 + blen;
160         if (p2[0] != ':') {
161                 DEBUG(10, (__location__ ": %s", p2));
162                 goto failed;
163         }
164         dn_str = p2+1;
165                 
166                 
167         switch (dn_format) {
168         case DSDB_BINARY_DN:
169                 if ((blen % 2 != 0)) {
170                         DEBUG(10, (__location__ ": blen=%u - not an even number\n", (unsigned)blen));
171                         goto failed;
172                 }
173                 
174                 if (blen >= 2) {
175                         bval.length = (blen/2)+1;
176                         bval.data = talloc_size(tmp_ctx, bval.length);
177                         if (bval.data == NULL) {
178                                 DEBUG(10, (__location__ ": err\n"));
179                                 goto failed;
180                         }
181                         bval.data[bval.length-1] = 0;
182                 
183                         bval.length = strhex_to_str((char *)bval.data, bval.length,
184                                                     p1, blen);
185                         if (bval.length != (blen / 2)) {
186                                 DEBUG(10, (__location__ ": non hexidecimal characters found in binary prefix\n"));
187                                 goto failed;
188                         }
189                 } else {
190                         bval = data_blob_null;
191                 }
192
193                 break;
194         case DSDB_STRING_DN:
195                 bval = data_blob(p1, blen);
196                 break;
197         default:
198                 /* never reached */
199                 return NULL;
200         }
201         
202
203         dval.data = (uint8_t *)dn_str;
204         dval.length = strlen(dn_str);
205                 
206         dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &dval);
207         if (!dn || !ldb_dn_validate(dn)) {
208                 DEBUG(10, (__location__ ": err\n"));
209                 goto failed;
210         }
211                 
212         dsdb_dn = dsdb_dn_construct(mem_ctx, dn, bval, dn_oid);
213                 
214         return dsdb_dn;
215
216 failed:
217         talloc_free(tmp_ctx);
218         return NULL;
219 }
220
221
222 static char *dsdb_dn_get_with_postfix(TALLOC_CTX *mem_ctx, 
223                                      struct dsdb_dn *dsdb_dn,
224                                      const char *postfix)
225 {
226         if (!postfix) {
227                 return NULL;
228         }
229
230         switch (dsdb_dn->dn_format) {
231         case DSDB_NORMAL_DN:
232         {
233                 return talloc_strdup(mem_ctx, postfix);
234         }
235         case DSDB_BINARY_DN:
236         {
237                 char *hexstr = data_blob_hex_string_upper(mem_ctx, &dsdb_dn->extra_part);
238         
239                 char *p = talloc_asprintf(mem_ctx, "B:%u:%s:%s", (unsigned)(dsdb_dn->extra_part.length*2), hexstr, 
240                                           postfix);
241                 talloc_free(hexstr);
242                 return p;
243         }
244         case DSDB_STRING_DN:
245         {
246                 return talloc_asprintf(mem_ctx, "S:%u:%*.*s:%s", 
247                                     (unsigned)(dsdb_dn->extra_part.length), 
248                                     (int)(dsdb_dn->extra_part.length), 
249                                     (int)(dsdb_dn->extra_part.length), 
250                                     (const char *)dsdb_dn->extra_part.data, 
251                                     postfix);
252         }
253         default:
254                 return NULL;
255         }
256 }
257
258 char *dsdb_dn_get_linearized(TALLOC_CTX *mem_ctx, 
259                               struct dsdb_dn *dsdb_dn)
260 {
261         const char *postfix = ldb_dn_get_linearized(dsdb_dn->dn);
262         return dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
263 }
264
265 char *dsdb_dn_get_casefold(TALLOC_CTX *mem_ctx, 
266                            struct dsdb_dn *dsdb_dn) 
267 {
268         const char *postfix = ldb_dn_get_casefold(dsdb_dn->dn);
269         return dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
270 }
271
272 char *dsdb_dn_get_extended_linearized(TALLOC_CTX *mem_ctx, 
273                                       struct dsdb_dn *dsdb_dn,
274                                       int mode)
275 {
276         char *postfix = ldb_dn_get_extended_linearized(mem_ctx, dsdb_dn->dn, mode);
277         char *ret = dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
278         talloc_free(postfix);
279         return ret;
280 }
281
282 int dsdb_dn_binary_canonicalise(struct ldb_context *ldb, void *mem_ctx,
283                                 const struct ldb_val *in, struct ldb_val *out)
284 {
285         struct dsdb_dn *dsdb_dn = dsdb_dn_parse(mem_ctx, ldb, in, DSDB_SYNTAX_BINARY_DN);
286         
287         if (!dsdb_dn) {
288                 return -1;
289         }
290         *out = data_blob_string_const(dsdb_dn_get_casefold(mem_ctx, dsdb_dn));
291         talloc_free(dsdb_dn);
292         if (!out->data) {
293                 return -1;
294         }
295         return 0;
296 }
297
298 int dsdb_dn_binary_comparison(struct ldb_context *ldb, void *mem_ctx,
299                                      const struct ldb_val *v1,
300                                      const struct ldb_val *v2)
301 {
302         return ldb_any_comparison(ldb, mem_ctx, dsdb_dn_binary_canonicalise, v1, v2);
303 }
304
305 int dsdb_dn_string_canonicalise(struct ldb_context *ldb, void *mem_ctx,
306                                 const struct ldb_val *in, struct ldb_val *out)
307 {
308         struct dsdb_dn *dsdb_dn = dsdb_dn_parse(mem_ctx, ldb, in, DSDB_SYNTAX_STRING_DN);
309         
310         if (!dsdb_dn) {
311                 return -1;
312         }
313         *out = data_blob_string_const(dsdb_dn_get_casefold(mem_ctx, dsdb_dn));
314         talloc_free(dsdb_dn);
315         if (!out->data) {
316                 return -1;
317         }
318         return 0;
319 }
320
321 int dsdb_dn_string_comparison(struct ldb_context *ldb, void *mem_ctx,
322                                      const struct ldb_val *v1,
323                                      const struct ldb_val *v2)
324 {
325         return ldb_any_comparison(ldb, mem_ctx, dsdb_dn_string_canonicalise, v1, v2);
326 }
327
328 /*
329   format a drsuapi_DsReplicaObjectIdentifier naming context as a string
330  */
331 char *drs_ObjectIdentifier_to_string(TALLOC_CTX *mem_ctx,
332                                      struct drsuapi_DsReplicaObjectIdentifier *nc)
333 {
334         char *ret = NULL;
335         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
336         if (!GUID_all_zero(&nc->guid)) {
337                 char *guid = GUID_string(tmp_ctx, &nc->guid);
338                 if (guid) {
339                         ret = talloc_asprintf_append(ret, "<GUID=%s>;", guid);
340                 }
341         }
342         if (nc->__ndr_size_sid != 0 && nc->sid.sid_rev_num != 0) {
343                 const char *sid = dom_sid_string(tmp_ctx, &nc->sid);
344                 if (sid) {
345                         ret = talloc_asprintf_append(ret, "<SID=%s>;", sid);
346                 }
347         }
348         if (nc->__ndr_size_dn != 0 && nc->dn) {
349                 ret = talloc_asprintf_append(ret, "%s", nc->dn);
350         }
351         talloc_free(tmp_ctx);
352         talloc_steal(mem_ctx, ret);
353         return ret;
354 }
355
356 struct ldb_dn *drs_ObjectIdentifier_to_dn(TALLOC_CTX *mem_ctx,
357                                           struct ldb_context *ldb,
358                                           struct drsuapi_DsReplicaObjectIdentifier *nc)
359 {
360         char *dn_string = drs_ObjectIdentifier_to_string(mem_ctx, nc);
361         struct ldb_dn *new_dn;
362         new_dn = ldb_dn_new(mem_ctx, ldb, dn_string);
363         talloc_free(dn_string);
364         return new_dn;
365 }