python: models: rename argument ldb to samdb
[samba.git] / source3 / libads / ldap_schema.c
blobd2f486e4918e86550c0a4cd3c56a77658bae0f54
1 /*
2 Unix SMB/CIFS implementation.
3 ads (active directory) utility library
4 Copyright (C) Guenther Deschner 2005-2007
5 Copyright (C) Gerald (Jerry) Carter 2006
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 "ads.h"
23 #include "libads/ldap_schema.h"
24 #include "libads/ldap_schema_oids.h"
25 #include "../libcli/ldap/ldap_ndr.h"
27 #ifdef HAVE_LDAP
29 static ADS_STATUS ads_get_attrnames_by_oids(ADS_STRUCT *ads,
30 TALLOC_CTX *mem_ctx,
31 const char *schema_path,
32 const char **OIDs,
33 size_t num_OIDs,
34 char ***OIDs_out, char ***names,
35 size_t *count)
37 ADS_STATUS status;
38 LDAPMessage *res = NULL;
39 LDAPMessage *msg;
40 char *expr = NULL;
41 const char *attrs[] = { "lDAPDisplayName", "attributeId", NULL };
42 int i = 0, p = 0;
44 if (!ads || !mem_ctx || !names || !count || !OIDs || !OIDs_out) {
45 return ADS_ERROR(LDAP_PARAM_ERROR);
48 if (num_OIDs == 0 || OIDs[0] == NULL) {
49 return ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
52 expr = talloc_asprintf(mem_ctx, "(|");
54 for (i=0; i<num_OIDs; i++) {
55 talloc_asprintf_addbuf(&expr, "(attributeId=%s)", OIDs[i]);
58 talloc_asprintf_addbuf(&expr, ")");
59 if (expr == NULL) {
60 return ADS_ERROR(LDAP_NO_MEMORY);
63 status = ads_do_search_retry(ads, schema_path,
64 LDAP_SCOPE_SUBTREE, expr, attrs, &res);
65 if (!ADS_ERR_OK(status)) {
66 return status;
69 *count = ads_count_replies(ads, res);
70 if (*count == 0 || !res) {
71 status = ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
72 goto out;
75 if (((*names) = talloc_array(mem_ctx, char *, *count)) == NULL) {
76 status = ADS_ERROR(LDAP_NO_MEMORY);
77 goto out;
79 if (((*OIDs_out) = talloc_array(mem_ctx, char *, *count)) == NULL) {
80 status = ADS_ERROR(LDAP_NO_MEMORY);
81 goto out;
84 for (msg = ads_first_entry(ads, res); msg != NULL;
85 msg = ads_next_entry(ads, msg)) {
87 (*names)[p] = ads_pull_string(ads, mem_ctx, msg,
88 "lDAPDisplayName");
89 (*OIDs_out)[p] = ads_pull_string(ads, mem_ctx, msg,
90 "attributeId");
91 if (((*names)[p] == NULL) || ((*OIDs_out)[p] == NULL)) {
92 status = ADS_ERROR(LDAP_NO_MEMORY);
93 goto out;
96 p++;
99 if (*count < num_OIDs) {
100 status = ADS_ERROR_NT(STATUS_SOME_UNMAPPED);
101 goto out;
104 status = ADS_ERROR(LDAP_SUCCESS);
105 out:
106 ads_msgfree(ads, res);
108 return status;
111 const char *ads_get_attrname_by_guid(ADS_STRUCT *ads,
112 const char *schema_path,
113 TALLOC_CTX *mem_ctx,
114 const struct GUID *schema_guid)
116 ADS_STATUS rc;
117 LDAPMessage *res = NULL;
118 char *expr = NULL;
119 const char *attrs[] = { "lDAPDisplayName", NULL };
120 const char *result = NULL;
121 char *guid_bin = NULL;
123 if (!ads || !mem_ctx || !schema_guid) {
124 goto done;
127 guid_bin = ldap_encode_ndr_GUID(mem_ctx, schema_guid);
128 if (!guid_bin) {
129 goto done;
132 expr = talloc_asprintf(mem_ctx, "(schemaIDGUID=%s)", guid_bin);
133 if (!expr) {
134 goto done;
137 rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE,
138 expr, attrs, &res);
139 if (!ADS_ERR_OK(rc)) {
140 goto done;
143 if (ads_count_replies(ads, res) != 1) {
144 goto done;
147 result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
149 done:
150 TALLOC_FREE(guid_bin);
151 ads_msgfree(ads, res);
152 return result;
156 /*********************************************************************
157 *********************************************************************/
159 ADS_STATUS ads_schema_path(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **schema_path)
161 ADS_STATUS status;
162 LDAPMessage *res;
163 const char *schema;
164 const char *attrs[] = { "schemaNamingContext", NULL };
166 status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
167 if (!ADS_ERR_OK(status)) {
168 return status;
171 if ( (schema = ads_pull_string(ads, mem_ctx, res, "schemaNamingContext")) == NULL ) {
172 ads_msgfree(ads, res);
173 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
176 if ( (*schema_path = talloc_strdup(mem_ctx, schema)) == NULL ) {
177 ads_msgfree(ads, res);
178 return ADS_ERROR(LDAP_NO_MEMORY);
181 ads_msgfree(ads, res);
183 return status;
187 * Check for "Services for Unix" or rfc2307 Schema and load some attributes into the ADS_STRUCT
188 * @param ads connection to ads server
189 * @param enum mapping type
190 * @return ADS_STATUS status of search (False if one or more attributes couldn't be
191 * found in Active Directory)
193 ADS_STATUS ads_check_posix_schema_mapping(TALLOC_CTX *mem_ctx,
194 ADS_STRUCT *ads,
195 enum wb_posix_mapping map_type,
196 struct posix_schema **s )
198 TALLOC_CTX *ctx = NULL;
199 ADS_STATUS status;
200 char **oids_out, **names_out;
201 size_t num_names;
202 char *schema_path = NULL;
203 int i;
204 struct posix_schema *schema = NULL;
206 const char *oids_sfu[] = { ADS_ATTR_SFU_UIDNUMBER_OID,
207 ADS_ATTR_SFU_GIDNUMBER_OID,
208 ADS_ATTR_SFU_HOMEDIR_OID,
209 ADS_ATTR_SFU_SHELL_OID,
210 ADS_ATTR_SFU_GECOS_OID,
211 ADS_ATTR_SFU_UID_OID };
213 const char *oids_sfu20[] = { ADS_ATTR_SFU20_UIDNUMBER_OID,
214 ADS_ATTR_SFU20_GIDNUMBER_OID,
215 ADS_ATTR_SFU20_HOMEDIR_OID,
216 ADS_ATTR_SFU20_SHELL_OID,
217 ADS_ATTR_SFU20_GECOS_OID,
218 ADS_ATTR_SFU20_UID_OID };
220 const char *oids_rfc2307[] = { ADS_ATTR_RFC2307_UIDNUMBER_OID,
221 ADS_ATTR_RFC2307_GIDNUMBER_OID,
222 ADS_ATTR_RFC2307_HOMEDIR_OID,
223 ADS_ATTR_RFC2307_SHELL_OID,
224 ADS_ATTR_RFC2307_GECOS_OID,
225 ADS_ATTR_RFC2307_UID_OID };
227 DEBUG(10,("ads_check_posix_schema_mapping for schema mode: %d\n", map_type));
229 switch (map_type) {
231 case WB_POSIX_MAP_TEMPLATE:
232 case WB_POSIX_MAP_UNIXINFO:
233 DEBUG(10,("ads_check_posix_schema_mapping: nothing to do\n"));
234 return ADS_ERROR(LDAP_SUCCESS);
236 case WB_POSIX_MAP_SFU:
237 case WB_POSIX_MAP_SFU20:
238 case WB_POSIX_MAP_RFC2307:
239 break;
241 default:
242 DEBUG(0,("ads_check_posix_schema_mapping: "
243 "unknown enum %d\n", map_type));
244 return ADS_ERROR(LDAP_PARAM_ERROR);
247 if ( (ctx = talloc_init("ads_check_posix_schema_mapping")) == NULL ) {
248 return ADS_ERROR(LDAP_NO_MEMORY);
251 if ( (schema = talloc(mem_ctx, struct posix_schema)) == NULL ) {
252 TALLOC_FREE( ctx );
253 return ADS_ERROR(LDAP_NO_MEMORY);
256 status = ads_schema_path(ads, ctx, &schema_path);
257 if (!ADS_ERR_OK(status)) {
258 DEBUG(3,("ads_check_posix_mapping: Unable to retrieve schema DN!\n"));
259 goto done;
262 switch (map_type) {
263 case WB_POSIX_MAP_SFU:
264 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu,
265 ARRAY_SIZE(oids_sfu),
266 &oids_out, &names_out, &num_names);
267 break;
268 case WB_POSIX_MAP_SFU20:
269 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu20,
270 ARRAY_SIZE(oids_sfu20),
271 &oids_out, &names_out, &num_names);
272 break;
273 case WB_POSIX_MAP_RFC2307:
274 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_rfc2307,
275 ARRAY_SIZE(oids_rfc2307),
276 &oids_out, &names_out, &num_names);
277 break;
278 default:
279 status = ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
280 break;
283 if (!ADS_ERR_OK(status)) {
284 DEBUG(3,("ads_check_posix_schema_mapping: failed %s\n",
285 ads_errstr(status)));
286 goto done;
289 for (i=0; i<num_names; i++) {
291 DEBUGADD(10,("\tOID %s has name: %s\n", oids_out[i], names_out[i]));
293 if (strequal(ADS_ATTR_RFC2307_UIDNUMBER_OID, oids_out[i]) ||
294 strequal(ADS_ATTR_SFU_UIDNUMBER_OID, oids_out[i]) ||
295 strequal(ADS_ATTR_SFU20_UIDNUMBER_OID, oids_out[i])) {
296 schema->posix_uidnumber_attr = talloc_strdup(schema, names_out[i]);
297 continue;
300 if (strequal(ADS_ATTR_RFC2307_GIDNUMBER_OID, oids_out[i]) ||
301 strequal(ADS_ATTR_SFU_GIDNUMBER_OID, oids_out[i]) ||
302 strequal(ADS_ATTR_SFU20_GIDNUMBER_OID, oids_out[i])) {
303 schema->posix_gidnumber_attr = talloc_strdup(schema, names_out[i]);
304 continue;
307 if (strequal(ADS_ATTR_RFC2307_HOMEDIR_OID, oids_out[i]) ||
308 strequal(ADS_ATTR_SFU_HOMEDIR_OID, oids_out[i]) ||
309 strequal(ADS_ATTR_SFU20_HOMEDIR_OID, oids_out[i])) {
310 schema->posix_homedir_attr = talloc_strdup(schema, names_out[i]);
311 continue;
314 if (strequal(ADS_ATTR_RFC2307_SHELL_OID, oids_out[i]) ||
315 strequal(ADS_ATTR_SFU_SHELL_OID, oids_out[i]) ||
316 strequal(ADS_ATTR_SFU20_SHELL_OID, oids_out[i])) {
317 schema->posix_shell_attr = talloc_strdup(schema, names_out[i]);
318 continue;
321 if (strequal(ADS_ATTR_RFC2307_GECOS_OID, oids_out[i]) ||
322 strequal(ADS_ATTR_SFU_GECOS_OID, oids_out[i]) ||
323 strequal(ADS_ATTR_SFU20_GECOS_OID, oids_out[i])) {
324 schema->posix_gecos_attr = talloc_strdup(schema, names_out[i]);
327 if (strequal(ADS_ATTR_RFC2307_UID_OID, oids_out[i]) ||
328 strequal(ADS_ATTR_SFU_UID_OID, oids_out[i]) ||
329 strequal(ADS_ATTR_SFU20_UID_OID, oids_out[i])) {
330 schema->posix_uid_attr = talloc_strdup(schema, names_out[i]);
334 if (!schema->posix_uidnumber_attr ||
335 !schema->posix_gidnumber_attr ||
336 !schema->posix_homedir_attr ||
337 !schema->posix_shell_attr ||
338 !schema->posix_gecos_attr) {
339 status = ADS_ERROR(LDAP_NO_MEMORY);
340 TALLOC_FREE( schema );
341 goto done;
344 *s = schema;
346 status = ADS_ERROR(LDAP_SUCCESS);
348 done:
349 TALLOC_FREE(ctx);
351 return status;
354 #endif