r20231: - add more oid => uint32 id mappings
[kai/samba.git] / source4 / dsdb / schema / schema_init.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB schema header
4    
5    Copyright (C) Stefan Metzmacher 2006
6     
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 2 of the License, or
10    (at your option) any later version.
11    
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.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #include "includes.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "lib/util/dlinklist.h"
26
27 #define _PREFIX(uint32, oid) {uint32,oid,sizeof(oid)}
28 static const struct {
29         uint32_t uint32;
30         const char *oid;
31         size_t oid_len;
32 } prefix_mappings[] = {
33         _PREFIX(0x00000000, "2.5.4."),
34         _PREFIX(0x00010000, "2.5.6."),
35         _PREFIX(0x00020000, "1.2.840.113556.1.2."),
36         _PREFIX(0x00030000, "1.2.840.113556.1.3."),
37         _PREFIX(0x00080000, "2.5.5."),
38         _PREFIX(0x00090000, "1.2.840.113556.1.4."),
39         _PREFIX(0x000A0000, "1.2.840.113556.1.5."),
40         _PREFIX(0x00140000, "2.16.840.1.113730.3."),
41         _PREFIX(0x00150000, "0.9.2342.19200300.100.1."),
42         _PREFIX(0x00160000, "2.16.840.1.113730.3.1."),
43         _PREFIX(0x00170000, "1.2.840.113556.1.5.7000."),
44         _PREFIX(0x00180000, "2.5.21."),
45         _PREFIX(0x00190000, "2.5.18."),
46         _PREFIX(0x001A0000, "2.5.20."),
47         _PREFIX(0x001B0000, "1.3.6.1.4.1.1466.101.119."),
48         _PREFIX(0x001C0000, "2.16.840.1.113730.3.2."),
49         _PREFIX(0x001D0000, "1.3.6.1.4.1.250.1."),
50         _PREFIX(0x001E0000, "1.2.840.113549.1.9."),
51         _PREFIX(0x001F0000, "0.9.2342.19200300.100.4."),
52 };
53
54 WERROR dsdb_map_oid2int(const char *in, uint32_t *out)
55 {
56         uint32_t i;
57
58         for (i=0; i < ARRAY_SIZE(prefix_mappings); i++) {
59                 const char *val_str;
60                 char *end_str;
61                 unsigned val;
62
63                 if (strncmp(prefix_mappings[i].oid, in, prefix_mappings[i].oid_len - 1) != 0) {
64                         continue;
65                 }
66
67                 val_str = in + prefix_mappings[i].oid_len - 1;
68                 end_str = NULL;
69                 errno = 0;
70
71                 if (val_str[0] == '\0') {
72                         return WERR_INVALID_PARAM;
73                 }
74
75                 /* two '.' chars are invalid */
76                 if (val_str[0] == '.') {
77                         return WERR_INVALID_PARAM;
78                 }
79
80                 val = strtoul(val_str, &end_str, 10);
81                 if (end_str[0] == '.' && end_str[1] != '\0') {
82                         /*
83                          * if it's a '.' and not the last char
84                          * then maybe an other mapping apply
85                          */
86                         continue;
87                 } else if (end_str[0] != '\0') {
88                         return WERR_INVALID_PARAM;
89                 } else if (val > 0xFFFF) {
90                         return WERR_INVALID_PARAM;
91                 }
92
93                 *out = prefix_mappings[i].uint32 | val;
94                 return WERR_OK;
95         }
96
97         return WERR_DS_NO_MSDS_INTID;
98 }
99
100 WERROR dsdb_map_int2oid(uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
101 {
102         uint32_t i;
103
104         for (i=0; i < ARRAY_SIZE(prefix_mappings); i++) {
105                 const char *val;
106                 if (prefix_mappings[i].uint32 != (in & 0xFFFF0000)) {
107                         continue;
108                 }
109
110                 val = talloc_asprintf(mem_ctx, "%s%u",
111                                       prefix_mappings[i].oid,
112                                       in & 0xFFFF);
113                 W_ERROR_HAVE_NO_MEMORY(val);
114
115                 *out = val;
116                 return WERR_OK;
117         }
118
119         return WERR_DS_NO_MSDS_INTID;
120 }
121
122 #define GET_STRING_LDB(msg, p, elem, strict) do { \
123         (p)->elem = samdb_result_string(msg, #elem, NULL);\
124         if (strict && (p)->elem == NULL) { \
125                 d_printf("%s: %s == NULL\n", __location__, #elem); \
126                 return WERR_INVALID_PARAM; \
127         } \
128         (void)talloc_steal(p, (p)->elem); \
129 } while (0)
130
131 #define GET_BOOL_LDB(msg, p, elem, strict) do { \
132         const char *str; \
133         str = samdb_result_string(msg, #elem, NULL);\
134         if (str == NULL) { \
135                 if (strict) { \
136                         d_printf("%s: %s == NULL\n", __location__, #elem); \
137                         return WERR_INVALID_PARAM; \
138                 } else { \
139                         (p)->elem = False; \
140                 } \
141         } else if (strcasecmp("TRUE", str) == 0) { \
142                 (p)->elem = True; \
143         } else if (strcasecmp("FALSE", str) == 0) { \
144                 (p)->elem = False; \
145         } else { \
146                 d_printf("%s: %s == %s\n", __location__, #elem, str); \
147                 return WERR_INVALID_PARAM; \
148         } \
149 } while (0)
150
151 #define GET_UINT32_LDB(msg, p, elem) do { \
152         (p)->elem = samdb_result_uint(msg, #elem, 0);\
153 } while (0)
154
155 #define GET_GUID_LDB(msg, p, elem) do { \
156         (p)->elem = samdb_result_guid(msg, #elem);\
157 } while (0)
158
159 #define GET_BLOB_LDB(msg, p, elem, attr) do { \
160         const struct ldb_val *_val;\
161         _val = ldb_msg_find_ldb_val(msg, attr);\
162         if (_val) {\
163                 (p)->elem = *_val;\
164                 (void)talloc_steal(p, (p)->elem.data);\
165         } else {\
166                 ZERO_STRUCT((p)->elem);\
167         }\
168 } while (0)
169
170 WERROR dsdb_attribute_from_ldb(struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct dsdb_attribute *attr)
171 {
172         WERROR status;
173
174         GET_STRING_LDB(msg, attr, cn, True);
175         GET_STRING_LDB(msg, attr, lDAPDisplayName, True);
176         GET_STRING_LDB(msg, attr, attributeID_oid, True);
177         status = dsdb_map_oid2int(attr->attributeID_oid, &attr->attributeID_id);
178         W_ERROR_NOT_OK_RETURN(status);
179         GET_GUID_LDB(msg, attr, schemaIDGUID);
180         GET_UINT32_LDB(msg, attr, mAPIID);
181
182         GET_GUID_LDB(msg, attr, attributeSecurityGUID);
183
184         GET_UINT32_LDB(msg, attr, searchFlags);
185         GET_UINT32_LDB(msg, attr, systemFlags);
186         GET_BOOL_LDB(msg, attr, isMemberOfPartialAttributeSet, False);
187         GET_UINT32_LDB(msg, attr, linkID);
188
189         GET_STRING_LDB(msg, attr, attributeSyntax_oid, True);
190         status = dsdb_map_oid2int(attr->attributeSyntax_oid, &attr->attributeSyntax_id);
191         W_ERROR_NOT_OK_RETURN(status);
192         GET_UINT32_LDB(msg, attr, oMSyntax);
193         GET_BLOB_LDB(msg, attr, oMObjectClass, "oMObjectClass");
194
195         GET_BOOL_LDB(msg, attr, isSingleValued, True);
196         GET_UINT32_LDB(msg, attr, rangeLower);
197         GET_UINT32_LDB(msg, attr, rangeUpper);
198         GET_BOOL_LDB(msg, attr, extendedCharsAllowed, False);
199
200         GET_UINT32_LDB(msg, attr, schemaFlagsEx);
201         GET_BLOB_LDB(msg, attr, msDs_Schema_Extensions, "msDs-Schema-Extensions");
202
203         GET_BOOL_LDB(msg, attr, showInAdvancedViewOnly, False);
204         GET_STRING_LDB(msg, attr, adminDisplayName, True);
205         GET_STRING_LDB(msg, attr, adminDescription, True);
206         GET_STRING_LDB(msg, attr, classDisplayName, True);
207         GET_BOOL_LDB(msg, attr, isEphemeral, False);
208         GET_BOOL_LDB(msg, attr, isDefunct, False);
209         GET_BOOL_LDB(msg, attr, systemOnly, False);
210
211         return WERR_OK;
212 }
213
214 WERROR dsdb_class_from_ldb(struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct dsdb_class *obj)
215 {
216         WERROR status;
217
218         GET_STRING_LDB(msg, obj, cn, True);
219         GET_STRING_LDB(msg, obj, lDAPDisplayName, True);
220         GET_STRING_LDB(msg, obj, governsID_oid, True);
221         status = dsdb_map_oid2int(obj->governsID_oid, &obj->governsID_id);
222         W_ERROR_NOT_OK_RETURN(status);
223         GET_GUID_LDB(msg, obj, schemaIDGUID);
224
225         GET_UINT32_LDB(msg, obj, objectClassCategory);
226         GET_STRING_LDB(msg, obj, rDNAttID, True);
227         GET_STRING_LDB(msg, obj, defaultObjectCategory, True);
228  
229         GET_STRING_LDB(msg, obj, subClassOf, True);
230
231         GET_STRING_LDB(msg, obj, systemAuxiliaryClass, False);
232         obj->systemPossSuperiors= NULL;
233         obj->systemMustContain  = NULL;
234         obj->systemMayContain   = NULL;
235
236         GET_STRING_LDB(msg, obj, auxiliaryClass, False);
237         obj->possSuperiors      = NULL;
238         obj->mustContain        = NULL;
239         obj->mayContain         = NULL;
240
241         GET_STRING_LDB(msg, obj, defaultSecurityDescriptor, False);
242
243         GET_UINT32_LDB(msg, obj, schemaFlagsEx);
244         GET_BLOB_LDB(msg, obj, msDs_Schema_Extensions, "msDs-Schema-Extensions");
245
246         GET_BOOL_LDB(msg, obj, showInAdvancedViewOnly, False);
247         GET_STRING_LDB(msg, obj, adminDisplayName, True);
248         GET_STRING_LDB(msg, obj, adminDescription, True);
249         GET_STRING_LDB(msg, obj, classDisplayName, True);
250         GET_BOOL_LDB(msg, obj, defaultHidingValue, True);
251         GET_BOOL_LDB(msg, obj, isDefunct, False);
252         GET_BOOL_LDB(msg, obj, systemOnly, False);
253
254         return WERR_OK;
255 }