WHATSNEW: Update changes.
[Samba.git] / source3 / libads / ldap_schema.c
blobc739286abbdc0c881120a968ff4c4b798261f2b5
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 "libads/ldap_schema.h"
24 #ifdef HAVE_LDAP
26 ADS_STATUS ads_get_attrnames_by_oids(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
27 const char *schema_path,
28 const char **OIDs, size_t num_OIDs,
29 char ***OIDs_out, char ***names, size_t *count)
31 ADS_STATUS status;
32 LDAPMessage *res = NULL;
33 LDAPMessage *msg;
34 char *expr = NULL;
35 const char *attrs[] = { "lDAPDisplayName", "attributeId", NULL };
36 int i = 0, p = 0;
38 if (!ads || !mem_ctx || !names || !count || !OIDs || !OIDs_out) {
39 return ADS_ERROR(LDAP_PARAM_ERROR);
42 if (num_OIDs == 0 || OIDs[0] == NULL) {
43 return ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
46 if ((expr = talloc_asprintf(mem_ctx, "(|")) == NULL) {
47 return ADS_ERROR(LDAP_NO_MEMORY);
50 for (i=0; i<num_OIDs; i++) {
52 if ((expr = talloc_asprintf_append_buffer(expr, "(attributeId=%s)",
53 OIDs[i])) == NULL) {
54 return ADS_ERROR(LDAP_NO_MEMORY);
58 if ((expr = talloc_asprintf_append_buffer(expr, ")")) == NULL) {
59 return ADS_ERROR(LDAP_NO_MEMORY);
62 status = ads_do_search_retry(ads, schema_path,
63 LDAP_SCOPE_SUBTREE, expr, attrs, &res);
64 if (!ADS_ERR_OK(status)) {
65 return status;
68 *count = ads_count_replies(ads, res);
69 if (*count == 0 || !res) {
70 status = ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
71 goto out;
74 if (((*names) = TALLOC_ARRAY(mem_ctx, char *, *count)) == NULL) {
75 status = ADS_ERROR(LDAP_NO_MEMORY);
76 goto out;
78 if (((*OIDs_out) = TALLOC_ARRAY(mem_ctx, char *, *count)) == NULL) {
79 status = ADS_ERROR(LDAP_NO_MEMORY);
80 goto out;
83 for (msg = ads_first_entry(ads, res); msg != NULL;
84 msg = ads_next_entry(ads, msg)) {
86 (*names)[p] = ads_pull_string(ads, mem_ctx, msg,
87 "lDAPDisplayName");
88 (*OIDs_out)[p] = ads_pull_string(ads, mem_ctx, msg,
89 "attributeId");
90 if (((*names)[p] == NULL) || ((*OIDs_out)[p] == NULL)) {
91 status = ADS_ERROR(LDAP_NO_MEMORY);
92 goto out;
95 p++;
98 if (*count < num_OIDs) {
99 status = ADS_ERROR_NT(STATUS_SOME_UNMAPPED);
100 goto out;
103 status = ADS_ERROR(LDAP_SUCCESS);
104 out:
105 ads_msgfree(ads, res);
107 return status;
110 const char *ads_get_attrname_by_guid(ADS_STRUCT *ads,
111 const char *schema_path,
112 TALLOC_CTX *mem_ctx,
113 const struct GUID *schema_guid)
115 ADS_STATUS rc;
116 LDAPMessage *res = NULL;
117 char *expr = NULL;
118 const char *attrs[] = { "lDAPDisplayName", NULL };
119 const char *result = NULL;
120 char *guid_bin = NULL;
122 if (!ads || !mem_ctx || !schema_guid) {
123 goto done;
126 guid_bin = guid_binstring(mem_ctx, schema_guid);
127 if (!guid_bin) {
128 goto done;
131 expr = talloc_asprintf(mem_ctx, "(schemaIDGUID=%s)", guid_bin);
132 if (!expr) {
133 goto done;
136 rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE,
137 expr, attrs, &res);
138 if (!ADS_ERR_OK(rc)) {
139 goto done;
142 if (ads_count_replies(ads, res) != 1) {
143 goto done;
146 result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
148 done:
149 TALLOC_FREE(guid_bin);
150 ads_msgfree(ads, res);
151 return result;
155 const char *ads_get_attrname_by_oid(ADS_STRUCT *ads, const char *schema_path, TALLOC_CTX *mem_ctx, const char * OID)
157 ADS_STATUS rc;
158 int count = 0;
159 LDAPMessage *res = NULL;
160 char *expr = NULL;
161 const char *attrs[] = { "lDAPDisplayName", NULL };
162 char *result;
164 if (ads == NULL || mem_ctx == NULL || OID == NULL) {
165 goto failed;
168 expr = talloc_asprintf(mem_ctx, "(attributeId=%s)", OID);
169 if (expr == NULL) {
170 goto failed;
173 rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE,
174 expr, attrs, &res);
175 if (!ADS_ERR_OK(rc)) {
176 goto failed;
179 count = ads_count_replies(ads, res);
180 if (count == 0 || !res) {
181 goto failed;
184 result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
185 ads_msgfree(ads, res);
187 return result;
189 failed:
190 DEBUG(0,("ads_get_attrname_by_oid: failed to retrieve name for oid: %s\n",
191 OID));
193 ads_msgfree(ads, res);
194 return NULL;
196 /*********************************************************************
197 *********************************************************************/
199 ADS_STATUS ads_schema_path(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **schema_path)
201 ADS_STATUS status;
202 LDAPMessage *res;
203 const char *schema;
204 const char *attrs[] = { "schemaNamingContext", NULL };
206 status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
207 if (!ADS_ERR_OK(status)) {
208 return status;
211 if ( (schema = ads_pull_string(ads, mem_ctx, res, "schemaNamingContext")) == NULL ) {
212 ads_msgfree(ads, res);
213 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
216 if ( (*schema_path = talloc_strdup(mem_ctx, schema)) == NULL ) {
217 ads_msgfree(ads, res);
218 return ADS_ERROR(LDAP_NO_MEMORY);
221 ads_msgfree(ads, res);
223 return status;
227 * Check for "Services for Unix" or rfc2307 Schema and load some attributes into the ADS_STRUCT
228 * @param ads connection to ads server
229 * @param enum mapping type
230 * @return ADS_STATUS status of search (False if one or more attributes couldn't be
231 * found in Active Directory)
232 **/
233 ADS_STATUS ads_check_posix_schema_mapping(TALLOC_CTX *mem_ctx,
234 ADS_STRUCT *ads,
235 enum wb_posix_mapping map_type,
236 struct posix_schema **s )
238 TALLOC_CTX *ctx = NULL;
239 ADS_STATUS status;
240 char **oids_out, **names_out;
241 size_t num_names;
242 char *schema_path = NULL;
243 int i;
244 struct posix_schema *schema = NULL;
246 const char *oids_sfu[] = { ADS_ATTR_SFU_UIDNUMBER_OID,
247 ADS_ATTR_SFU_GIDNUMBER_OID,
248 ADS_ATTR_SFU_HOMEDIR_OID,
249 ADS_ATTR_SFU_SHELL_OID,
250 ADS_ATTR_SFU_GECOS_OID,
251 ADS_ATTR_SFU_UID_OID };
253 const char *oids_sfu20[] = { ADS_ATTR_SFU20_UIDNUMBER_OID,
254 ADS_ATTR_SFU20_GIDNUMBER_OID,
255 ADS_ATTR_SFU20_HOMEDIR_OID,
256 ADS_ATTR_SFU20_SHELL_OID,
257 ADS_ATTR_SFU20_GECOS_OID,
258 ADS_ATTR_SFU20_UID_OID };
260 const char *oids_rfc2307[] = { ADS_ATTR_RFC2307_UIDNUMBER_OID,
261 ADS_ATTR_RFC2307_GIDNUMBER_OID,
262 ADS_ATTR_RFC2307_HOMEDIR_OID,
263 ADS_ATTR_RFC2307_SHELL_OID,
264 ADS_ATTR_RFC2307_GECOS_OID,
265 ADS_ATTR_RFC2307_UID_OID };
267 DEBUG(10,("ads_check_posix_schema_mapping for schema mode: %d\n", map_type));
269 switch (map_type) {
271 case WB_POSIX_MAP_TEMPLATE:
272 case WB_POSIX_MAP_UNIXINFO:
273 DEBUG(10,("ads_check_posix_schema_mapping: nothing to do\n"));
274 return ADS_ERROR(LDAP_SUCCESS);
276 case WB_POSIX_MAP_SFU:
277 case WB_POSIX_MAP_SFU20:
278 case WB_POSIX_MAP_RFC2307:
279 break;
281 default:
282 DEBUG(0,("ads_check_posix_schema_mapping: "
283 "unknown enum %d\n", map_type));
284 return ADS_ERROR(LDAP_PARAM_ERROR);
287 if ( (ctx = talloc_init("ads_check_posix_schema_mapping")) == NULL ) {
288 return ADS_ERROR(LDAP_NO_MEMORY);
291 if ( (schema = TALLOC_P(mem_ctx, struct posix_schema)) == NULL ) {
292 TALLOC_FREE( ctx );
293 return ADS_ERROR(LDAP_NO_MEMORY);
296 status = ads_schema_path(ads, ctx, &schema_path);
297 if (!ADS_ERR_OK(status)) {
298 DEBUG(3,("ads_check_posix_mapping: Unable to retrieve schema DN!\n"));
299 goto done;
302 switch (map_type) {
303 case WB_POSIX_MAP_SFU:
304 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu,
305 ARRAY_SIZE(oids_sfu),
306 &oids_out, &names_out, &num_names);
307 break;
308 case WB_POSIX_MAP_SFU20:
309 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu20,
310 ARRAY_SIZE(oids_sfu20),
311 &oids_out, &names_out, &num_names);
312 break;
313 case WB_POSIX_MAP_RFC2307:
314 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_rfc2307,
315 ARRAY_SIZE(oids_rfc2307),
316 &oids_out, &names_out, &num_names);
317 break;
318 default:
319 status = ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
320 break;
323 if (!ADS_ERR_OK(status)) {
324 DEBUG(3,("ads_check_posix_schema_mapping: failed %s\n",
325 ads_errstr(status)));
326 goto done;
329 for (i=0; i<num_names; i++) {
331 DEBUGADD(10,("\tOID %s has name: %s\n", oids_out[i], names_out[i]));
333 if (strequal(ADS_ATTR_RFC2307_UIDNUMBER_OID, oids_out[i]) ||
334 strequal(ADS_ATTR_SFU_UIDNUMBER_OID, oids_out[i]) ||
335 strequal(ADS_ATTR_SFU20_UIDNUMBER_OID, oids_out[i])) {
336 schema->posix_uidnumber_attr = talloc_strdup(schema, names_out[i]);
337 continue;
340 if (strequal(ADS_ATTR_RFC2307_GIDNUMBER_OID, oids_out[i]) ||
341 strequal(ADS_ATTR_SFU_GIDNUMBER_OID, oids_out[i]) ||
342 strequal(ADS_ATTR_SFU20_GIDNUMBER_OID, oids_out[i])) {
343 schema->posix_gidnumber_attr = talloc_strdup(schema, names_out[i]);
344 continue;
347 if (strequal(ADS_ATTR_RFC2307_HOMEDIR_OID, oids_out[i]) ||
348 strequal(ADS_ATTR_SFU_HOMEDIR_OID, oids_out[i]) ||
349 strequal(ADS_ATTR_SFU20_HOMEDIR_OID, oids_out[i])) {
350 schema->posix_homedir_attr = talloc_strdup(schema, names_out[i]);
351 continue;
354 if (strequal(ADS_ATTR_RFC2307_SHELL_OID, oids_out[i]) ||
355 strequal(ADS_ATTR_SFU_SHELL_OID, oids_out[i]) ||
356 strequal(ADS_ATTR_SFU20_SHELL_OID, oids_out[i])) {
357 schema->posix_shell_attr = talloc_strdup(schema, names_out[i]);
358 continue;
361 if (strequal(ADS_ATTR_RFC2307_GECOS_OID, oids_out[i]) ||
362 strequal(ADS_ATTR_SFU_GECOS_OID, oids_out[i]) ||
363 strequal(ADS_ATTR_SFU20_GECOS_OID, oids_out[i])) {
364 schema->posix_gecos_attr = talloc_strdup(schema, names_out[i]);
367 if (strequal(ADS_ATTR_RFC2307_UID_OID, oids_out[i]) ||
368 strequal(ADS_ATTR_SFU_UID_OID, oids_out[i]) ||
369 strequal(ADS_ATTR_SFU20_UID_OID, oids_out[i])) {
370 schema->posix_uid_attr = talloc_strdup(schema, names_out[i]);
374 if (!schema->posix_uidnumber_attr ||
375 !schema->posix_gidnumber_attr ||
376 !schema->posix_homedir_attr ||
377 !schema->posix_shell_attr ||
378 !schema->posix_gecos_attr) {
379 status = ADS_ERROR(LDAP_NO_MEMORY);
380 TALLOC_FREE( schema );
381 goto done;
384 *s = schema;
386 status = ADS_ERROR(LDAP_SUCCESS);
388 done:
389 TALLOC_FREE(ctx);
391 return status;
394 #endif