summaryrefslogtreecommitdiff
path: root/source4/dsdb/schema/schema_init.c
blob: 9dc87e31a998e980e2bd3f5583b89e45d51c5325 (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
/* 
   Unix SMB/CIFS mplementation.
   DSDB schema header
   
   Copyright (C) Stefan Metzmacher 2006
    
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
*/

#include "includes.h"
#include "dsdb/samdb/samdb.h"
#include "lib/util/dlinklist.h"

#define _PREFIX(uint32, oid) {uint32,oid,sizeof(oid)}
static const struct {
	uint32_t uint32;
	const char *oid;
	size_t oid_len;
} prefix_mappings[] = {
	_PREFIX(0x00000000, "2.5.4."),
	_PREFIX(0x00010000, "2.5.6."),
	_PREFIX(0x00020000, "1.2.840.113556.1.2."),
	_PREFIX(0x00030000, "1.2.840.113556.1.3."),
	_PREFIX(0x00080000, "2.5.5."),
	_PREFIX(0x00090000, "1.2.840.113556.1.4."),
	_PREFIX(0x000A0000, "1.2.840.113556.1.5."),
	_PREFIX(0x00140000, "2.16.840.1.113730.3."),
	_PREFIX(0x00150000, "0.9.2342.19200300.100.1."),
	_PREFIX(0x00160000, "2.16.840.1.113730.3.1."),
	_PREFIX(0x00170000, "1.2.840.113556.1.5.7000."),
	_PREFIX(0x001A0000, "2.5.20."),
	_PREFIX(0x001C0000, "2.16.840.1.113730.3.2."),
	_PREFIX(0x001D0000, "1.3.6.1.4.1.250.1."),
	_PREFIX(0x001F0000, "0.9.2342.19200300.100.4."),
};

WERROR dsdb_map_oid2int(const char *in, uint32_t *out)
{
	uint32_t i;

	for (i=0; i < ARRAY_SIZE(prefix_mappings); i++) {
		const char *val_str;
		char *end_str;
		unsigned val;

		if (strncmp(prefix_mappings[i].oid, in, prefix_mappings[i].oid_len - 1) != 0) {
			continue;
		}

		val_str = in + prefix_mappings[i].oid_len - 1;
		end_str = NULL;
		errno = 0;

		if (val_str[0] == '\0') {
			return WERR_INVALID_PARAM;
		}

		val = strtoul(val_str, &end_str, 10);
		if (end_str[0] != '\0') {
			return WERR_INVALID_PARAM;
		} else if (val > 0xFFFF) {
			return WERR_INVALID_PARAM;
		}

		*out = prefix_mappings[i].uint32 | val;
		return WERR_OK;
	}

	return WERR_DS_NO_MSDS_INTID;
}

WERROR dsdb_map_int2oid(uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
{
	uint32_t i;

	for (i=0; i < ARRAY_SIZE(prefix_mappings); i++) {
		const char *val;
		if (prefix_mappings[i].uint32 != (in & 0xFFFF0000)) {
			continue;
		}

		val = talloc_asprintf(mem_ctx, "%s%u",
				      prefix_mappings[i].oid,
				      in & 0xFFFF);
		W_ERROR_HAVE_NO_MEMORY(val);

		*out = val;
		return WERR_OK;
	}

	return WERR_DS_NO_MSDS_INTID;
}

#define GET_STRING_LDB(msg, p, elem, strict) do { \
	(p)->elem = samdb_result_string(msg, #elem, NULL);\
	if (strict && (p)->elem == NULL) { \
		d_printf("%s: %s == NULL\n", __location__, #elem); \
		return WERR_INVALID_PARAM; \
	} \
	(void)talloc_steal(p, (p)->elem); \
} while (0)

#define GET_BOOL_LDB(msg, p, elem, strict) do { \
	const char *str; \
	str = samdb_result_string(msg, #elem, NULL);\
	if (str == NULL) { \
		if (strict) { \
			d_printf("%s: %s == NULL\n", __location__, #elem); \
			return WERR_INVALID_PARAM; \
		} else { \
			(p)->elem = False; \
		} \
	} else if (strcasecmp("TRUE", str) == 0) { \
		(p)->elem = True; \
	} else if (strcasecmp("FALSE", str) == 0) { \
		(p)->elem = False; \
	} else { \
		d_printf("%s: %s == %s\n", __location__, #elem, str); \
		return WERR_INVALID_PARAM; \
	} \
} while (0)

#define GET_UINT32_LDB(msg, p, elem) do { \
	(p)->elem = samdb_result_uint(msg, #elem, 0);\
} while (0)

#define GET_GUID_LDB(msg, p, elem) do { \
	(p)->elem = samdb_result_guid(msg, #elem);\
} while (0)

#define GET_BLOB_LDB(msg, p, elem, attr) do { \
	const struct ldb_val *_val;\
	_val = ldb_msg_find_ldb_val(msg, attr);\
	if (_val) {\
		(p)->elem = *_val;\
		(void)talloc_steal(p, (p)->elem.data);\
	} else {\
		ZERO_STRUCT((p)->elem);\
	}\
} while (0)

WERROR dsdb_attribute_from_ldb(struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct dsdb_attribute *attr)
{
	WERROR status;

	GET_STRING_LDB(msg, attr, cn, True);
	GET_STRING_LDB(msg, attr, lDAPDisplayName, True);
	GET_STRING_LDB(msg, attr, attributeID_oid, True);
	status = dsdb_map_oid2int(attr->attributeID_oid, &attr->attributeID_id);
	W_ERROR_NOT_OK_RETURN(status);
	GET_GUID_LDB(msg, attr, schemaIDGUID);
	GET_UINT32_LDB(msg, attr, mAPIID);

	GET_GUID_LDB(msg, attr, attributeSecurityGUID);

	GET_UINT32_LDB(msg, attr, searchFlags);
	GET_UINT32_LDB(msg, attr, systemFlags);
	GET_BOOL_LDB(msg, attr, isMemberOfPartialAttributeSet, False);
	GET_UINT32_LDB(msg, attr, linkID);

	GET_STRING_LDB(msg, attr, attributeSyntax_oid, True);
	status = dsdb_map_oid2int(attr->attributeSyntax_oid, &attr->attributeSyntax_id);
	W_ERROR_NOT_OK_RETURN(status);
	GET_UINT32_LDB(msg, attr, oMSyntax);
	GET_BLOB_LDB(msg, attr, oMObjectClass, "oMObjectClass");

	GET_BOOL_LDB(msg, attr, isSingleValued, True);
	GET_UINT32_LDB(msg, attr, rangeLower);
	GET_UINT32_LDB(msg, attr, rangeUpper);
	GET_BOOL_LDB(msg, attr, extendedCharsAllowed, False);

	GET_UINT32_LDB(msg, attr, schemaFlagsEx);
	GET_BLOB_LDB(msg, attr, msDs_Schema_Extensions, "msDs-Schema-Extensions");

	GET_BOOL_LDB(msg, attr, showInAdvancedViewOnly, False);
	GET_STRING_LDB(msg, attr, adminDisplayName, True);
	GET_STRING_LDB(msg, attr, adminDescription, True);
	GET_STRING_LDB(msg, attr, classDisplayName, True);
	GET_BOOL_LDB(msg, attr, isEphemeral, False);
	GET_BOOL_LDB(msg, attr, isDefunct, False);
	GET_BOOL_LDB(msg, attr, systemOnly, False);

	return WERR_OK;
}

WERROR dsdb_class_from_ldb(struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct dsdb_class *obj)
{
	WERROR status;

	GET_STRING_LDB(msg, obj, cn, True);
	GET_STRING_LDB(msg, obj, lDAPDisplayName, True);
	GET_STRING_LDB(msg, obj, governsID_oid, True);
	status = dsdb_map_oid2int(obj->governsID_oid, &obj->governsID_id);
	W_ERROR_NOT_OK_RETURN(status);
	GET_GUID_LDB(msg, obj, schemaIDGUID);

	GET_UINT32_LDB(msg, obj, objectClassCategory);
	GET_STRING_LDB(msg, obj, rDNAttID, True);
	GET_STRING_LDB(msg, obj, defaultObjectCategory, True);
 
	GET_STRING_LDB(msg, obj, subClassOf, True);

	GET_STRING_LDB(msg, obj, systemAuxiliaryClass, False);
	obj->systemPossSuperiors= NULL;
	obj->systemMustContain	= NULL;
	obj->systemMayContain	= NULL;

	GET_STRING_LDB(msg, obj, auxiliaryClass, False);
	obj->possSuperiors	= NULL;
	obj->mustContain	= NULL;
	obj->mayContain		= NULL;

	GET_STRING_LDB(msg, obj, defaultSecurityDescriptor, False);

	GET_UINT32_LDB(msg, obj, schemaFlagsEx);
	GET_BLOB_LDB(msg, obj, msDs_Schema_Extensions, "msDs-Schema-Extensions");

	GET_BOOL_LDB(msg, obj, showInAdvancedViewOnly, False);
	GET_STRING_LDB(msg, obj, adminDisplayName, True);
	GET_STRING_LDB(msg, obj, adminDescription, True);
	GET_STRING_LDB(msg, obj, classDisplayName, True);
	GET_BOOL_LDB(msg, obj, defaultHidingValue, True);
	GET_BOOL_LDB(msg, obj, isDefunct, False);
	GET_BOOL_LDB(msg, obj, systemOnly, False);

	return WERR_OK;
}