r20226: rename macros with a _LDB at the end, because I'll add macros with _DS
[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(0x001A0000, "2.5.20."),
45         _PREFIX(0x001C0000, "2.16.840.1.113730.3.2."),
46         _PREFIX(0x001D0000, "1.3.6.1.4.1.250.1."),
47         _PREFIX(0x001F0000, "0.9.2342.19200300.100.4."),
48 };
49
50 WERROR dsdb_map_oid2int(const char *in, uint32_t *out)
51 {
52         uint32_t i;
53
54         for (i=0; i < ARRAY_SIZE(prefix_mappings); i++) {
55                 const char *val_str;
56                 char *end_str;
57                 unsigned val;
58
59                 if (strncmp(prefix_mappings[i].oid, in, prefix_mappings[i].oid_len - 1) != 0) {
60                         continue;
61                 }
62
63                 val_str = in + prefix_mappings[i].oid_len - 1;
64                 end_str = NULL;
65                 errno = 0;
66
67                 if (val_str[0] == '\0') {
68                         return WERR_INVALID_PARAM;
69                 }
70
71                 val = strtoul(val_str, &end_str, 10);
72                 if (end_str[0] != '\0') {
73                         return WERR_INVALID_PARAM;
74                 } else if (val > 0xFFFF) {
75                         return WERR_INVALID_PARAM;
76                 }
77
78                 *out = prefix_mappings[i].uint32 | val;
79                 return WERR_OK;
80         }
81
82         return WERR_DS_NO_MSDS_INTID;
83 }
84
85 WERROR dsdb_map_int2oid(uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
86 {
87         uint32_t i;
88
89         for (i=0; i < ARRAY_SIZE(prefix_mappings); i++) {
90                 const char *val;
91                 if (prefix_mappings[i].uint32 != (in & 0xFFFF0000)) {
92                         continue;
93                 }
94
95                 val = talloc_asprintf(mem_ctx, "%s%u",
96                                       prefix_mappings[i].oid,
97                                       in & 0xFFFF);
98                 W_ERROR_HAVE_NO_MEMORY(val);
99
100                 *out = val;
101                 return WERR_OK;
102         }
103
104         return WERR_DS_NO_MSDS_INTID;
105 }
106
107 #define GET_STRING_LDB(msg, p, elem, strict) do { \
108         (p)->elem = samdb_result_string(msg, #elem, NULL);\
109         if (strict && (p)->elem == NULL) { \
110                 d_printf("%s: %s == NULL\n", __location__, #elem); \
111                 return WERR_INVALID_PARAM; \
112         } \
113         (void)talloc_steal(p, (p)->elem); \
114 } while (0)
115
116 #define GET_BOOL_LDB(msg, p, elem, strict) do { \
117         const char *str; \
118         str = samdb_result_string(msg, #elem, NULL);\
119         if (str == NULL) { \
120                 if (strict) { \
121                         d_printf("%s: %s == NULL\n", __location__, #elem); \
122                         return WERR_INVALID_PARAM; \
123                 } else { \
124                         (p)->elem = False; \
125                 } \
126         } else if (strcasecmp("TRUE", str) == 0) { \
127                 (p)->elem = True; \
128         } else if (strcasecmp("FALSE", str) == 0) { \
129                 (p)->elem = False; \
130         } else { \
131                 d_printf("%s: %s == %s\n", __location__, #elem, str); \
132                 return WERR_INVALID_PARAM; \
133         } \
134 } while (0)
135
136 #define GET_UINT32_LDB(msg, p, elem) do { \
137         (p)->elem = samdb_result_uint(msg, #elem, 0);\
138 } while (0)
139
140 #define GET_GUID_LDB(msg, p, elem) do { \
141         (p)->elem = samdb_result_guid(msg, #elem);\
142 } while (0)
143
144 #define GET_BLOB_LDB(msg, p, elem, attr) do { \
145         const struct ldb_val *_val;\
146         _val = ldb_msg_find_ldb_val(msg, attr);\
147         if (_val) {\
148                 (p)->elem = *_val;\
149                 (void)talloc_steal(p, (p)->elem.data);\
150         } else {\
151                 ZERO_STRUCT((p)->elem);\
152         }\
153 } while (0)
154
155 WERROR dsdb_attribute_from_ldb(struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct dsdb_attribute *attr)
156 {
157         WERROR status;
158
159         GET_STRING_LDB(msg, attr, cn, True);
160         GET_STRING_LDB(msg, attr, lDAPDisplayName, True);
161         GET_STRING_LDB(msg, attr, attributeID_oid, True);
162         status = dsdb_map_oid2int(attr->attributeID_oid, &attr->attributeID_id);
163         W_ERROR_NOT_OK_RETURN(status);
164         GET_GUID_LDB(msg, attr, schemaIDGUID);
165         GET_UINT32_LDB(msg, attr, mAPIID);
166
167         GET_GUID_LDB(msg, attr, attributeSecurityGUID);
168
169         GET_UINT32_LDB(msg, attr, searchFlags);
170         GET_UINT32_LDB(msg, attr, systemFlags);
171         GET_BOOL_LDB(msg, attr, isMemberOfPartialAttributeSet, False);
172         GET_UINT32_LDB(msg, attr, linkID);
173
174         GET_STRING_LDB(msg, attr, attributeSyntax_oid, True);
175         status = dsdb_map_oid2int(attr->attributeSyntax_oid, &attr->attributeSyntax_id);
176         W_ERROR_NOT_OK_RETURN(status);
177         GET_UINT32_LDB(msg, attr, oMSyntax);
178         GET_BLOB_LDB(msg, attr, oMObjectClass, "oMObjectClass");
179
180         GET_BOOL_LDB(msg, attr, isSingleValued, True);
181         GET_UINT32_LDB(msg, attr, rangeLower);
182         GET_UINT32_LDB(msg, attr, rangeUpper);
183         GET_BOOL_LDB(msg, attr, extendedCharsAllowed, False);
184
185         GET_UINT32_LDB(msg, attr, schemaFlagsEx);
186         GET_BLOB_LDB(msg, attr, msDs_Schema_Extensions, "msDs-Schema-Extensions");
187
188         GET_BOOL_LDB(msg, attr, showInAdvancedViewOnly, False);
189         GET_STRING_LDB(msg, attr, adminDisplayName, True);
190         GET_STRING_LDB(msg, attr, adminDescription, True);
191         GET_STRING_LDB(msg, attr, classDisplayName, True);
192         GET_BOOL_LDB(msg, attr, isEphemeral, False);
193         GET_BOOL_LDB(msg, attr, isDefunct, False);
194         GET_BOOL_LDB(msg, attr, systemOnly, False);
195
196         return WERR_OK;
197 }
198
199 WERROR dsdb_class_from_ldb(struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct dsdb_class *obj)
200 {
201         WERROR status;
202
203         GET_STRING_LDB(msg, obj, cn, True);
204         GET_STRING_LDB(msg, obj, lDAPDisplayName, True);
205         GET_STRING_LDB(msg, obj, governsID_oid, True);
206         status = dsdb_map_oid2int(obj->governsID_oid, &obj->governsID_id);
207         W_ERROR_NOT_OK_RETURN(status);
208         GET_GUID_LDB(msg, obj, schemaIDGUID);
209
210         GET_UINT32_LDB(msg, obj, objectClassCategory);
211         GET_STRING_LDB(msg, obj, rDNAttID, True);
212         GET_STRING_LDB(msg, obj, defaultObjectCategory, True);
213  
214         GET_STRING_LDB(msg, obj, subClassOf, True);
215
216         GET_STRING_LDB(msg, obj, systemAuxiliaryClass, False);
217         obj->systemPossSuperiors= NULL;
218         obj->systemMustContain  = NULL;
219         obj->systemMayContain   = NULL;
220
221         GET_STRING_LDB(msg, obj, auxiliaryClass, False);
222         obj->possSuperiors      = NULL;
223         obj->mustContain        = NULL;
224         obj->mayContain         = NULL;
225
226         GET_STRING_LDB(msg, obj, defaultSecurityDescriptor, False);
227
228         GET_UINT32_LDB(msg, obj, schemaFlagsEx);
229         GET_BLOB_LDB(msg, obj, msDs_Schema_Extensions, "msDs-Schema-Extensions");
230
231         GET_BOOL_LDB(msg, obj, showInAdvancedViewOnly, False);
232         GET_STRING_LDB(msg, obj, adminDisplayName, True);
233         GET_STRING_LDB(msg, obj, adminDescription, True);
234         GET_STRING_LDB(msg, obj, classDisplayName, True);
235         GET_BOOL_LDB(msg, obj, defaultHidingValue, True);
236         GET_BOOL_LDB(msg, obj, isDefunct, False);
237         GET_BOOL_LDB(msg, obj, systemOnly, False);
238
239         return WERR_OK;
240 }