summaryrefslogtreecommitdiff
path: root/source4/dsdb/schema/schema_description.c
blob: c3c37b46533320b56bb2e4ebcb8f6f68c6843aa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* 
   Unix SMB/CIFS mplementation.
   Print schema info into string format
   
   Copyright (C) Andrew Bartlett 2006-2008
    
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   
*/
#include "includes.h"
#include "dsdb/samdb/samdb.h"

#define IF_NULL_FAIL_RET(x) do {     \
		if (!x) {		\
			return NULL;	\
		}			\
	} while (0) 


char *schema_attribute_description(TALLOC_CTX *mem_ctx, 
					  enum dsdb_schema_convert_target target,
					  const char *seperator,
					  const char *oid, 
					  const char *name,
					  const char *equality, 
					  const char *substring, 
					  const char *syntax,
					  bool single_value, bool operational)
{
	char *schema_entry = talloc_asprintf(mem_ctx, 
					     "(%s%s%s", seperator, oid, seperator);
	
	schema_entry = talloc_asprintf_append(schema_entry, 
					      "NAME '%s'%s", name, seperator);
	IF_NULL_FAIL_RET(schema_entry);
	
	if (equality) {
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "EQUALITY %s%s", equality, seperator);
		IF_NULL_FAIL_RET(schema_entry);
	}
	if (substring) {
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "SUBSTR %s%s", substring, seperator);
		IF_NULL_FAIL_RET(schema_entry);
	}
	
	schema_entry = talloc_asprintf_append(schema_entry, 
					      "SYNTAX %s%s", syntax, seperator);
	IF_NULL_FAIL_RET(schema_entry);
	
	if (single_value) {
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "SINGLE-VALUE%s", seperator);
		IF_NULL_FAIL_RET(schema_entry);
	}
	
	if (operational) {
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "NO-USER-MODIFICATION%s", seperator);
		IF_NULL_FAIL_RET(schema_entry);
	}
	
	schema_entry = talloc_asprintf_append(schema_entry, 
					      ")");
	return schema_entry;
}

char *schema_attribute_to_description(TALLOC_CTX *mem_ctx, const struct dsdb_attribute *attribute) 
{
	char *schema_description;
	const struct dsdb_syntax *map = find_syntax_map_by_ad_oid(attribute->attributeSyntax_oid);
	const char *syntax = map ? map->ldap_oid : attribute->attributeSyntax_oid;
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	if (!tmp_ctx) {
		return NULL;
	}

	
	schema_description 
		= schema_attribute_description(mem_ctx, 
					       TARGET_AD_SCHEMA_SUBENTRY,
					       " ",
					       attribute->attributeID_oid,
					       attribute->lDAPDisplayName,
					       NULL, NULL, talloc_asprintf(tmp_ctx, "'%s'", syntax),
					       attribute->isSingleValued,
					       attribute->systemOnly);
	talloc_free(tmp_ctx);
	return schema_description;
}

#define APPEND_ATTRS(attributes)				\
	do {								\
		int k;							\
		for (k=0; attributes && attributes[k]; k++) {		\
			const char *attr_name = attributes[k];		\
									\
			schema_entry = talloc_asprintf_append(schema_entry, \
							      "%s ",	\
							      attr_name); \
			IF_NULL_FAIL_RET(schema_entry);			\
			if (attributes[k+1]) {				\
				IF_NULL_FAIL_RET(schema_entry);		\
				if (target == TARGET_OPENLDAP && ((k+1)%5 == 0)) { \
					schema_entry = talloc_asprintf_append(schema_entry, \
									      "$%s ", seperator); \
					IF_NULL_FAIL_RET(schema_entry);	\
				} else {				\
					schema_entry = talloc_asprintf_append(schema_entry, \
									      "$ "); \
				}					\
			}						\
		}							\
	} while (0)
	

/* Print a schema class or dITContentRule as a string.  
 *
 * To print a scheam class, specify objectClassCategory but not auxillary_classes
 * To print a dITContentRule, specify auxillary_classes but set objectClassCategory == -1
 *
 */

