s4:dsdb/schema: we don't need to use find_syntax_map_by_ad_oid() as the syntax is...
[Samba/nascimento.git] / source4 / dsdb / schema / schema_description.c
blob9e162f28b19177061dde56590cdf3a640638a488
1 /*
2 Unix SMB/CIFS mplementation.
3 Print schema info into string format
5 Copyright (C) Andrew Bartlett 2006-2008
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "dsdb/samdb/samdb.h"
24 #define IF_NULL_FAIL_RET(x) do { \
25 if (!x) { \
26 return NULL; \
27 } \
28 } while (0)
31 char *schema_attribute_description(TALLOC_CTX *mem_ctx,
32 enum dsdb_schema_convert_target target,
33 const char *seperator,
34 const char *oid,
35 const char *name,
36 const char *equality,
37 const char *substring,
38 const char *syntax,
39 bool single_value, bool operational)
41 char *schema_entry = talloc_asprintf(mem_ctx,
42 "(%s%s%s", seperator, oid, seperator);
44 schema_entry = talloc_asprintf_append(schema_entry,
45 "NAME '%s'%s", name, seperator);
46 IF_NULL_FAIL_RET(schema_entry);
48 if (equality) {
49 schema_entry = talloc_asprintf_append(schema_entry,
50 "EQUALITY %s%s", equality, seperator);
51 IF_NULL_FAIL_RET(schema_entry);
53 if (substring) {
54 schema_entry = talloc_asprintf_append(schema_entry,
55 "SUBSTR %s%s", substring, seperator);
56 IF_NULL_FAIL_RET(schema_entry);
59 schema_entry = talloc_asprintf_append(schema_entry,
60 "SYNTAX %s%s", syntax, seperator);
61 IF_NULL_FAIL_RET(schema_entry);
63 if (single_value) {
64 schema_entry = talloc_asprintf_append(schema_entry,
65 "SINGLE-VALUE%s", seperator);
66 IF_NULL_FAIL_RET(schema_entry);
69 if (operational) {
70 schema_entry = talloc_asprintf_append(schema_entry,
71 "NO-USER-MODIFICATION%s", seperator);
72 IF_NULL_FAIL_RET(schema_entry);
75 schema_entry = talloc_asprintf_append(schema_entry,
76 ")");
77 return schema_entry;
80 char *schema_attribute_to_description(TALLOC_CTX *mem_ctx, const struct dsdb_attribute *attribute)
82 char *schema_description;
83 const char *syntax = attribute->syntax->ldap_oid;
84 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
85 if (!tmp_ctx) {
86 return NULL;
89 schema_description
90 = schema_attribute_description(mem_ctx,
91 TARGET_AD_SCHEMA_SUBENTRY,
92 " ",
93 attribute->attributeID_oid,
94 attribute->lDAPDisplayName,
95 NULL, NULL, talloc_asprintf(tmp_ctx, "'%s'", syntax),
96 attribute->isSingleValued,
97 attribute->systemOnly);
98 talloc_free(tmp_ctx);
99 return schema_description;
102 #define APPEND_ATTRS(attributes) \
103 do { \
104 int k; \
105 for (k=0; attributes && attributes[k]; k++) { \
106 const char *attr_name = attributes[k]; \
108 schema_entry = talloc_asprintf_append(schema_entry, \
109 "%s ", \
110 attr_name); \
111 IF_NULL_FAIL_RET(schema_entry); \
112 if (attributes[k+1]) { \
113 IF_NULL_FAIL_RET(schema_entry); \
114 if (target == TARGET_OPENLDAP && ((k+1)%5 == 0)) { \
115 schema_entry = talloc_asprintf_append(schema_entry, \
116 "$%s ", seperator); \
117 IF_NULL_FAIL_RET(schema_entry); \
118 } else { \
119 schema_entry = talloc_asprintf_append(schema_entry, \
120 "$ "); \
124 } while (0)
127 /* Print a schema class or dITContentRule as a string.
129 * To print a scheam class, specify objectClassCategory but not auxillary_classes
130 * To print a dITContentRule, specify auxillary_classes but set objectClassCategory == -1
134 char *schema_class_description(TALLOC_CTX *mem_ctx,
135 enum dsdb_schema_convert_target target,
136 const char *seperator,
137 const char *oid,
138 const char *name,
139 const char **auxillary_classes,
140 const char *subClassOf,
141 int objectClassCategory,
142 char **must,
143 char **may)
145 char *schema_entry = talloc_asprintf(mem_ctx,
146 "(%s%s%s", seperator, oid, seperator);
148 IF_NULL_FAIL_RET(schema_entry);
150 schema_entry = talloc_asprintf_append(schema_entry,
151 "NAME '%s'%s", name, seperator);
152 IF_NULL_FAIL_RET(schema_entry);
154 if (auxillary_classes) {
155 schema_entry = talloc_asprintf_append(schema_entry,
156 "AUX ( ");
157 IF_NULL_FAIL_RET(schema_entry);
159 APPEND_ATTRS(auxillary_classes);
161 schema_entry = talloc_asprintf_append(schema_entry,
162 ")%s", seperator);
163 IF_NULL_FAIL_RET(schema_entry);
166 if (subClassOf && strcasecmp(subClassOf, name) != 0) {
167 schema_entry = talloc_asprintf_append(schema_entry,
168 "SUP %s%s", subClassOf, seperator);
169 IF_NULL_FAIL_RET(schema_entry);
172 switch (objectClassCategory) {
173 case -1:
174 break;
175 /* Dummy case for when used for printing ditContentRules */
176 case 0:
178 * NOTE: this is an type 88 class
179 * e.g. 2.5.6.6 NAME 'person'
180 * but w2k3 gives STRUCTURAL here!
182 schema_entry = talloc_asprintf_append(schema_entry,
183 "STRUCTURAL%s", seperator);
184 IF_NULL_FAIL_RET(schema_entry);
185 break;
186 case 1:
187 schema_entry = talloc_asprintf_append(schema_entry,
188 "STRUCTURAL%s", seperator);
189 IF_NULL_FAIL_RET(schema_entry);
190 break;
191 case 2:
192 schema_entry = talloc_asprintf_append(schema_entry,
193 "ABSTRACT%s", seperator);
194 IF_NULL_FAIL_RET(schema_entry);
195 break;
196 case 3:
197 schema_entry = talloc_asprintf_append(schema_entry,
198 "AUXILIARY%s", seperator);
199 IF_NULL_FAIL_RET(schema_entry);
200 break;
203 if (must) {
204 schema_entry = talloc_asprintf_append(schema_entry,
205 "MUST (%s", target == TARGET_AD_SCHEMA_SUBENTRY ? "" : " ");
206 IF_NULL_FAIL_RET(schema_entry);
208 APPEND_ATTRS(must);
210 schema_entry = talloc_asprintf_append(schema_entry,
211 ")%s", seperator);
212 IF_NULL_FAIL_RET(schema_entry);
215 if (may) {
216 schema_entry = talloc_asprintf_append(schema_entry,
217 "MAY (%s", target == TARGET_AD_SCHEMA_SUBENTRY ? "" : " ");
218 IF_NULL_FAIL_RET(schema_entry);
220 APPEND_ATTRS(may);
222 schema_entry = talloc_asprintf_append(schema_entry,
223 ")%s", seperator);
224 IF_NULL_FAIL_RET(schema_entry);
227 schema_entry = talloc_asprintf_append(schema_entry,
228 ")");
229 return schema_entry;
232 char *schema_class_to_description(TALLOC_CTX *mem_ctx, const struct dsdb_class *class)
234 char *schema_description;
235 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
236 if (!tmp_ctx) {
237 return NULL;
240 schema_description
241 = schema_class_description(mem_ctx,
242 TARGET_AD_SCHEMA_SUBENTRY,
243 " ",
244 class->governsID_oid,
245 class->lDAPDisplayName,
246 NULL,
247 class->subClassOf,
248 class->objectClassCategory,
249 dsdb_attribute_list(tmp_ctx,
250 class, DSDB_SCHEMA_ALL_MUST),
251 dsdb_attribute_list(tmp_ctx,
252 class, DSDB_SCHEMA_ALL_MAY));
253 talloc_free(tmp_ctx);
254 return schema_description;
256 char *schema_class_to_dITContentRule(TALLOC_CTX *mem_ctx, const struct dsdb_class *class,
257 const struct dsdb_schema *schema)
259 int i;
260 char *schema_description;
261 char **aux_class_list = NULL;
262 char **attrs;
263 char **must_attr_list = NULL;
264 char **may_attr_list = NULL;
265 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
266 const struct dsdb_class *aux_class;
267 if (!tmp_ctx) {
268 return NULL;
271 aux_class_list = merge_attr_list(tmp_ctx, aux_class_list, class->systemAuxiliaryClass);
272 aux_class_list = merge_attr_list(tmp_ctx, aux_class_list, class->auxiliaryClass);
274 for (i=0; aux_class_list && aux_class_list[i]; i++) {
275 aux_class = dsdb_class_by_lDAPDisplayName(schema, aux_class_list[i]);
277 attrs = dsdb_attribute_list(mem_ctx, aux_class, DSDB_SCHEMA_ALL_MUST);
278 must_attr_list = merge_attr_list(mem_ctx, must_attr_list, attrs);
280 attrs = dsdb_attribute_list(mem_ctx, aux_class, DSDB_SCHEMA_ALL_MAY);
281 may_attr_list = merge_attr_list(mem_ctx, may_attr_list, attrs);
284 schema_description
285 = schema_class_description(mem_ctx,
286 TARGET_AD_SCHEMA_SUBENTRY,
287 " ",
288 class->governsID_oid,
289 class->lDAPDisplayName,
290 (const char **)aux_class_list,
291 NULL, /* Must not specify a
292 * SUP (subclass) in
293 * ditContentRules
294 * per MS-ADTS
295 * 3.1.1.3.1.1.1 */
296 -1, must_attr_list, may_attr_list);
297 talloc_free(tmp_ctx);
298 return schema_description;