source: trunk/server/source4/dsdb/common/dsdb_dn.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

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