char *schema_class_description(TALLOC_CTX *mem_ctx, 
			       enum dsdb_schema_convert_target target,
			       const char *seperator,
			       const char *oid, 
			       const char *name,
			       const char **auxillary_classes,
			       const char *subClassOf,
			       int objectClassCategory,
			       char **must,
			       char **may)
{
	char *schema_entry = talloc_asprintf(mem_ctx, 
					     "(%s%s%s", seperator, oid, seperator);
	
	IF_NULL_FAIL_RET(schema_entry);

	schema_entry = talloc_asprintf_append(schema_entry, 
					      "NAME '%s'%s", name, seperator);
	IF_NULL_FAIL_RET(schema_entry);
	
	if (auxillary_classes) {
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "AUX ( ");
		IF_NULL_FAIL_RET(schema_entry);
		
		APPEND_ATTRS(auxillary_classes);
		
		schema_entry = talloc_asprintf_append(schema_entry, 
						      ")%s", seperator);
		IF_NULL_FAIL_RET(schema_entry);
	}

	if (subClassOf && strcasecmp(subClassOf, name) != 0) {
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "SUP %s%s", subClassOf, seperator);
		IF_NULL_FAIL_RET(schema_entry);
	}
	
	switch (objectClassCategory) {
	case -1:
		break;
		/* Dummy case for when used for printing ditContentRules */
	case 0:
		/*
		 * NOTE: this is an type 88 class
		 *       e.g. 2.5.6.6 NAME 'person'
		 *	 but w2k3 gives STRUCTURAL here!
		 */
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "STRUCTURAL%s", seperator);
		IF_NULL_FAIL_RET(schema_entry);
		break;
	case 1:
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "STRUCTURAL%s", seperator);
		IF_NULL_FAIL_RET(schema_entry);
		break;
	case 2:
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "ABSTRACT%s", seperator);
		IF_NULL_FAIL_RET(schema_entry);
		break;
	case 3:
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "AUXILIARY%s", seperator);
		IF_NULL_FAIL_RET(schema_entry);
		break;
	}
	
	if (must) {
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "MUST (%s", target == TARGET_AD_SCHEMA_SUBENTRY ? "" : " ");
		IF_NULL_FAIL_RET(schema_entry);
		
		APPEND_ATTRS(must);
		
		schema_entry = talloc_asprintf_append(schema_entry, 
						      ")%s", seperator);
		IF_NULL_FAIL_RET(schema_entry);
	}
	
	if (may) {
		schema_entry = talloc_asprintf_append(schema_entry, 
						      "MAY (%s", target == TARGET_AD_SCHEMA_SUBENTRY ? "" : " ");
		IF_NULL_FAIL_RET(schema_entry);
		
		APPEND_ATTRS(may);
		
		schema_entry = talloc_asprintf_append(schema_entry, 
						      ")%s", seperator);
		IF_NULL_FAIL_RET(schema_entry);
	}
	
	schema_entry = talloc_asprintf_append(schema_entry, 
					      ")");
	return schema_entry;
}

char *schema_class_to_description(TALLOC_CTX *mem_ctx, const struct dsdb_class *class) 
{
	char *schema_description;
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	if (!tmp_ctx) {
		return NULL;
	}
	
	schema_description
		= schema_class_description(mem_ctx, 
					   TARGET_AD_SCHEMA_SUBENTRY,
					   " ",
					   class->governsID_oid,
					   class->lDAPDisplayName,
					   NULL, 
					   class->subClassOf,
					   class->objectClassCategory,
					   dsdb_attribute_list(tmp_ctx, 
							       class, DSDB_SCHEMA_ALL_MUST),
					   dsdb_attribute_list(tmp_ctx, 
							       class, DSDB_SCHEMA_ALL_MAY));
	talloc_free(tmp_ctx);
	return schema_description;
}
char *schema_class_to_dITContentRule(TALLOC_CTX *mem_ctx, const struct dsdb_class *class,
				     const struct dsdb_schema *schema) 
{
	int i;
	char *schema_description;
	char **aux_class_list = NULL;
	char **attrs;
	char **must_attr_list = NULL;
	char **may_attr_list = NULL;
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	const struct dsdb_class *aux_class;
	if (!tmp_ctx) {
		return NULL;
	}

	aux_class_list = merge_attr_list(tmp_ctx, aux_class_list, class->systemAuxiliaryClass);
	aux_class_list = merge_attr_list(tmp_ctx, aux_class_list, class->auxiliaryClass);

	for (i=0; aux_class_list && aux_class_list[i]; i++) {
		aux_class = dsdb_class_by_lDAPDisplayName(schema, aux_class_list[i]);
		
		attrs = dsdb_attribute_list(mem_ctx, aux_class, DSDB_SCHEMA_ALL_MUST);
		must_attr_list = merge_attr_list(mem_ctx, must_attr_list, attrs);

		attrs = dsdb_attribute_list(mem_ctx, aux_class, DSDB_SCHEMA_ALL_MAY);
		may_attr_list = merge_attr_list(mem_ctx, may_attr_list, attrs);
	}

	schema_description
		= schema_class_description(mem_ctx, 
					   TARGET_AD_SCHEMA_SUBENTRY,
					   " ",
					   class->governsID_oid,
					   class->lDAPDisplayName,
					   (const char **)aux_class_list,
					   NULL, /* Must not specify a
						  * SUP (subclass) in
						  * ditContentRules
						  * per MS-ADTS
						  * 3.1.1.3.1.1.1 */
					   -1, must_attr_list, may_attr_list);
	talloc_free(tmp_ctx);
	return schema_description;
}