s4:dsdb Add new dsdb_dn to handle DN+Binary and DN+String
[Samba/kamenim.git] / source4 / dsdb / common / dsdb_dn.c
blob3605a6a0562311e9ee7419129a607ce81b5e8d3b
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 return dsdb_dn_construct_internal(mem_ctx, dn, extra_part, dsdb_dn_oid_to_format(oid), oid);
69 struct dsdb_dn *dsdb_dn_parse(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
70 const struct ldb_val *dn_blob, const char *dn_oid)
72 struct dsdb_dn *dsdb_dn;
73 struct ldb_dn *dn;
74 const char *data;
75 size_t len;
76 TALLOC_CTX *tmp_ctx;
77 char *p1;
78 char *p2;
79 uint32_t blen;
80 struct ldb_val bval;
81 struct ldb_val dval;
82 char *dn_str;
84 enum dsdb_dn_format dn_format = dsdb_dn_oid_to_format(dn_oid);
85 switch (dn_format) {
86 case DSDB_INVALID_DN:
87 return NULL;
88 case DSDB_NORMAL_DN:
90 dn = ldb_dn_from_ldb_val(mem_ctx, ldb, dn_blob);
91 if (!dn || !ldb_dn_validate(dn)) {
92 talloc_free(dn);
93 return NULL;
95 return dsdb_dn_construct_internal(mem_ctx, dn, data_blob_null, dn_format, dn_oid);
97 case DSDB_BINARY_DN:
98 if (dn_blob->length < 2 || dn_blob->data[0] != 'B' || dn_blob->data[1] != ':') {
99 return NULL;
101 break;
102 case DSDB_STRING_DN:
103 if (dn_blob->length < 2 || dn_blob->data[0] != 'S' || dn_blob->data[1] != ':') {
104 return NULL;
106 break;
107 default:
108 return NULL;
111 if (dn_blob && dn_blob->data
112 && (strlen((const char*)dn_blob->data) != dn_blob->length)) {
113 /* The RDN must not contain a character with value 0x0 */
114 return NULL;
117 if (!dn_blob->data || dn_blob->length == 0) {
118 return NULL;
121 tmp_ctx = talloc_new(mem_ctx);
122 if (tmp_ctx == NULL) {
123 return NULL;
126 data = (const char *)dn_blob->data;
128 len = dn_blob->length - 2;
129 p1 = talloc_strndup(tmp_ctx, (const char *)dn_blob->data + 2, len);
130 if (!p1) {
131 goto failed;
134 errno = 0;
135 blen = strtoul(p1, &p2, 10);
136 if (errno != 0) {
137 DEBUG(10, (__location__ ": failed\n"));
138 goto failed;
140 if (p2 == NULL) {
141 DEBUG(10, (__location__ ": failed\n"));
142 goto failed;
144 if (p2[0] != ':') {
145 DEBUG(10, (__location__ ": failed\n"));
146 goto failed;
148 len -= PTR_DIFF(p2,p1);//???
149 p1 = p2+1;
150 len--;
152 if ((blen % 2 != 0)) {
153 DEBUG(10, (__location__ ": blen=%u - not an even number\n", (unsigned)blen));
154 goto failed;
157 if (blen >= len) {
158 DEBUG(10, (__location__ ": blen=%u len=%u\n", (unsigned)blen, (unsigned)len));
159 goto failed;
162 p2 = p1 + blen;
163 if (p2[0] != ':') {
164 DEBUG(10, (__location__ ": %s", p2));
165 goto failed;
167 dn_str = p2+1;
170 switch (dn_format) {
171 case DSDB_BINARY_DN:
172 if (blen >= 2) {
173 bval.length = (blen/2)+1;
174 bval.data = talloc_size(tmp_ctx, bval.length);
175 if (bval.data == NULL) {
176 DEBUG(10, (__location__ ": err\n"));
177 goto failed;
179 bval.data[bval.length-1] = 0;
181 bval.length = strhex_to_str((char *)bval.data, bval.length,
182 p1, blen);
183 if (bval.length != (blen / 2)) {
184 DEBUG(10, (__location__ ": non hexidecimal characters found in binary prefix\n"));
185 goto failed;
187 } else {
188 bval = data_blob_null;
191 break;
192 case DSDB_STRING_DN:
193 bval = data_blob(p1, blen);
194 break;
195 default:
196 /* never reached */
197 return NULL;
201 dval.data = (uint8_t *)dn_str;
202 dval.length = strlen(dn_str);
204 dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &dval);
205 if (!dn || !ldb_dn_validate(dn)) {
206 DEBUG(10, (__location__ ": err\n"));
207 goto failed;
210 dsdb_dn = dsdb_dn_construct(mem_ctx, dn, bval, dn_oid);
212 return dsdb_dn;
214 failed:
215 talloc_free(tmp_ctx);
216 return NULL;
220 static char *dsdb_dn_get_with_postfix(TALLOC_CTX *mem_ctx,
221 struct dsdb_dn *dsdb_dn,
222 const char *postfix)
224 if (!postfix) {
225 return NULL;
228 switch (dsdb_dn->dn_format) {
229 case DSDB_NORMAL_DN:
231 return talloc_strdup(mem_ctx, postfix);
233 case DSDB_BINARY_DN:
235 char *hexstr = data_blob_hex_string_upper(mem_ctx, &dsdb_dn->extra_part);
237 char *p = talloc_asprintf(mem_ctx, "B:%u:%s:%s", (unsigned)(dsdb_dn->extra_part.length*2), hexstr,
238 postfix);
239 talloc_free(hexstr);
240 return p;
242 case DSDB_STRING_DN:
244 return talloc_asprintf(mem_ctx, "S:%u:%*.*s:%s",
245 (unsigned)(dsdb_dn->extra_part.length),
246 (int)(dsdb_dn->extra_part.length),
247 (int)(dsdb_dn->extra_part.length),
248 (const char *)dsdb_dn->extra_part.data,
249 postfix);
251 default:
252 return NULL;
256 char *dsdb_dn_get_linearized(TALLOC_CTX *mem_ctx,
257 struct dsdb_dn *dsdb_dn)
259 const char *postfix = ldb_dn_get_linearized(dsdb_dn->dn);
260 return dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
263 char *dsdb_dn_get_casefold(TALLOC_CTX *mem_ctx,
264 struct dsdb_dn *dsdb_dn)
266 const char *postfix = ldb_dn_get_casefold(dsdb_dn->dn);
267 return dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
270 char *dsdb_dn_get_extended_linearized(TALLOC_CTX *mem_ctx,
271 struct dsdb_dn *dsdb_dn,
272 int mode)
274 char *postfix = ldb_dn_get_extended_linearized(mem_ctx, dsdb_dn->dn, mode);
275 char *ret = dsdb_dn_get_with_postfix(mem_ctx, dsdb_dn, postfix);
276 talloc_free(postfix);
277 return ret;
280 int dsdb_dn_binary_canonicalise(struct ldb_context *ldb, void *mem_ctx,
281 const struct ldb_val *in, struct ldb_val *out)
283 struct dsdb_dn *dsdb_dn = dsdb_dn_parse(mem_ctx, ldb, in, DSDB_SYNTAX_BINARY_DN);
285 if (!dsdb_dn) {
286 return -1;
288 *out = data_blob_string_const(dsdb_dn_get_casefold(mem_ctx, dsdb_dn));
289 talloc_free(dsdb_dn);
290 if (!out->data) {
291 return -1;
293 return 0;
296 int dsdb_dn_binary_comparison(struct ldb_context *ldb, void *mem_ctx,
297 const struct ldb_val *v1,
298 const struct ldb_val *v2)
300 return ldb_any_comparison(ldb, mem_ctx, dsdb_dn_binary_canonicalise, v1, v2);
303 int dsdb_dn_string_canonicalise(struct ldb_context *ldb, void *mem_ctx,
304 const struct ldb_val *in, struct ldb_val *out)
306 struct dsdb_dn *dsdb_dn = dsdb_dn_parse(mem_ctx, ldb, in, DSDB_SYNTAX_STRING_DN);
308 if (!dsdb_dn) {
309 return -1;
311 *out = data_blob_string_const(dsdb_dn_get_casefold(mem_ctx, dsdb_dn));
312 talloc_free(dsdb_dn);
313 if (!out->data) {
314 return -1;
316 return 0;
319 int dsdb_dn_string_comparison(struct ldb_context *ldb, void *mem_ctx,
320 const struct ldb_val *v1,
321 const struct ldb_val *v2)
323 return ldb_any_comparison(ldb, mem_ctx, dsdb_dn_string_canonicalise, v1, v2);