s4:dsdb Cosmetic fixes found by metze in review of dsdb_dn changes
[Samba/cd1.git] / source4 / dsdb / common / dsdb_dn.c
blobdbc63a93be7cf92cad47428603f8043d9547ae36
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 2009
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
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.
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.
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/>.
22 #include "includes.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "lib/ldb/include/ldb_module.h"
26 enum dsdb_dn_format dsdb_dn_oid_to_format(const char *oid)
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 {
35 return DSDB_INVALID_DN;
39 static struct dsdb_dn *dsdb_dn_construct_internal(TALLOC_CTX *mem_ctx,
40 struct ldb_dn *dn,
41 DATA_BLOB extra_part,
42 enum dsdb_dn_format dn_format,
43 const char *oid)
45 struct dsdb_dn *dsdb_dn = talloc(mem_ctx, struct dsdb_dn);
46 if (!dsdb_dn) {
47 return NULL;
49 dsdb_dn->dn = talloc_steal(dsdb_dn, dn);
50 dsdb_dn->extra_part = extra_part;
51 dsdb_dn->dn_format = dn_format;
52 /* Look to see if this attributeSyntax is a DN */
53 if (dsdb_dn->dn_format == DSDB_INVALID_DN) {
54 talloc_free(dsdb_dn);
55 return NULL;
58 dsdb_dn->oid = oid;
59 talloc_steal(dsdb_dn, extra_part.data);
60 return dsdb_dn;
63 struct dsdb_dn *dsdb_dn_construct(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, DATA_BLOB extra_part,
64 const char *oid)
66 enum dsdb_dn_format dn_format = dsdb_dn_oid_to_format(oid);
67 return dsdb_dn_construct_internal(mem_ctx, dn, extra_part, dn_format, oid);
70 struct dsdb_dn *dsdb_dn_parse(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
71 const struct ldb_val *dn_blob, const char *dn_oid)
73 struct dsdb_dn *dsdb_dn;
74 struct ldb_dn *dn;
75 const char *data;
76 size_t len;
77 TALLOC_CTX *tmp_ctx;
78 char *p1;
79 char *p2;
80 uint32_t blen;
81 struct ldb_val bval;
82 struct ldb_val dval;
83 char *dn_str;
85 enum dsdb_dn_format dn_format = dsdb_dn_oid_to_format(dn_oid);
86 switch (dn_format) {
87 case DSDB_INVALID_DN:
88 return NULL;
89 case DSDB_NORMAL_DN:
91 dn = ldb_dn_from_ldb_val(mem_ctx, ldb, dn_blob);
92 if (!dn || !ldb_dn_validate(dn)) {
93 talloc_free(dn);
94 return NULL;
96 return dsdb_dn_construct_internal(mem_ctx, dn, data_blob_null, dn_format, dn_oid);
98 case DSDB_BINARY_DN:
99 if (dn_blob->length < 2 || dn_blob->data[0] != 'B' || dn_blob->data[1] != ':') {
100 return NULL;
102 break;
103 case DSDB_STRING_DN:
104 if (dn_blob->length < 2 || dn_blob->data[0] != 'S' || dn_blob->data[1] != ':') {
105 return NULL;
107 break;
108 default:
109 return NULL;
112 if (dn_blob && dn_blob->data
113 && (strlen((const char*)dn_blob->data) != dn_blob->length)) {
114 /* The RDN must not contain a character with value 0x0 */
115 return NULL;
118 if (!dn_blob->data || dn_blob->length == 0) {
119 return NULL;
122 tmp_ctx = talloc_new(mem_ctx);
123 if (tmp_ctx == NULL) {
124 return NULL;
127 data = (const char *)dn_blob->data;
129 len = dn_blob->length - 2;
130 p1 = talloc_strndup(tmp_ctx, (const char *)dn_blob->data + 2, len);
131 if (!p1) {
132 goto failed;
135 errno = 0;
136 blen = strtoul(p1, &p2, 10);
137 if (errno != 0) {
138 DEBUG(10, (__location__ ": failed\n"));
139 goto failed;
141 if (p2 == NULL) {
142 DEBUG(10, (__location__ ": failed\n"));
143 goto failed;
145 if (p2[0] != ':') {
146 DEBUG(10, (__location__ ": failed\n"));
147 goto failed;
149 len -= PTR_DIFF(p2,p1);//???
150 p1 = p2+1;
151 len--;
153 if (blen >= len) {
154 DEBUG(10, (__location__ ": blen=%u len=%u\n", (unsigned)blen, (unsigned)len));
155 goto failed;
158 p2 = p1 + blen;
159 if (p2[0] != ':') {
160 DEBUG(10, (__location__ ": %s", p2));
161 goto failed;
163 dn_str = p2+1;
166 switch (dn_format) {
167 case DSDB_BINARY_DN:
168 if ((blen % 2 != 0)) {
169 DEBUG(10, (__location__ ": blen=%u - not an even number\n", (unsigned)blen));
170 goto failed;
173 if (blen >= 2) {
174 bval.length = (blen/2)+1;
175 bval.data = talloc_size(tmp_ctx, bval.length);
176 if (bval.data == NULL) {
177 DEBUG(10, (__location__ ": err\n"));
178 goto failed;
180 bval.data[bval.length-1] = 0;
182 bval.length = strhex_to_str((char *)bval.data, bval.length,
183 p1, blen);
184 if (bval.length != (blen / 2)) {
185 DEBUG(10, (__location__ ": non hexidecimal characters found in binary prefix\n"));
186 goto failed;
188 } else {
189 bval = data_blob_null;
192 break;
193 case DSDB_STRING_DN:
194 bval = data_blob(p1, blen);
195 break;
196 default:
197 /* never reached */
198 return NULL;
202 dval.data = (uint8_t *)dn_str;
203 dval.length = strlen(dn_str);
205 dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &dval);
206 if (!dn || !ldb_dn_validate(dn)) {
207 DEBUG(10, (__location__ ": err\n"));
208 goto failed;
211 dsdb_dn = dsdb_dn_construct(mem_ctx, dn, bval, dn_oid);
213 return dsdb_dn;
215 failed:
216 talloc_free(tmp_ctx);
217 return NULL;
221 static char *dsdb_dn_get_with_postfix(TALLOC_CTX *mem_ctx,
222 struct dsdb_dn *dsdb_dn,
223 const char *postfix)
225 if (!postfix) {
226 return NULL;
229 switch (dsdb_dn->dn_format) {
230 case DSDB_NORMAL_DN:
232 return talloc_strdup(mem_ctx, postfix);
234 case DSDB_BINARY_DN:
236 char *hexstr = data_blob_hex_string_upper(mem_ctx, &dsdb_dn->extra_part);
238 char *p = talloc_asprintf(mem_ctx, "B:%u:%s:%s", (unsigned)(dsdb_dn->extra_part.length*2), hexstr,
239 postfix);
240 talloc_free(hexstr);
241 return p;
243 case DSDB_STRING_DN:
245 return talloc_asprintf(mem_ctx, "S:%u:%*.*s:%s",
246 (unsigned)(dsdb_dn->extra_part.length),
247 (int)(dsdb_dn->extra_part.length),
248 (int)(dsdb_dn->extra_part.length),
249 (const char *)dsdb_dn->extra_part.data,
250 postfix);
252 default:
253 return NULL;
257 char *dsdb_dn_get_linearized(TALLOC_CTX *mem_ctx,
258 struct dsdb_dn *dsdb_dn)
260 const char *postfix = ldb_dn_get_linearized(dsdb_dn->dn);
261 return dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
264 char *dsdb_dn_get_casefold(TALLOC_CTX *mem_ctx,
265 struct dsdb_dn *dsdb_dn)
267 const char *postfix = ldb_dn_get_casefold(dsdb_dn->dn);
268 return dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
271 char *dsdb_dn_get_extended_linearized(TALLOC_CTX *mem_ctx,
272 struct dsdb_dn *dsdb_dn,
273 int mode)
275 char *postfix = ldb_dn_get_extended_linearized(mem_ctx, dsdb_dn->dn, mode);
276 char *ret = dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
277 talloc_free(postfix);
278 return ret;
281 int dsdb_dn_binary_canonicalise(struct ldb_context *ldb, void *mem_ctx,
282 const struct ldb_val *in, struct ldb_val *out)
284 struct dsdb_dn *dsdb_dn = dsdb_dn_parse(mem_ctx, ldb, in, DSDB_SYNTAX_BINARY_DN);
286 if (!dsdb_dn) {
287 return -1;
289 *out = data_blob_string_const(dsdb_dn_get_casefold(mem_ctx, dsdb_dn));
290 talloc_free(dsdb_dn);
291 if (!out->data) {
292 return -1;
294 return 0;
297 int dsdb_dn_binary_comparison(struct ldb_context *ldb, void *mem_ctx,
298 const struct ldb_val *v1,
299 const struct ldb_val *v2)
301 return ldb_any_comparison(ldb, mem_ctx, dsdb_dn_binary_canonicalise, v1, v2);
304 int dsdb_dn_string_canonicalise(struct ldb_context *ldb, void *mem_ctx,
305 const struct ldb_val *in, struct ldb_val *out)
307 struct dsdb_dn *dsdb_dn = dsdb_dn_parse(mem_ctx, ldb, in, DSDB_SYNTAX_STRING_DN);
309 if (!dsdb_dn) {
310 return -1;
312 *out = data_blob_string_const(dsdb_dn_get_casefold(mem_ctx, dsdb_dn));
313 talloc_free(dsdb_dn);
314 if (!out->data) {
315 return -1;
317 return 0;
320 int dsdb_dn_string_comparison(struct ldb_context *ldb, void *mem_ctx,
321 const struct ldb_val *v1,
322 const struct ldb_val *v2)
324 return ldb_any_comparison(ldb, mem_ctx, dsdb_dn_string_canonicalise, v1, v2);