Line data Source code
1 : /*
2 : Unix SMB/CIFS Implementation.
3 : DSDB schema header
4 :
5 : Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
6 : Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
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 :
23 : #include "includes.h"
24 : #include "dsdb/samdb/samdb.h"
25 : #include <ldb_module.h>
26 : #include "lib/util/binsearch.h"
27 : #include "lib/util/tsort.h"
28 : #include "util/dlinklist.h"
29 :
30 : #undef strcasecmp
31 : #undef strncasecmp
32 :
33 : static const char **dsdb_full_attribute_list_internal(TALLOC_CTX *mem_ctx,
34 : const struct dsdb_schema *schema,
35 : const char **class_list,
36 : enum dsdb_attr_list_query query);
37 :
38 456034433 : static int uint32_cmp(uint32_t c1, uint32_t c2)
39 : {
40 456034433 : if (c1 == c2) return 0;
41 407921042 : return c1 > c2 ? 1 : -1;
42 : }
43 :
44 123219419 : static int strcasecmp_with_ldb_val(const struct ldb_val *target, const char *str)
45 : {
46 123219419 : int ret = strncasecmp((const char *)target->data, str, target->length);
47 123219419 : if (ret == 0) {
48 18366450 : size_t len = strlen(str);
49 18366450 : if (target->length > len) {
50 0 : if (target->data[len] == 0) {
51 0 : return 0;
52 : }
53 0 : return 1;
54 : }
55 18366450 : return (target->length - len);
56 : }
57 95257313 : return ret;
58 : }
59 :
60 42741418 : const struct dsdb_attribute *dsdb_attribute_by_attributeID_id(const struct dsdb_schema *schema,
61 : uint32_t id)
62 : {
63 1460877 : struct dsdb_attribute *c;
64 :
65 : /*
66 : * 0xFFFFFFFF is used as value when no mapping table is available,
67 : * so don't try to match with it
68 : */
69 42741418 : if (id == 0xFFFFFFFF) return NULL;
70 :
71 : /* check for msDS-IntId type attribute */
72 42741418 : if (dsdb_pfm_get_attid_type(id) == DSDB_ATTID_TYPE_INTID) {
73 3486 : BINARY_ARRAY_SEARCH_P(schema->attributes_by_msDS_IntId,
74 : schema->num_int_id_attr, msDS_IntId, id, uint32_cmp, c);
75 1247 : return c;
76 : }
77 :
78 418998753 : BINARY_ARRAY_SEARCH_P(schema->attributes_by_attributeID_id,
79 : schema->num_attributes, attributeID_id, id, uint32_cmp, c);
80 41279294 : return c;
81 : }
82 :
83 653 : const struct dsdb_attribute *dsdb_attribute_by_attributeID_oid(const struct dsdb_schema *schema,
84 : const char *oid)
85 : {
86 0 : struct dsdb_attribute *c;
87 :
88 653 : if (!oid) return NULL;
89 :
90 6763 : BINARY_ARRAY_SEARCH_P(schema->attributes_by_attributeID_oid,
91 : schema->num_attributes, attributeID_oid, oid, strcasecmp, c);
92 653 : return c;
93 : }
94 :
95 1789621601 : const struct dsdb_attribute *dsdb_attribute_by_lDAPDisplayName(const struct dsdb_schema *schema,
96 : const char *name)
97 : {
98 57850171 : struct dsdb_attribute *c;
99 :
100 1789621601 : if (!name) return NULL;
101 :
102 17414252264 : BINARY_ARRAY_SEARCH_P(schema->attributes_by_lDAPDisplayName,
103 : schema->num_attributes, lDAPDisplayName, name, strcasecmp, c);
104 1731771430 : return c;
105 : }
106 :
107 0 : const struct dsdb_attribute *dsdb_attribute_by_lDAPDisplayName_ldb_val(const struct dsdb_schema *schema,
108 : const struct ldb_val *name)
109 : {
110 0 : struct dsdb_attribute *a;
111 :
112 0 : if (!name) return NULL;
113 :
114 0 : BINARY_ARRAY_SEARCH_P(schema->attributes_by_lDAPDisplayName,
115 : schema->num_attributes, lDAPDisplayName, name, strcasecmp_with_ldb_val, a);
116 0 : return a;
117 : }
118 :
119 2135598 : const struct dsdb_attribute *dsdb_attribute_by_linkID(const struct dsdb_schema *schema,
120 : int linkID)
121 : {
122 28328 : struct dsdb_attribute *c;
123 :
124 21694895 : BINARY_ARRAY_SEARCH_P(schema->attributes_by_linkID,
125 : schema->num_attributes, linkID, linkID, uint32_cmp, c);
126 2135598 : return c;
127 : }
128 :
129 5176 : const struct dsdb_attribute *dsdb_attribute_by_cn_ldb_val(const struct dsdb_schema *schema,
130 : const struct ldb_val *cn)
131 : {
132 0 : struct dsdb_attribute *c;
133 :
134 46028 : BINARY_ARRAY_SEARCH_P(schema->attributes_by_cn,
135 : schema->num_attributes, cn, cn, strcasecmp_with_ldb_val, c);
136 5176 : return c;
137 : }
138 :
139 3878109 : const struct dsdb_class *dsdb_class_by_governsID_id(const struct dsdb_schema *schema,
140 : uint32_t id)
141 : {
142 357113 : struct dsdb_class *c;
143 :
144 : /*
145 : * 0xFFFFFFFF is used as value when no mapping table is available,
146 : * so don't try to match with it
147 : */
148 3878109 : if (id == 0xFFFFFFFF) return NULL;
149 :
150 30817295 : BINARY_ARRAY_SEARCH_P(schema->classes_by_governsID_id,
151 : schema->num_classes, governsID_id, id, uint32_cmp, c);
152 3520996 : return c;
153 : }
154 :
155 22 : const struct dsdb_class *dsdb_class_by_governsID_oid(const struct dsdb_schema *schema,
156 : const char *oid)
157 : {
158 0 : struct dsdb_class *c;
159 22 : if (!oid) return NULL;
160 152 : BINARY_ARRAY_SEARCH_P(schema->classes_by_governsID_oid,
161 : schema->num_classes, governsID_oid, oid, strcasecmp, c);
162 22 : return c;
163 : }
164 :
165 111593438 : const struct dsdb_class *dsdb_class_by_lDAPDisplayName(const struct dsdb_schema *schema,
166 : const char *name)
167 : {
168 1823751 : struct dsdb_class *c;
169 111593438 : if (!name) return NULL;
170 778723281 : BINARY_ARRAY_SEARCH_P(schema->classes_by_lDAPDisplayName,
171 : schema->num_classes, lDAPDisplayName, name, strcasecmp, c);
172 109769687 : return c;
173 : }
174 :
175 16981223 : const struct dsdb_class *dsdb_class_by_lDAPDisplayName_ldb_val(const struct dsdb_schema *schema,
176 : const struct ldb_val *name)
177 : {
178 1653137 : struct dsdb_class *c;
179 16981223 : if (!name) return NULL;
180 122090774 : BINARY_ARRAY_SEARCH_P(schema->classes_by_lDAPDisplayName,
181 : schema->num_classes, lDAPDisplayName, name, strcasecmp_with_ldb_val, c);
182 15328086 : return c;
183 : }
184 :
185 150208 : const struct dsdb_class *dsdb_class_by_cn_ldb_val(const struct dsdb_schema *schema,
186 : const struct ldb_val *cn)
187 : {
188 9483 : struct dsdb_class *c;
189 150208 : if (!cn) return NULL;
190 1082620 : BINARY_ARRAY_SEARCH_P(schema->classes_by_cn,
191 : schema->num_classes, cn, cn, strcasecmp_with_ldb_val, c);
192 140725 : return c;
193 : }
194 :
195 0 : const char *dsdb_lDAPDisplayName_by_id(const struct dsdb_schema *schema,
196 : uint32_t id)
197 : {
198 0 : const struct dsdb_attribute *a;
199 0 : const struct dsdb_class *c;
200 :
201 0 : a = dsdb_attribute_by_attributeID_id(schema, id);
202 0 : if (a) {
203 0 : return a->lDAPDisplayName;
204 : }
205 :
206 0 : c = dsdb_class_by_governsID_id(schema, id);
207 0 : if (c) {
208 0 : return c->lDAPDisplayName;
209 : }
210 :
211 0 : return NULL;
212 : }
213 :
214 : /**
215 : Return a list of linked attributes, in lDAPDisplayName format.
216 :
217 : This may be used to determine if a modification would require
218 : backlinks to be updated, for example
219 : */
220 :
221 0 : WERROR dsdb_linked_attribute_lDAPDisplayName_list(const struct dsdb_schema *schema, TALLOC_CTX *mem_ctx, const char ***attr_list_ret)
222 : {
223 0 : const char **attr_list = NULL;
224 0 : struct dsdb_attribute *cur;
225 0 : unsigned int i = 0;
226 0 : for (cur = schema->attributes; cur; cur = cur->next) {
227 0 : if (cur->linkID == 0) continue;
228 :
229 0 : attr_list = talloc_realloc(mem_ctx, attr_list, const char *, i+2);
230 0 : if (!attr_list) {
231 0 : return WERR_NOT_ENOUGH_MEMORY;
232 : }
233 0 : attr_list[i] = cur->lDAPDisplayName;
234 0 : i++;
235 : }
236 0 : if (attr_list != NULL && attr_list[i] != NULL) {
237 0 : attr_list[i] = NULL;
238 : }
239 0 : *attr_list_ret = attr_list;
240 0 : return WERR_OK;
241 : }
242 :
243 28183825 : const char **merge_attr_list(TALLOC_CTX *mem_ctx,
244 : const char **attrs, const char * const*new_attrs)
245 : {
246 2349704 : const char **ret_attrs;
247 2349704 : unsigned int i;
248 28183825 : size_t new_len, new_attr_len, orig_len = str_list_length(attrs);
249 28183825 : if (new_attrs == NULL || new_attrs[0] == NULL) {
250 15350999 : return attrs;
251 : }
252 11444222 : new_attr_len = str_list_length(new_attrs);
253 :
254 11444222 : ret_attrs = talloc_realloc(mem_ctx,
255 : attrs, const char *, orig_len + new_attr_len + 1);
256 11444222 : if (ret_attrs) {
257 360706902 : for (i = 0; i < new_attr_len; i++) {
258 349262680 : ret_attrs[orig_len + i] = new_attrs[i];
259 : }
260 11444222 : new_len = orig_len + new_attr_len;
261 :
262 11444222 : ret_attrs[new_len] = NULL;
263 : }
264 :
265 10483122 : return ret_attrs;
266 : }
267 :
268 : /*
269 : Return a merged list of the attributes of exactly one class (not
270 : considering subclasses, auxiliary classes etc)
271 : */
272 :
273 5643579 : const char **dsdb_attribute_list(TALLOC_CTX *mem_ctx, const struct dsdb_class *sclass, enum dsdb_attr_list_query query)
274 : {
275 5643579 : const char **attr_list = NULL;
276 5643579 : switch (query) {
277 2804995 : case DSDB_SCHEMA_ALL_MAY:
278 2804995 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->mayContain);
279 2804995 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->systemMayContain);
280 2804995 : break;
281 :
282 2806642 : case DSDB_SCHEMA_ALL_MUST:
283 2806642 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->mustContain);
284 2806642 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->systemMustContain);
285 2806642 : break;
286 :
287 0 : case DSDB_SCHEMA_SYS_MAY:
288 0 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->systemMayContain);
289 0 : break;
290 :
291 0 : case DSDB_SCHEMA_SYS_MUST:
292 0 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->systemMustContain);
293 0 : break;
294 :
295 0 : case DSDB_SCHEMA_MAY:
296 0 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->mayContain);
297 0 : break;
298 :
299 0 : case DSDB_SCHEMA_MUST:
300 0 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->mustContain);
301 0 : break;
302 :
303 31942 : case DSDB_SCHEMA_ALL:
304 31942 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->mayContain);
305 31942 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->systemMayContain);
306 31942 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->mustContain);
307 31942 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass->systemMustContain);
308 31942 : break;
309 : }
310 5643579 : return attr_list;
311 : }
312 :
313 5610777 : static const char **attribute_list_from_class(TALLOC_CTX *mem_ctx,
314 : const struct dsdb_schema *schema,
315 : const struct dsdb_class *sclass,
316 : enum dsdb_attr_list_query query)
317 : {
318 469668 : const char **this_class_list;
319 469668 : const char **system_recursive_list;
320 469668 : const char **recursive_list;
321 469668 : const char **attr_list;
322 :
323 5610777 : this_class_list = dsdb_attribute_list(mem_ctx, sclass, query);
324 :
325 6080445 : recursive_list = dsdb_full_attribute_list_internal(mem_ctx, schema,
326 5610777 : sclass->systemAuxiliaryClass,
327 : query);
328 :
329 6080445 : system_recursive_list = dsdb_full_attribute_list_internal(mem_ctx, schema,
330 5610777 : sclass->auxiliaryClass,
331 : query);
332 :
333 5610777 : attr_list = this_class_list;
334 5610777 : attr_list = merge_attr_list(mem_ctx, attr_list, recursive_list);
335 5610777 : attr_list = merge_attr_list(mem_ctx, attr_list, system_recursive_list);
336 5610777 : return attr_list;
337 : }
338 :
339 : /* Return a full attribute list for a given class list
340 :
341 : Via attribute_list_from_class() this calls itself when recursing on auxiliary classes
342 : */
343 11221554 : static const char **dsdb_full_attribute_list_internal(TALLOC_CTX *mem_ctx,
344 : const struct dsdb_schema *schema,
345 : const char **class_list,
346 : enum dsdb_attr_list_query query)
347 : {
348 939336 : unsigned int i;
349 11221554 : const char **attr_list = NULL;
350 :
351 12525961 : for (i=0; class_list && class_list[i]; i++) {
352 28814 : const char **sclass_list
353 1304407 : = attribute_list_from_class(mem_ctx, schema,
354 1275593 : dsdb_class_by_lDAPDisplayName(schema, class_list[i]),
355 : query);
356 :
357 1304407 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass_list);
358 : }
359 11221554 : return attr_list;
360 : }
361 :
362 : /* Return a full attribute list for a given class list (as a ldb_message_element)
363 :
364 : Using the ldb_message_element ensures we do length-limited
365 : comparisons, rather than casting the possibly-unterminated string
366 :
367 : Via attribute_list_from_class() this calls
368 : dsdb_full_attribute_list_internal() when recursing on auxiliary classes
369 : */
370 1859862 : static const char **dsdb_full_attribute_list_internal_el(TALLOC_CTX *mem_ctx,
371 : const struct dsdb_schema *schema,
372 : const struct ldb_message_element *el,
373 : enum dsdb_attr_list_query query)
374 : {
375 213572 : unsigned int i;
376 1859862 : const char **attr_list = NULL;
377 :
378 6166232 : for (i=0; i < el->num_values; i++) {
379 440854 : const char **sclass_list
380 4306370 : = attribute_list_from_class(mem_ctx, schema,
381 4306370 : dsdb_class_by_lDAPDisplayName_ldb_val(schema, &el->values[i]),
382 : query);
383 :
384 4306370 : attr_list = merge_attr_list(mem_ctx, attr_list, sclass_list);
385 : }
386 1859862 : return attr_list;
387 : }
388 :
389 886831938 : static int qsort_string(const char **s1, const char **s2)
390 : {
391 886831938 : return strcasecmp(*s1, *s2);
392 : }
393 :
394 : /* Helper function to remove duplicates from the attribute list to be returned */
395 1859862 : static const char **dedup_attr_list(const char **attr_list)
396 : {
397 1859862 : size_t new_len = str_list_length(attr_list);
398 : /* Remove duplicates */
399 1859862 : if (new_len > 1) {
400 213572 : size_t i;
401 1859862 : TYPESAFE_QSORT(attr_list, new_len, qsort_string);
402 :
403 163477595 : for (i=1; i < new_len; i++) {
404 161404161 : const char **val1 = &attr_list[i-1];
405 161404161 : const char **val2 = &attr_list[i];
406 161404161 : if (ldb_attr_cmp(*val1, *val2) == 0) {
407 3496250 : memmove(val1, val2, (new_len - i) * sizeof( *attr_list));
408 3496250 : attr_list[new_len-1] = NULL;
409 3496250 : new_len--;
410 3496250 : i--;
411 : }
412 : }
413 : }
414 1859862 : return attr_list;
415 : }
416 :
417 : /* Return a full attribute list for a given class list (as a ldb_message_element)
418 :
419 : Using the ldb_message_element ensures we do length-limited
420 : comparisons, rather than casting the possibly-unterminated string
421 :
422 : The result contains only unique values
423 : */
424 1859862 : const char **dsdb_full_attribute_list(TALLOC_CTX *mem_ctx,
425 : const struct dsdb_schema *schema,
426 : const struct ldb_message_element *class_list,
427 : enum dsdb_attr_list_query query)
428 : {
429 1859862 : const char **attr_list = dsdb_full_attribute_list_internal_el(mem_ctx, schema, class_list, query);
430 1859862 : return dedup_attr_list(attr_list);
431 : }
432 :
433 : /* Return the schemaIDGUID of a class */
434 :
435 0 : const struct GUID *class_schemaid_guid_by_lDAPDisplayName(const struct dsdb_schema *schema,
436 : const char *name)
437 : {
438 0 : const struct dsdb_class *object_class = dsdb_class_by_lDAPDisplayName(schema, name);
439 0 : if (!object_class)
440 0 : return NULL;
441 :
442 0 : return &object_class->schemaIDGUID;
443 : }
444 :
445 0 : const struct GUID *attribute_schemaid_guid_by_lDAPDisplayName(const struct dsdb_schema *schema,
446 : const char *name)
447 : {
448 0 : const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema, name);
449 0 : if (!attr)
450 0 : return NULL;
451 :
452 0 : return &attr->schemaIDGUID;
453 : }
454 :
455 : /*
456 : * Sort a "objectClass" attribute (LDB message element "objectclass_element")
457 : * into correct order and validate that all object classes specified actually
458 : * exist in the schema.
459 : * The output is written in an existing LDB message element
460 : * "out_objectclass_element" where the values will be allocated on "mem_ctx".
461 : */
462 1589004 : int dsdb_sort_objectClass_attr(struct ldb_context *ldb,
463 : const struct dsdb_schema *schema,
464 : const struct ldb_message_element *objectclass_element,
465 : TALLOC_CTX *mem_ctx,
466 : struct ldb_message_element *out_objectclass_element)
467 : {
468 246161 : unsigned int i, lowest;
469 246161 : struct class_list {
470 : struct class_list *prev, *next;
471 : const struct dsdb_class *objectclass;
472 1589004 : } *unsorted = NULL, *sorted = NULL, *current = NULL,
473 1589004 : *poss_parent = NULL, *new_parent = NULL,
474 1589004 : *current_lowest = NULL, *current_lowest_struct = NULL;
475 246161 : struct ldb_message_element *el;
476 246161 : TALLOC_CTX *tmp_mem_ctx;
477 :
478 1589004 : tmp_mem_ctx = talloc_new(mem_ctx);
479 1589004 : if (tmp_mem_ctx == NULL) {
480 0 : return ldb_oom(ldb);
481 : }
482 :
483 : /*
484 : * DESIGN:
485 : *
486 : * We work on 4 different 'bins' (implemented here as linked lists):
487 : *
488 : * * sorted: the eventual list, in the order we wish to push
489 : * into the database. This is the only ordered list.
490 : *
491 : * * parent_class: The current parent class 'bin' we are
492 : * trying to find subclasses for
493 : *
494 : * * subclass: The subclasses we have found so far
495 : *
496 : * * unsorted: The remaining objectClasses
497 : *
498 : * The process is a matter of filtering objectClasses up from
499 : * unsorted into sorted. Order is irrelevant in the later 3 'bins'.
500 : *
501 : * We start with 'top' (found and promoted to parent_class
502 : * initially). Then we find (in unsorted) all the direct
503 : * subclasses of 'top'. parent_classes is concatenated onto
504 : * the end of 'sorted', and subclass becomes the list in
505 : * parent_class.
506 : *
507 : * We then repeat, until we find no more subclasses. Any left
508 : * over classes are added to the end.
509 : *
510 : */
511 :
512 : /*
513 : * Firstly, dump all the "objectClass" values into the unsorted bin,
514 : * except for 'top', which is special
515 : */
516 4837168 : for (i=0; i < objectclass_element->num_values; i++) {
517 3248166 : current = talloc(tmp_mem_ctx, struct class_list);
518 3248166 : if (!current) {
519 0 : talloc_free(tmp_mem_ctx);
520 0 : return ldb_oom(ldb);
521 : }
522 3248166 : current->objectclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &objectclass_element->values[i]);
523 3248166 : if (!current->objectclass) {
524 2 : ldb_asprintf_errstring(ldb, "objectclass %.*s is not a valid objectClass in schema",
525 2 : (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data);
526 : /* This looks weird, but windows apparently returns this for invalid objectClass values */
527 2 : talloc_free(tmp_mem_ctx);
528 2 : return LDB_ERR_NO_SUCH_ATTRIBUTE;
529 3248164 : } else if (current->objectclass->isDefunct) {
530 0 : ldb_asprintf_errstring(ldb, "objectclass %.*s marked as isDefunct objectClass in schema - not valid for new objects",
531 0 : (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data);
532 : /* This looks weird, but windows apparently returns this for invalid objectClass values */
533 0 : talloc_free(tmp_mem_ctx);
534 0 : return LDB_ERR_NO_SUCH_ATTRIBUTE;
535 : }
536 :
537 : /* Don't add top to list, we will do that later */
538 3248164 : if (ldb_attr_cmp("top", current->objectclass->lDAPDisplayName) != 0) {
539 1989339 : DLIST_ADD_END(unsorted, current);
540 : }
541 : }
542 :
543 :
544 : /* Add top here, to prevent duplicates */
545 1589002 : current = talloc(tmp_mem_ctx, struct class_list);
546 1589002 : current->objectclass = dsdb_class_by_lDAPDisplayName(schema, "top");
547 1589002 : DLIST_ADD_END(sorted, current);
548 :
549 : /* For each object: find parent chain */
550 3403299 : for (current = unsorted; current != NULL; current = current->next) {
551 3820237 : for (poss_parent = unsorted; poss_parent; poss_parent = poss_parent->next) {
552 2163032 : if (ldb_attr_cmp(poss_parent->objectclass->lDAPDisplayName, current->objectclass->subClassOf) == 0) {
553 153411 : break;
554 : }
555 : }
556 : /* If we didn't get to the end of the list, we need to add this parent */
557 1814297 : if (poss_parent || (ldb_attr_cmp("top", current->objectclass->subClassOf) == 0)) {
558 1746320 : continue;
559 : }
560 :
561 67977 : new_parent = talloc(tmp_mem_ctx, struct class_list);
562 67977 : new_parent->objectclass = dsdb_class_by_lDAPDisplayName(schema, current->objectclass->subClassOf);
563 317825 : DLIST_ADD_END(unsorted, new_parent);
564 : }
565 :
566 : /* For each object: order by hierarchy */
567 3697580 : while (unsorted != NULL) {
568 1563593 : lowest = UINT_MAX;
569 1563593 : current_lowest = current_lowest_struct = NULL;
570 3977078 : for (current = unsorted; current != NULL; current = current->next) {
571 2162781 : if (current->objectclass->subClass_order <= lowest) {
572 : /*
573 : * According to MS-ADTS 3.1.1.1.4 structural
574 : * and 88 object classes are always listed after
575 : * the other class types in a subclass hierarchy
576 : */
577 1921770 : if (current->objectclass->objectClassCategory > 1) {
578 15102 : current_lowest = current;
579 : } else {
580 1903869 : current_lowest_struct = current;
581 : }
582 1669887 : lowest = current->objectclass->subClass_order;
583 : }
584 : }
585 1814297 : if (current_lowest == NULL) {
586 1796399 : current_lowest = current_lowest_struct;
587 : }
588 :
589 1814297 : if (current_lowest != NULL) {
590 1814297 : DLIST_REMOVE(unsorted,current_lowest);
591 2311162 : DLIST_ADD_END(sorted,current_lowest);
592 : }
593 : }
594 :
595 : /* Now rebuild the sorted "objectClass" message element */
596 1589002 : el = out_objectclass_element;
597 :
598 1589002 : el->flags = objectclass_element->flags;
599 1589002 : el->name = talloc_strdup(mem_ctx, objectclass_element->name);
600 1589002 : if (el->name == NULL) {
601 0 : talloc_free(tmp_mem_ctx);
602 0 : return ldb_oom(ldb);
603 : }
604 1589002 : el->num_values = 0;
605 1589002 : el->values = NULL;
606 4992301 : for (current = sorted; current != NULL; current = current->next) {
607 3403299 : el->values = talloc_realloc(mem_ctx, el->values,
608 : struct ldb_val, el->num_values + 1);
609 3403299 : if (el->values == NULL) {
610 0 : talloc_free(tmp_mem_ctx);
611 0 : return ldb_oom(ldb);
612 : }
613 3403299 : el->values[el->num_values] = data_blob_string_const(current->objectclass->lDAPDisplayName);
614 :
615 3403299 : ++(el->num_values);
616 : }
617 :
618 1589002 : talloc_free(tmp_mem_ctx);
619 1589002 : return LDB_SUCCESS;
620 : }
|