Line data Source code
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
6 :
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.
11 :
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.
16 :
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/>.
19 : */
20 :
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"
26 :
27 : #ifdef HAVE_LDAP
28 :
29 0 : 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)
36 : {
37 0 : ADS_STATUS status;
38 0 : LDAPMessage *res = NULL;
39 0 : LDAPMessage *msg;
40 0 : char *expr = NULL;
41 0 : const char *attrs[] = { "lDAPDisplayName", "attributeId", NULL };
42 0 : int i = 0, p = 0;
43 :
44 0 : if (!ads || !mem_ctx || !names || !count || !OIDs || !OIDs_out) {
45 0 : return ADS_ERROR(LDAP_PARAM_ERROR);
46 : }
47 :
48 0 : if (num_OIDs == 0 || OIDs[0] == NULL) {
49 0 : return ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
50 : }
51 :
52 0 : expr = talloc_asprintf(mem_ctx, "(|");
53 :
54 0 : for (i=0; i<num_OIDs; i++) {
55 0 : talloc_asprintf_addbuf(&expr, "(attributeId=%s)", OIDs[i]);
56 : }
57 :
58 0 : talloc_asprintf_addbuf(&expr, ")");
59 0 : if (expr == NULL) {
60 0 : return ADS_ERROR(LDAP_NO_MEMORY);
61 : }
62 :
63 0 : status = ads_do_search_retry(ads, schema_path,
64 : LDAP_SCOPE_SUBTREE, expr, attrs, &res);
65 0 : if (!ADS_ERR_OK(status)) {
66 0 : return status;
67 : }
68 :
69 0 : *count = ads_count_replies(ads, res);
70 0 : if (*count == 0 || !res) {
71 0 : status = ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
72 0 : goto out;
73 : }
74 :
75 0 : if (((*names) = talloc_array(mem_ctx, char *, *count)) == NULL) {
76 0 : status = ADS_ERROR(LDAP_NO_MEMORY);
77 0 : goto out;
78 : }
79 0 : if (((*OIDs_out) = talloc_array(mem_ctx, char *, *count)) == NULL) {
80 0 : status = ADS_ERROR(LDAP_NO_MEMORY);
81 0 : goto out;
82 : }
83 :
84 0 : for (msg = ads_first_entry(ads, res); msg != NULL;
85 0 : msg = ads_next_entry(ads, msg)) {
86 :
87 0 : (*names)[p] = ads_pull_string(ads, mem_ctx, msg,
88 : "lDAPDisplayName");
89 0 : (*OIDs_out)[p] = ads_pull_string(ads, mem_ctx, msg,
90 : "attributeId");
91 0 : if (((*names)[p] == NULL) || ((*OIDs_out)[p] == NULL)) {
92 0 : status = ADS_ERROR(LDAP_NO_MEMORY);
93 0 : goto out;
94 : }
95 :
96 0 : p++;
97 : }
98 :
99 0 : if (*count < num_OIDs) {
100 0 : status = ADS_ERROR_NT(STATUS_SOME_UNMAPPED);
101 0 : goto out;
102 : }
103 :
104 0 : status = ADS_ERROR(LDAP_SUCCESS);
105 0 : out:
106 0 : ads_msgfree(ads, res);
107 :
108 0 : return status;
109 : }
110 :
111 0 : 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)
115 : {
116 0 : ADS_STATUS rc;
117 0 : LDAPMessage *res = NULL;
118 0 : char *expr = NULL;
119 0 : const char *attrs[] = { "lDAPDisplayName", NULL };
120 0 : const char *result = NULL;
121 0 : char *guid_bin = NULL;
122 :
123 0 : if (!ads || !mem_ctx || !schema_guid) {
124 0 : goto done;
125 : }
126 :
127 0 : guid_bin = ldap_encode_ndr_GUID(mem_ctx, schema_guid);
128 0 : if (!guid_bin) {
129 0 : goto done;
130 : }
131 :
132 0 : expr = talloc_asprintf(mem_ctx, "(schemaIDGUID=%s)", guid_bin);
133 0 : if (!expr) {
134 0 : goto done;
135 : }
136 :
137 0 : rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE,
138 : expr, attrs, &res);
139 0 : if (!ADS_ERR_OK(rc)) {
140 0 : goto done;
141 : }
142 :
143 0 : if (ads_count_replies(ads, res) != 1) {
144 0 : goto done;
145 : }
146 :
147 0 : result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
148 :
149 0 : done:
150 0 : TALLOC_FREE(guid_bin);
151 0 : ads_msgfree(ads, res);
152 0 : return result;
153 :
154 : }
155 :
156 : /*********************************************************************
157 : *********************************************************************/
158 :
159 0 : ADS_STATUS ads_schema_path(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **schema_path)
160 : {
161 0 : ADS_STATUS status;
162 0 : LDAPMessage *res;
163 0 : const char *schema;
164 0 : const char *attrs[] = { "schemaNamingContext", NULL };
165 :
166 0 : status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
167 0 : if (!ADS_ERR_OK(status)) {
168 0 : return status;
169 : }
170 :
171 0 : if ( (schema = ads_pull_string(ads, mem_ctx, res, "schemaNamingContext")) == NULL ) {
172 0 : ads_msgfree(ads, res);
173 0 : return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
174 : }
175 :
176 0 : if ( (*schema_path = talloc_strdup(mem_ctx, schema)) == NULL ) {
177 0 : ads_msgfree(ads, res);
178 0 : return ADS_ERROR(LDAP_NO_MEMORY);
179 : }
180 :
181 0 : ads_msgfree(ads, res);
182 :
183 0 : return status;
184 : }
185 :
186 : /**
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)
192 : **/
193 0 : 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 )
197 : {
198 0 : TALLOC_CTX *ctx = NULL;
199 0 : ADS_STATUS status;
200 0 : char **oids_out, **names_out;
201 0 : size_t num_names;
202 0 : char *schema_path = NULL;
203 0 : int i;
204 0 : struct posix_schema *schema = NULL;
205 :
206 0 : 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 };
212 :
213 0 : 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 };
219 :
220 0 : 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 };
226 :
227 0 : DEBUG(10,("ads_check_posix_schema_mapping for schema mode: %d\n", map_type));
228 :
229 0 : switch (map_type) {
230 :
231 0 : case WB_POSIX_MAP_TEMPLATE:
232 : case WB_POSIX_MAP_UNIXINFO:
233 0 : DEBUG(10,("ads_check_posix_schema_mapping: nothing to do\n"));
234 0 : return ADS_ERROR(LDAP_SUCCESS);
235 :
236 0 : case WB_POSIX_MAP_SFU:
237 : case WB_POSIX_MAP_SFU20:
238 : case WB_POSIX_MAP_RFC2307:
239 0 : break;
240 :
241 0 : default:
242 0 : DEBUG(0,("ads_check_posix_schema_mapping: "
243 : "unknown enum %d\n", map_type));
244 0 : return ADS_ERROR(LDAP_PARAM_ERROR);
245 : }
246 :
247 0 : if ( (ctx = talloc_init("ads_check_posix_schema_mapping")) == NULL ) {
248 0 : return ADS_ERROR(LDAP_NO_MEMORY);
249 : }
250 :
251 0 : if ( (schema = talloc(mem_ctx, struct posix_schema)) == NULL ) {
252 0 : TALLOC_FREE( ctx );
253 0 : return ADS_ERROR(LDAP_NO_MEMORY);
254 : }
255 :
256 0 : status = ads_schema_path(ads, ctx, &schema_path);
257 0 : if (!ADS_ERR_OK(status)) {
258 0 : DEBUG(3,("ads_check_posix_mapping: Unable to retrieve schema DN!\n"));
259 0 : goto done;
260 : }
261 :
262 0 : switch (map_type) {
263 0 : case WB_POSIX_MAP_SFU:
264 0 : 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 0 : break;
268 0 : case WB_POSIX_MAP_SFU20:
269 0 : 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 0 : break;
273 0 : case WB_POSIX_MAP_RFC2307:
274 0 : 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 0 : break;
278 0 : default:
279 0 : status = ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
280 0 : break;
281 : }
282 :
283 0 : if (!ADS_ERR_OK(status)) {
284 0 : DEBUG(3,("ads_check_posix_schema_mapping: failed %s\n",
285 : ads_errstr(status)));
286 0 : goto done;
287 : }
288 :
289 0 : for (i=0; i<num_names; i++) {
290 :
291 0 : DEBUGADD(10,("\tOID %s has name: %s\n", oids_out[i], names_out[i]));
292 :
293 0 : if (strequal(ADS_ATTR_RFC2307_UIDNUMBER_OID, oids_out[i]) ||
294 0 : strequal(ADS_ATTR_SFU_UIDNUMBER_OID, oids_out[i]) ||
295 0 : strequal(ADS_ATTR_SFU20_UIDNUMBER_OID, oids_out[i])) {
296 0 : schema->posix_uidnumber_attr = talloc_strdup(schema, names_out[i]);
297 0 : continue;
298 : }
299 :
300 0 : if (strequal(ADS_ATTR_RFC2307_GIDNUMBER_OID, oids_out[i]) ||
301 0 : strequal(ADS_ATTR_SFU_GIDNUMBER_OID, oids_out[i]) ||
302 0 : strequal(ADS_ATTR_SFU20_GIDNUMBER_OID, oids_out[i])) {
303 0 : schema->posix_gidnumber_attr = talloc_strdup(schema, names_out[i]);
304 0 : continue;
305 : }
306 :
307 0 : if (strequal(ADS_ATTR_RFC2307_HOMEDIR_OID, oids_out[i]) ||
308 0 : strequal(ADS_ATTR_SFU_HOMEDIR_OID, oids_out[i]) ||
309 0 : strequal(ADS_ATTR_SFU20_HOMEDIR_OID, oids_out[i])) {
310 0 : schema->posix_homedir_attr = talloc_strdup(schema, names_out[i]);
311 0 : continue;
312 : }
313 :
314 0 : if (strequal(ADS_ATTR_RFC2307_SHELL_OID, oids_out[i]) ||
315 0 : strequal(ADS_ATTR_SFU_SHELL_OID, oids_out[i]) ||
316 0 : strequal(ADS_ATTR_SFU20_SHELL_OID, oids_out[i])) {
317 0 : schema->posix_shell_attr = talloc_strdup(schema, names_out[i]);
318 0 : continue;
319 : }
320 :
321 0 : if (strequal(ADS_ATTR_RFC2307_GECOS_OID, oids_out[i]) ||
322 0 : strequal(ADS_ATTR_SFU_GECOS_OID, oids_out[i]) ||
323 0 : strequal(ADS_ATTR_SFU20_GECOS_OID, oids_out[i])) {
324 0 : schema->posix_gecos_attr = talloc_strdup(schema, names_out[i]);
325 : }
326 :
327 0 : if (strequal(ADS_ATTR_RFC2307_UID_OID, oids_out[i]) ||
328 0 : strequal(ADS_ATTR_SFU_UID_OID, oids_out[i]) ||
329 0 : strequal(ADS_ATTR_SFU20_UID_OID, oids_out[i])) {
330 0 : schema->posix_uid_attr = talloc_strdup(schema, names_out[i]);
331 : }
332 : }
333 :
334 0 : if (!schema->posix_uidnumber_attr ||
335 0 : !schema->posix_gidnumber_attr ||
336 0 : !schema->posix_homedir_attr ||
337 0 : !schema->posix_shell_attr ||
338 0 : !schema->posix_gecos_attr) {
339 0 : status = ADS_ERROR(LDAP_NO_MEMORY);
340 0 : TALLOC_FREE( schema );
341 0 : goto done;
342 : }
343 :
344 0 : *s = schema;
345 :
346 0 : status = ADS_ERROR(LDAP_SUCCESS);
347 :
348 0 : done:
349 0 : TALLOC_FREE(ctx);
350 :
351 0 : return status;
352 : }
353 :
354 : #endif
|