r17094: merge fixes for 3.0.23a
[Samba/gbeck.git] / source / libads / ldap_schema.c
blobb65ff956ac9b4029de7cece94ac46a4bf615d315
1 /*
2 Unix SMB/CIFS implementation.
3 ads (active directory) utility library
4 Copyright (C) Guenther Deschner 2005-2006
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.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 void *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(expr, "(attributeId=%s)",
53 OIDs[i])) == NULL) {
54 return ADS_ERROR(LDAP_NO_MEMORY);
58 if ((expr = talloc_asprintf_append(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_oid(ADS_STRUCT *ads, const char *schema_path, TALLOC_CTX *mem_ctx, const char * OID)
112 ADS_STATUS rc;
113 int count = 0;
114 void *res = NULL;
115 char *expr = NULL;
116 const char *attrs[] = { "lDAPDisplayName", NULL };
117 char *result;
119 if (ads == NULL || mem_ctx == NULL || OID == NULL) {
120 goto failed;
123 expr = talloc_asprintf(mem_ctx, "(attributeId=%s)", OID);
124 if (expr == NULL) {
125 goto failed;
128 rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE,
129 expr, attrs, &res);
130 if (!ADS_ERR_OK(rc)) {
131 goto failed;
134 count = ads_count_replies(ads, res);
135 if (count == 0 || !res) {
136 goto failed;
139 result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
140 ads_msgfree(ads, res);
142 return result;
144 failed:
145 DEBUG(0,("ads_get_attrname_by_oid: failed to retrieve name for oid: %s\n",
146 OID));
148 ads_msgfree(ads, res);
149 return NULL;
152 /*********************************************************************
153 *********************************************************************/
155 static ADS_STATUS ads_schema_path(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **schema_path)
157 ADS_STATUS status;
158 void *res;
159 const char *schema;
160 const char *attrs[] = { "schemaNamingContext", NULL };
162 status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
163 if (!ADS_ERR_OK(status)) {
164 return status;
167 if ( (schema = ads_pull_string(ads, mem_ctx, res, "schemaNamingContext")) == NULL ) {
168 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
171 if ( (*schema_path = talloc_strdup(mem_ctx, schema)) == NULL ) {
172 return ADS_ERROR(LDAP_NO_MEMORY);
175 ads_msgfree(ads, res);
177 return status;
181 * Check for "Services for Unix" or rfc2307 Schema and load some attributes into the ADS_STRUCT
182 * @param ads connection to ads server
183 * @param enum mapping type
184 * @return BOOL status of search (False if one or more attributes couldn't be
185 * found in Active Directory)
186 **/
187 ADS_STATUS ads_check_posix_schema_mapping(ADS_STRUCT *ads, enum wb_posix_mapping map_type)
189 TALLOC_CTX *ctx = NULL;
190 ADS_STATUS status;
191 char **oids_out, **names_out;
192 size_t num_names;
193 char *schema_path = NULL;
194 ADS_STRUCT *ads_s = ads;
195 int i;
197 const char *oids_sfu[] = { ADS_ATTR_SFU_UIDNUMBER_OID,
198 ADS_ATTR_SFU_GIDNUMBER_OID,
199 ADS_ATTR_SFU_HOMEDIR_OID,
200 ADS_ATTR_SFU_SHELL_OID,
201 ADS_ATTR_SFU_GECOS_OID};
203 const char *oids_rfc2307[] = { ADS_ATTR_RFC2307_UIDNUMBER_OID,
204 ADS_ATTR_RFC2307_GIDNUMBER_OID,
205 ADS_ATTR_RFC2307_HOMEDIR_OID,
206 ADS_ATTR_RFC2307_SHELL_OID,
207 ADS_ATTR_RFC2307_GECOS_OID };
209 DEBUG(10,("ads_check_posix_schema_mapping\n"));
211 switch (map_type) {
213 case WB_POSIX_MAP_TEMPLATE:
214 case WB_POSIX_MAP_UNIXINFO:
215 DEBUG(10,("ads_check_posix_schema_mapping: nothing to do\n"));
216 return ADS_ERROR(LDAP_SUCCESS);
218 case WB_POSIX_MAP_SFU:
219 case WB_POSIX_MAP_RFC2307:
220 break;
222 default:
223 DEBUG(0,("ads_check_posix_schema_mapping: "
224 "unknown enum %d\n", map_type));
225 return ADS_ERROR(LDAP_PARAM_ERROR);
228 ads->schema.posix_uidnumber_attr = NULL;
229 ads->schema.posix_gidnumber_attr = NULL;
230 ads->schema.posix_homedir_attr = NULL;
231 ads->schema.posix_shell_attr = NULL;
232 ads->schema.posix_gecos_attr = NULL;
234 ctx = talloc_init("ads_check_posix_schema_mapping");
235 if (ctx == NULL) {
236 return ADS_ERROR(LDAP_NO_MEMORY);
239 /* establish a new ldap tcp session if necessary */
241 if (!ads->ld) {
242 if ((ads_s = ads_init(ads->server.realm, ads->server.workgroup,
243 ads->server.ldap_server)) == NULL) {
244 status = ADS_ERROR(LDAP_SERVER_DOWN);
245 goto done;
248 ads_s->auth.flags = ADS_AUTH_ANON_BIND;
249 status = ads_connect(ads_s);
250 if (!ADS_ERR_OK(status)) {
251 goto done;
255 status = ads_schema_path(ads, ctx, &schema_path);
256 if (!ADS_ERR_OK(status)) {
257 DEBUG(3,("ads_check_posix_mapping: Unable to retrieve schema DN!\n"));
258 goto done;
261 if (map_type == WB_POSIX_MAP_SFU) {
262 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu,
263 ARRAY_SIZE(oids_sfu),
264 &oids_out, &names_out, &num_names);
265 } else {
266 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_rfc2307,
267 ARRAY_SIZE(oids_rfc2307),
268 &oids_out, &names_out, &num_names);
271 if (!ADS_ERR_OK(status)) {
272 DEBUG(3,("ads_check_posix_schema_mapping: failed %s\n",
273 ads_errstr(status)));
274 goto done;
277 DEBUG(10,("ads_check_posix_schema_mapping: query succeeded, identified: %s\n",
278 wb_posix_map_str(map_type)));
280 for (i=0; i<num_names; i++) {
282 DEBUGADD(10,("\tOID %s has name: %s\n", oids_out[i], names_out[i]));
284 if (strequal(ADS_ATTR_RFC2307_UIDNUMBER_OID, oids_out[i]) ||
285 strequal(ADS_ATTR_SFU_UIDNUMBER_OID, oids_out[i])) {
286 SAFE_FREE(ads->schema.posix_uidnumber_attr);
287 ads->schema.posix_uidnumber_attr = SMB_STRDUP(names_out[i]);
289 if (strequal(ADS_ATTR_RFC2307_GIDNUMBER_OID, oids_out[i]) ||
290 strequal(ADS_ATTR_SFU_GIDNUMBER_OID, oids_out[i])) {
291 SAFE_FREE(ads->schema.posix_gidnumber_attr);
292 ads->schema.posix_gidnumber_attr = SMB_STRDUP(names_out[i]);
294 if (strequal(ADS_ATTR_RFC2307_HOMEDIR_OID, oids_out[i]) ||
295 strequal(ADS_ATTR_SFU_HOMEDIR_OID, oids_out[i])) {
296 SAFE_FREE(ads->schema.posix_homedir_attr);
297 ads->schema.posix_homedir_attr = SMB_STRDUP(names_out[i]);
299 if (strequal(ADS_ATTR_RFC2307_SHELL_OID, oids_out[i]) ||
300 strequal(ADS_ATTR_SFU_SHELL_OID, oids_out[i])) {
301 SAFE_FREE(ads->schema.posix_shell_attr);
302 ads->schema.posix_shell_attr = SMB_STRDUP(names_out[i]);
304 if (strequal(ADS_ATTR_RFC2307_GECOS_OID, oids_out[i]) ||
305 strequal(ADS_ATTR_SFU_GECOS_OID, oids_out[i])) {
306 SAFE_FREE(ads->schema.posix_gecos_attr);
307 ads->schema.posix_gecos_attr = SMB_STRDUP(names_out[i]);
311 if (!ads->schema.posix_uidnumber_attr ||
312 !ads->schema.posix_gidnumber_attr ||
313 !ads->schema.posix_homedir_attr ||
314 !ads->schema.posix_shell_attr ||
315 !ads->schema.posix_gecos_attr) {
316 status = ADS_ERROR(LDAP_NO_MEMORY);
317 goto done;
320 status = ADS_ERROR(LDAP_SUCCESS);
322 ads->schema.map_type = map_type;
323 done:
324 /* free any temporary ads connections */
325 if (ads_s != ads) {
326 ads_destroy(&ads_s);
328 if (ctx) {
329 talloc_destroy(ctx);
332 return status;
335 #endif