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/>.
23 #include "libads/ldap_schema.h"
24 #include "../libcli/ldap/ldap_ndr.h"
28 static ADS_STATUS ads_get_attrnames_by_oids(ADS_STRUCT *ads,
30 const char *schema_path,
33 char ***OIDs_out, char ***names,
37 LDAPMessage *res = NULL;
40 const char *attrs[] = { "lDAPDisplayName", "attributeId", NULL };
43 if (!ads || !mem_ctx || !names || !count || !OIDs || !OIDs_out) {
44 return ADS_ERROR(LDAP_PARAM_ERROR);
47 if (num_OIDs == 0 || OIDs[0] == NULL) {
48 return ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
51 if ((expr = talloc_asprintf(mem_ctx, "(|")) == NULL) {
52 return ADS_ERROR(LDAP_NO_MEMORY);
55 for (i=0; i<num_OIDs; i++) {
57 if ((expr = talloc_asprintf_append_buffer(expr, "(attributeId=%s)",
59 return ADS_ERROR(LDAP_NO_MEMORY);
63 if ((expr = talloc_asprintf_append_buffer(expr, ")")) == NULL) {
64 return ADS_ERROR(LDAP_NO_MEMORY);
67 status = ads_do_search_retry(ads, schema_path,
68 LDAP_SCOPE_SUBTREE, expr, attrs, &res);
69 if (!ADS_ERR_OK(status)) {
73 *count = ads_count_replies(ads, res);
74 if (*count == 0 || !res) {
75 status = ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
79 if (((*names) = talloc_array(mem_ctx, char *, *count)) == NULL) {
80 status = ADS_ERROR(LDAP_NO_MEMORY);
83 if (((*OIDs_out) = talloc_array(mem_ctx, char *, *count)) == NULL) {
84 status = ADS_ERROR(LDAP_NO_MEMORY);
88 for (msg = ads_first_entry(ads, res); msg != NULL;
89 msg = ads_next_entry(ads, msg)) {
91 (*names)[p] = ads_pull_string(ads, mem_ctx, msg,
93 (*OIDs_out)[p] = ads_pull_string(ads, mem_ctx, msg,
95 if (((*names)[p] == NULL) || ((*OIDs_out)[p] == NULL)) {
96 status = ADS_ERROR(LDAP_NO_MEMORY);
103 if (*count < num_OIDs) {
104 status = ADS_ERROR_NT(STATUS_SOME_UNMAPPED);
108 status = ADS_ERROR(LDAP_SUCCESS);
110 ads_msgfree(ads, res);
115 const char *ads_get_attrname_by_guid(ADS_STRUCT *ads,
116 const char *schema_path,
118 const struct GUID *schema_guid)
121 LDAPMessage *res = NULL;
123 const char *attrs[] = { "lDAPDisplayName", NULL };
124 const char *result = NULL;
125 char *guid_bin = NULL;
127 if (!ads || !mem_ctx || !schema_guid) {
131 guid_bin = ldap_encode_ndr_GUID(mem_ctx, schema_guid);
136 expr = talloc_asprintf(mem_ctx, "(schemaIDGUID=%s)", guid_bin);
141 rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE,
143 if (!ADS_ERR_OK(rc)) {
147 if (ads_count_replies(ads, res) != 1) {
151 result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
154 TALLOC_FREE(guid_bin);
155 ads_msgfree(ads, res);
160 /*********************************************************************
161 *********************************************************************/
163 ADS_STATUS ads_schema_path(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **schema_path)
168 const char *attrs[] = { "schemaNamingContext", NULL };
170 status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
171 if (!ADS_ERR_OK(status)) {
175 if ( (schema = ads_pull_string(ads, mem_ctx, res, "schemaNamingContext")) == NULL ) {
176 ads_msgfree(ads, res);
177 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
180 if ( (*schema_path = talloc_strdup(mem_ctx, schema)) == NULL ) {
181 ads_msgfree(ads, res);
182 return ADS_ERROR(LDAP_NO_MEMORY);
185 ads_msgfree(ads, res);
191 * Check for "Services for Unix" or rfc2307 Schema and load some attributes into the ADS_STRUCT
192 * @param ads connection to ads server
193 * @param enum mapping type
194 * @return ADS_STATUS status of search (False if one or more attributes couldn't be
195 * found in Active Directory)
197 ADS_STATUS ads_check_posix_schema_mapping(TALLOC_CTX *mem_ctx,
199 enum wb_posix_mapping map_type,
200 struct posix_schema **s )
202 TALLOC_CTX *ctx = NULL;
204 char **oids_out, **names_out;
206 char *schema_path = NULL;
208 struct posix_schema *schema = NULL;
210 const char *oids_sfu[] = { ADS_ATTR_SFU_UIDNUMBER_OID,
211 ADS_ATTR_SFU_GIDNUMBER_OID,
212 ADS_ATTR_SFU_HOMEDIR_OID,
213 ADS_ATTR_SFU_SHELL_OID,
214 ADS_ATTR_SFU_GECOS_OID,
215 ADS_ATTR_SFU_UID_OID };
217 const char *oids_sfu20[] = { ADS_ATTR_SFU20_UIDNUMBER_OID,
218 ADS_ATTR_SFU20_GIDNUMBER_OID,
219 ADS_ATTR_SFU20_HOMEDIR_OID,
220 ADS_ATTR_SFU20_SHELL_OID,
221 ADS_ATTR_SFU20_GECOS_OID,
222 ADS_ATTR_SFU20_UID_OID };
224 const char *oids_rfc2307[] = { ADS_ATTR_RFC2307_UIDNUMBER_OID,
225 ADS_ATTR_RFC2307_GIDNUMBER_OID,
226 ADS_ATTR_RFC2307_HOMEDIR_OID,
227 ADS_ATTR_RFC2307_SHELL_OID,
228 ADS_ATTR_RFC2307_GECOS_OID,
229 ADS_ATTR_RFC2307_UID_OID };
231 DEBUG(10,("ads_check_posix_schema_mapping for schema mode: %d\n", map_type));
235 case WB_POSIX_MAP_TEMPLATE:
236 case WB_POSIX_MAP_UNIXINFO:
237 DEBUG(10,("ads_check_posix_schema_mapping: nothing to do\n"));
238 return ADS_ERROR(LDAP_SUCCESS);
240 case WB_POSIX_MAP_SFU:
241 case WB_POSIX_MAP_SFU20:
242 case WB_POSIX_MAP_RFC2307:
246 DEBUG(0,("ads_check_posix_schema_mapping: "
247 "unknown enum %d\n", map_type));
248 return ADS_ERROR(LDAP_PARAM_ERROR);
251 if ( (ctx = talloc_init("ads_check_posix_schema_mapping")) == NULL ) {
252 return ADS_ERROR(LDAP_NO_MEMORY);
255 if ( (schema = talloc(mem_ctx, struct posix_schema)) == NULL ) {
257 return ADS_ERROR(LDAP_NO_MEMORY);
260 status = ads_schema_path(ads, ctx, &schema_path);
261 if (!ADS_ERR_OK(status)) {
262 DEBUG(3,("ads_check_posix_mapping: Unable to retrieve schema DN!\n"));
267 case WB_POSIX_MAP_SFU:
268 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu,
269 ARRAY_SIZE(oids_sfu),
270 &oids_out, &names_out, &num_names);
272 case WB_POSIX_MAP_SFU20:
273 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu20,
274 ARRAY_SIZE(oids_sfu20),
275 &oids_out, &names_out, &num_names);
277 case WB_POSIX_MAP_RFC2307:
278 status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_rfc2307,
279 ARRAY_SIZE(oids_rfc2307),
280 &oids_out, &names_out, &num_names);
283 status = ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
287 if (!ADS_ERR_OK(status)) {
288 DEBUG(3,("ads_check_posix_schema_mapping: failed %s\n",
289 ads_errstr(status)));
293 for (i=0; i<num_names; i++) {
295 DEBUGADD(10,("\tOID %s has name: %s\n", oids_out[i], names_out[i]));
297 if (strequal(ADS_ATTR_RFC2307_UIDNUMBER_OID, oids_out[i]) ||
298 strequal(ADS_ATTR_SFU_UIDNUMBER_OID, oids_out[i]) ||
299 strequal(ADS_ATTR_SFU20_UIDNUMBER_OID, oids_out[i])) {
300 schema->posix_uidnumber_attr = talloc_strdup(schema, names_out[i]);
304 if (strequal(ADS_ATTR_RFC2307_GIDNUMBER_OID, oids_out[i]) ||
305 strequal(ADS_ATTR_SFU_GIDNUMBER_OID, oids_out[i]) ||
306 strequal(ADS_ATTR_SFU20_GIDNUMBER_OID, oids_out[i])) {
307 schema->posix_gidnumber_attr = talloc_strdup(schema, names_out[i]);
311 if (strequal(ADS_ATTR_RFC2307_HOMEDIR_OID, oids_out[i]) ||
312 strequal(ADS_ATTR_SFU_HOMEDIR_OID, oids_out[i]) ||
313 strequal(ADS_ATTR_SFU20_HOMEDIR_OID, oids_out[i])) {
314 schema->posix_homedir_attr = talloc_strdup(schema, names_out[i]);
318 if (strequal(ADS_ATTR_RFC2307_SHELL_OID, oids_out[i]) ||
319 strequal(ADS_ATTR_SFU_SHELL_OID, oids_out[i]) ||
320 strequal(ADS_ATTR_SFU20_SHELL_OID, oids_out[i])) {
321 schema->posix_shell_attr = talloc_strdup(schema, names_out[i]);
325 if (strequal(ADS_ATTR_RFC2307_GECOS_OID, oids_out[i]) ||
326 strequal(ADS_ATTR_SFU_GECOS_OID, oids_out[i]) ||
327 strequal(ADS_ATTR_SFU20_GECOS_OID, oids_out[i])) {
328 schema->posix_gecos_attr = talloc_strdup(schema, names_out[i]);
331 if (strequal(ADS_ATTR_RFC2307_UID_OID, oids_out[i]) ||
332 strequal(ADS_ATTR_SFU_UID_OID, oids_out[i]) ||
333 strequal(ADS_ATTR_SFU20_UID_OID, oids_out[i])) {
334 schema->posix_uid_attr = talloc_strdup(schema, names_out[i]);
338 if (!schema->posix_uidnumber_attr ||
339 !schema->posix_gidnumber_attr ||
340 !schema->posix_homedir_attr ||
341 !schema->posix_shell_attr ||
342 !schema->posix_gecos_attr) {
343 status = ADS_ERROR(LDAP_NO_MEMORY);
344 TALLOC_FREE( schema );
350 status = ADS_ERROR(LDAP_SUCCESS);