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/>.
25 ADS_STATUS
ads_get_attrnames_by_oids(ADS_STRUCT
*ads
, TALLOC_CTX
*mem_ctx
,
26 const char *schema_path
,
27 const char **OIDs
, size_t num_OIDs
,
28 char ***OIDs_out
, char ***names
, size_t *count
)
31 LDAPMessage
*res
= NULL
;
34 const char *attrs
[] = { "lDAPDisplayName", "attributeId", NULL
};
37 if (!ads
|| !mem_ctx
|| !names
|| !count
|| !OIDs
|| !OIDs_out
) {
38 return ADS_ERROR(LDAP_PARAM_ERROR
);
41 if (num_OIDs
== 0 || OIDs
[0] == NULL
) {
42 return ADS_ERROR_NT(NT_STATUS_NONE_MAPPED
);
45 if ((expr
= talloc_asprintf(mem_ctx
, "(|")) == NULL
) {
46 return ADS_ERROR(LDAP_NO_MEMORY
);
49 for (i
=0; i
<num_OIDs
; i
++) {
51 if ((expr
= talloc_asprintf_append_buffer(expr
, "(attributeId=%s)",
53 return ADS_ERROR(LDAP_NO_MEMORY
);
57 if ((expr
= talloc_asprintf_append_buffer(expr
, ")")) == NULL
) {
58 return ADS_ERROR(LDAP_NO_MEMORY
);
61 status
= ads_do_search_retry(ads
, schema_path
,
62 LDAP_SCOPE_SUBTREE
, expr
, attrs
, &res
);
63 if (!ADS_ERR_OK(status
)) {
67 *count
= ads_count_replies(ads
, res
);
68 if (*count
== 0 || !res
) {
69 status
= ADS_ERROR_NT(NT_STATUS_NONE_MAPPED
);
73 if (((*names
) = TALLOC_ARRAY(mem_ctx
, char *, *count
)) == NULL
) {
74 status
= ADS_ERROR(LDAP_NO_MEMORY
);
77 if (((*OIDs_out
) = TALLOC_ARRAY(mem_ctx
, char *, *count
)) == NULL
) {
78 status
= ADS_ERROR(LDAP_NO_MEMORY
);
82 for (msg
= ads_first_entry(ads
, res
); msg
!= NULL
;
83 msg
= ads_next_entry(ads
, msg
)) {
85 (*names
)[p
] = ads_pull_string(ads
, mem_ctx
, msg
,
87 (*OIDs_out
)[p
] = ads_pull_string(ads
, mem_ctx
, msg
,
89 if (((*names
)[p
] == NULL
) || ((*OIDs_out
)[p
] == NULL
)) {
90 status
= ADS_ERROR(LDAP_NO_MEMORY
);
97 if (*count
< num_OIDs
) {
98 status
= ADS_ERROR_NT(STATUS_SOME_UNMAPPED
);
102 status
= ADS_ERROR(LDAP_SUCCESS
);
104 ads_msgfree(ads
, res
);
109 const char *ads_get_attrname_by_guid(ADS_STRUCT
*ads
,
110 const char *schema_path
,
112 const struct GUID
*schema_guid
)
115 LDAPMessage
*res
= NULL
;
117 const char *attrs
[] = { "lDAPDisplayName", NULL
};
118 const char *result
= NULL
;
119 char *guid_bin
= NULL
;
121 if (!ads
|| !mem_ctx
|| !schema_guid
) {
125 guid_bin
= guid_binstring(schema_guid
);
130 expr
= talloc_asprintf(mem_ctx
, "(schemaIDGUID=%s)", guid_bin
);
135 rc
= ads_do_search_retry(ads
, schema_path
, LDAP_SCOPE_SUBTREE
,
137 if (!ADS_ERR_OK(rc
)) {
141 if (ads_count_replies(ads
, res
) != 1) {
145 result
= ads_pull_string(ads
, mem_ctx
, res
, "lDAPDisplayName");
149 ads_msgfree(ads
, res
);
154 const char *ads_get_attrname_by_oid(ADS_STRUCT
*ads
, const char *schema_path
, TALLOC_CTX
*mem_ctx
, const char * OID
)
158 LDAPMessage
*res
= NULL
;
160 const char *attrs
[] = { "lDAPDisplayName", NULL
};
163 if (ads
== NULL
|| mem_ctx
== NULL
|| OID
== NULL
) {
167 expr
= talloc_asprintf(mem_ctx
, "(attributeId=%s)", OID
);
172 rc
= ads_do_search_retry(ads
, schema_path
, LDAP_SCOPE_SUBTREE
,
174 if (!ADS_ERR_OK(rc
)) {
178 count
= ads_count_replies(ads
, res
);
179 if (count
== 0 || !res
) {
183 result
= ads_pull_string(ads
, mem_ctx
, res
, "lDAPDisplayName");
184 ads_msgfree(ads
, res
);
189 DEBUG(0,("ads_get_attrname_by_oid: failed to retrieve name for oid: %s\n",
192 ads_msgfree(ads
, res
);
195 /*********************************************************************
196 *********************************************************************/
198 ADS_STATUS
ads_schema_path(ADS_STRUCT
*ads
, TALLOC_CTX
*mem_ctx
, char **schema_path
)
203 const char *attrs
[] = { "schemaNamingContext", NULL
};
205 status
= ads_do_search(ads
, "", LDAP_SCOPE_BASE
, "(objectclass=*)", attrs
, &res
);
206 if (!ADS_ERR_OK(status
)) {
210 if ( (schema
= ads_pull_string(ads
, mem_ctx
, res
, "schemaNamingContext")) == NULL
) {
211 ads_msgfree(ads
, res
);
212 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED
);
215 if ( (*schema_path
= talloc_strdup(mem_ctx
, schema
)) == NULL
) {
216 ads_msgfree(ads
, res
);
217 return ADS_ERROR(LDAP_NO_MEMORY
);
220 ads_msgfree(ads
, res
);
226 * Check for "Services for Unix" or rfc2307 Schema and load some attributes into the ADS_STRUCT
227 * @param ads connection to ads server
228 * @param enum mapping type
229 * @return ADS_STATUS status of search (False if one or more attributes couldn't be
230 * found in Active Directory)
232 ADS_STATUS
ads_check_posix_schema_mapping(TALLOC_CTX
*mem_ctx
,
234 enum wb_posix_mapping map_type
,
235 struct posix_schema
**s
)
237 TALLOC_CTX
*ctx
= NULL
;
239 char **oids_out
, **names_out
;
241 char *schema_path
= NULL
;
243 struct posix_schema
*schema
= NULL
;
245 const char *oids_sfu
[] = { ADS_ATTR_SFU_UIDNUMBER_OID
,
246 ADS_ATTR_SFU_GIDNUMBER_OID
,
247 ADS_ATTR_SFU_HOMEDIR_OID
,
248 ADS_ATTR_SFU_SHELL_OID
,
249 ADS_ATTR_SFU_GECOS_OID
};
251 const char *oids_sfu20
[] = { ADS_ATTR_SFU20_UIDNUMBER_OID
,
252 ADS_ATTR_SFU20_GIDNUMBER_OID
,
253 ADS_ATTR_SFU20_HOMEDIR_OID
,
254 ADS_ATTR_SFU20_SHELL_OID
,
255 ADS_ATTR_SFU20_GECOS_OID
};
257 const char *oids_rfc2307
[] = { ADS_ATTR_RFC2307_UIDNUMBER_OID
,
258 ADS_ATTR_RFC2307_GIDNUMBER_OID
,
259 ADS_ATTR_RFC2307_HOMEDIR_OID
,
260 ADS_ATTR_RFC2307_SHELL_OID
,
261 ADS_ATTR_RFC2307_GECOS_OID
};
263 DEBUG(10,("ads_check_posix_schema_mapping for schema mode: %d\n", map_type
));
267 case WB_POSIX_MAP_TEMPLATE
:
268 case WB_POSIX_MAP_UNIXINFO
:
269 DEBUG(10,("ads_check_posix_schema_mapping: nothing to do\n"));
270 return ADS_ERROR(LDAP_SUCCESS
);
272 case WB_POSIX_MAP_SFU
:
273 case WB_POSIX_MAP_SFU20
:
274 case WB_POSIX_MAP_RFC2307
:
278 DEBUG(0,("ads_check_posix_schema_mapping: "
279 "unknown enum %d\n", map_type
));
280 return ADS_ERROR(LDAP_PARAM_ERROR
);
283 if ( (ctx
= talloc_init("ads_check_posix_schema_mapping")) == NULL
) {
284 return ADS_ERROR(LDAP_NO_MEMORY
);
287 if ( (schema
= TALLOC_P(mem_ctx
, struct posix_schema
)) == NULL
) {
289 return ADS_ERROR(LDAP_NO_MEMORY
);
292 status
= ads_schema_path(ads
, ctx
, &schema_path
);
293 if (!ADS_ERR_OK(status
)) {
294 DEBUG(3,("ads_check_posix_mapping: Unable to retrieve schema DN!\n"));
299 case WB_POSIX_MAP_SFU
:
300 status
= ads_get_attrnames_by_oids(ads
, ctx
, schema_path
, oids_sfu
,
301 ARRAY_SIZE(oids_sfu
),
302 &oids_out
, &names_out
, &num_names
);
304 case WB_POSIX_MAP_SFU20
:
305 status
= ads_get_attrnames_by_oids(ads
, ctx
, schema_path
, oids_sfu20
,
306 ARRAY_SIZE(oids_sfu20
),
307 &oids_out
, &names_out
, &num_names
);
309 case WB_POSIX_MAP_RFC2307
:
310 status
= ads_get_attrnames_by_oids(ads
, ctx
, schema_path
, oids_rfc2307
,
311 ARRAY_SIZE(oids_rfc2307
),
312 &oids_out
, &names_out
, &num_names
);
315 status
= ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
319 if (!ADS_ERR_OK(status
)) {
320 DEBUG(3,("ads_check_posix_schema_mapping: failed %s\n",
321 ads_errstr(status
)));
325 for (i
=0; i
<num_names
; i
++) {
327 DEBUGADD(10,("\tOID %s has name: %s\n", oids_out
[i
], names_out
[i
]));
329 if (strequal(ADS_ATTR_RFC2307_UIDNUMBER_OID
, oids_out
[i
]) ||
330 strequal(ADS_ATTR_SFU_UIDNUMBER_OID
, oids_out
[i
]) ||
331 strequal(ADS_ATTR_SFU20_UIDNUMBER_OID
, oids_out
[i
])) {
332 schema
->posix_uidnumber_attr
= talloc_strdup(schema
, names_out
[i
]);
336 if (strequal(ADS_ATTR_RFC2307_GIDNUMBER_OID
, oids_out
[i
]) ||
337 strequal(ADS_ATTR_SFU_GIDNUMBER_OID
, oids_out
[i
]) ||
338 strequal(ADS_ATTR_SFU20_GIDNUMBER_OID
, oids_out
[i
])) {
339 schema
->posix_gidnumber_attr
= talloc_strdup(schema
, names_out
[i
]);
343 if (strequal(ADS_ATTR_RFC2307_HOMEDIR_OID
, oids_out
[i
]) ||
344 strequal(ADS_ATTR_SFU_HOMEDIR_OID
, oids_out
[i
]) ||
345 strequal(ADS_ATTR_SFU20_HOMEDIR_OID
, oids_out
[i
])) {
346 schema
->posix_homedir_attr
= talloc_strdup(schema
, names_out
[i
]);
350 if (strequal(ADS_ATTR_RFC2307_SHELL_OID
, oids_out
[i
]) ||
351 strequal(ADS_ATTR_SFU_SHELL_OID
, oids_out
[i
]) ||
352 strequal(ADS_ATTR_SFU20_SHELL_OID
, oids_out
[i
])) {
353 schema
->posix_shell_attr
= talloc_strdup(schema
, names_out
[i
]);
357 if (strequal(ADS_ATTR_RFC2307_GECOS_OID
, oids_out
[i
]) ||
358 strequal(ADS_ATTR_SFU_GECOS_OID
, oids_out
[i
]) ||
359 strequal(ADS_ATTR_SFU20_GECOS_OID
, oids_out
[i
])) {
360 schema
->posix_gecos_attr
= talloc_strdup(schema
, names_out
[i
]);
364 if (!schema
->posix_uidnumber_attr
||
365 !schema
->posix_gidnumber_attr
||
366 !schema
->posix_homedir_attr
||
367 !schema
->posix_shell_attr
||
368 !schema
->posix_gecos_attr
) {
369 status
= ADS_ERROR(LDAP_NO_MEMORY
);
370 TALLOC_FREE( schema
);
376 status
= ADS_ERROR(LDAP_SUCCESS
);