s3: avoid global include of ads.h.
[samba.git] / source3 / libads / ldap_schema.c
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
25 #ifdef HAVE_LDAP
26
27 ADS_STATUS ads_get_attrnames_by_oids(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
28                                      const char *schema_path,
29                                      const char **OIDs, size_t num_OIDs, 
30                                      char ***OIDs_out, char ***names, size_t *count)
31 {
32         ADS_STATUS status;
33         LDAPMessage *res = NULL;
34         LDAPMessage *msg;
35         char *expr = NULL;
36         const char *attrs[] = { "lDAPDisplayName", "attributeId", NULL };
37         int i = 0, p = 0;
38         
39         if (!ads || !mem_ctx || !names || !count || !OIDs || !OIDs_out) {
40                 return ADS_ERROR(LDAP_PARAM_ERROR);
41         }
42
43         if (num_OIDs == 0 || OIDs[0] == NULL) {
44                 return ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
45         }
46
47         if ((expr = talloc_asprintf(mem_ctx, "(|")) == NULL) {
48                 return ADS_ERROR(LDAP_NO_MEMORY);
49         }
50
51         for (i=0; i<num_OIDs; i++) {
52
53                 if ((expr = talloc_asprintf_append_buffer(expr, "(attributeId=%s)", 
54                                                    OIDs[i])) == NULL) {
55                         return ADS_ERROR(LDAP_NO_MEMORY);
56                 }
57         }
58
59         if ((expr = talloc_asprintf_append_buffer(expr, ")")) == NULL) {
60                 return ADS_ERROR(LDAP_NO_MEMORY);
61         }
62
63         status = ads_do_search_retry(ads, schema_path, 
64                                      LDAP_SCOPE_SUBTREE, expr, attrs, &res);
65         if (!ADS_ERR_OK(status)) {
66                 return status;
67         }
68
69         *count = ads_count_replies(ads, res);
70         if (*count == 0 || !res) {
71                 status = ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
72                 goto out;
73         }
74
75         if (((*names) = TALLOC_ARRAY(mem_ctx, char *, *count)) == NULL) {
76                 status = ADS_ERROR(LDAP_NO_MEMORY);
77                 goto out;
78         }
79         if (((*OIDs_out) = TALLOC_ARRAY(mem_ctx, char *, *count)) == NULL) {
80                 status = ADS_ERROR(LDAP_NO_MEMORY);
81                 goto out;
82         }
83
84         for (msg = ads_first_entry(ads, res); msg != NULL; 
85              msg = ads_next_entry(ads, msg)) {
86
87                 (*names)[p]     = ads_pull_string(ads, mem_ctx, msg, 
88                                                   "lDAPDisplayName");
89                 (*OIDs_out)[p]  = ads_pull_string(ads, mem_ctx, msg, 
90                                                   "attributeId");
91                 if (((*names)[p] == NULL) || ((*OIDs_out)[p] == NULL)) {
92                         status = ADS_ERROR(LDAP_NO_MEMORY);
93                         goto out;
94                 }
95
96                 p++;
97         }
98
99         if (*count < num_OIDs) {
100                 status = ADS_ERROR_NT(STATUS_SOME_UNMAPPED);
101                 goto out;
102         }
103
104         status = ADS_ERROR(LDAP_SUCCESS);
105 out:
106         ads_msgfree(ads, res);
107
108         return status;
109 }
110
111 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         ADS_STATUS rc;
117         LDAPMessage *res = NULL;
118         char *expr = NULL;
119         const char *attrs[] = { "lDAPDisplayName", NULL };
120         const char *result = NULL;
121         char *guid_bin = NULL;
122
123         if (!ads || !mem_ctx || !schema_guid) {
124                 goto done;
125         }
126
127         guid_bin = guid_binstring(mem_ctx, schema_guid);
128         if (!guid_bin) {
129                 goto done;
130         }
131
132         expr = talloc_asprintf(mem_ctx, "(schemaIDGUID=%s)", guid_bin);
133         if (!expr) {
134                 goto done;
135         }
136
137         rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE, 
138                                  expr, attrs, &res);
139         if (!ADS_ERR_OK(rc)) {
140                 goto done;
141         }
142
143         if (ads_count_replies(ads, res) != 1) {
144                 goto done;
145         }
146
147         result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
148
149  done:
150         TALLOC_FREE(guid_bin);
151         ads_msgfree(ads, res);
152         return result;
153         
154 }
155
156 const char *ads_get_attrname_by_oid(ADS_STRUCT *ads, const char *schema_path, TALLOC_CTX *mem_ctx, const char * OID)
157 {
158         ADS_STATUS rc;
159         int count = 0;
160         LDAPMessage *res = NULL;
161         char *expr = NULL;
162         const char *attrs[] = { "lDAPDisplayName", NULL };
163         char *result;
164
165         if (ads == NULL || mem_ctx == NULL || OID == NULL) {
166                 goto failed;
167         }
168
169         expr = talloc_asprintf(mem_ctx, "(attributeId=%s)", OID);
170         if (expr == NULL) {
171                 goto failed;
172         }
173
174         rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE, 
175                 expr, attrs, &res);
176         if (!ADS_ERR_OK(rc)) {
177                 goto failed;
178         }
179
180         count = ads_count_replies(ads, res);
181         if (count == 0 || !res) {
182                 goto failed;
183         }
184
185         result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
186         ads_msgfree(ads, res);
187
188         return result;
189         
190 failed:
191         DEBUG(0,("ads_get_attrname_by_oid: failed to retrieve name for oid: %s\n", 
192                 OID));
193         
194         ads_msgfree(ads, res);
195         return NULL;
196 }
197 /*********************************************************************
198 *********************************************************************/
199
200 ADS_STATUS ads_schema_path(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **schema_path)
201 {
202         ADS_STATUS status;
203         LDAPMessage *res;
204         const char *schema;
205         const char *attrs[] = { "schemaNamingContext", NULL };
206
207         status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
208         if (!ADS_ERR_OK(status)) {
209                 return status;
210         }
211
212         if ( (schema = ads_pull_string(ads, mem_ctx, res, "schemaNamingContext")) == NULL ) {
213                 ads_msgfree(ads, res);
214                 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
215         }
216
217         if ( (*schema_path = talloc_strdup(mem_ctx, schema)) == NULL ) {
218                 ads_msgfree(ads, res);
219                 return ADS_ERROR(LDAP_NO_MEMORY);
220         }
221
222         ads_msgfree(ads, res);
223
224         return status;
225 }
226
227 /**
228  * Check for "Services for Unix" or rfc2307 Schema and load some attributes into the ADS_STRUCT
229  * @param ads connection to ads server
230  * @param enum mapping type
231  * @return ADS_STATUS status of search (False if one or more attributes couldn't be
232  * found in Active Directory)
233  **/ 
234 ADS_STATUS ads_check_posix_schema_mapping(TALLOC_CTX *mem_ctx,
235                                           ADS_STRUCT *ads,
236                                           enum wb_posix_mapping map_type,
237                                           struct posix_schema **s ) 
238 {
239         TALLOC_CTX *ctx = NULL; 
240         ADS_STATUS status;
241         char **oids_out, **names_out;
242         size_t num_names;
243         char *schema_path = NULL;
244         int i;
245         struct posix_schema *schema = NULL;
246
247         const char *oids_sfu[] = {      ADS_ATTR_SFU_UIDNUMBER_OID,
248                                         ADS_ATTR_SFU_GIDNUMBER_OID,
249                                         ADS_ATTR_SFU_HOMEDIR_OID,
250                                         ADS_ATTR_SFU_SHELL_OID,
251                                         ADS_ATTR_SFU_GECOS_OID,
252                                         ADS_ATTR_SFU_UID_OID };
253
254         const char *oids_sfu20[] = {    ADS_ATTR_SFU20_UIDNUMBER_OID,
255                                         ADS_ATTR_SFU20_GIDNUMBER_OID,
256                                         ADS_ATTR_SFU20_HOMEDIR_OID,
257                                         ADS_ATTR_SFU20_SHELL_OID,
258                                         ADS_ATTR_SFU20_GECOS_OID,
259                                         ADS_ATTR_SFU20_UID_OID };
260
261         const char *oids_rfc2307[] = {  ADS_ATTR_RFC2307_UIDNUMBER_OID,
262                                         ADS_ATTR_RFC2307_GIDNUMBER_OID,
263                                         ADS_ATTR_RFC2307_HOMEDIR_OID,
264                                         ADS_ATTR_RFC2307_SHELL_OID,
265                                         ADS_ATTR_RFC2307_GECOS_OID,
266                                         ADS_ATTR_RFC2307_UID_OID };
267
268         DEBUG(10,("ads_check_posix_schema_mapping for schema mode: %d\n", map_type));
269
270         switch (map_type) {
271         
272                 case WB_POSIX_MAP_TEMPLATE:
273                 case WB_POSIX_MAP_UNIXINFO:
274                         DEBUG(10,("ads_check_posix_schema_mapping: nothing to do\n"));
275                         return ADS_ERROR(LDAP_SUCCESS);
276
277                 case WB_POSIX_MAP_SFU:
278                 case WB_POSIX_MAP_SFU20:
279                 case WB_POSIX_MAP_RFC2307:
280                         break;
281
282                 default:
283                         DEBUG(0,("ads_check_posix_schema_mapping: "
284                                  "unknown enum %d\n", map_type));
285                         return ADS_ERROR(LDAP_PARAM_ERROR);
286         }
287
288         if ( (ctx = talloc_init("ads_check_posix_schema_mapping")) == NULL ) {
289                 return ADS_ERROR(LDAP_NO_MEMORY);
290         }
291
292         if ( (schema = TALLOC_P(mem_ctx, struct posix_schema)) == NULL ) {
293                 TALLOC_FREE( ctx );
294                 return ADS_ERROR(LDAP_NO_MEMORY);
295         }
296         
297         status = ads_schema_path(ads, ctx, &schema_path);
298         if (!ADS_ERR_OK(status)) {
299                 DEBUG(3,("ads_check_posix_mapping: Unable to retrieve schema DN!\n"));
300                 goto done;
301         }
302
303         switch (map_type) {
304                 case WB_POSIX_MAP_SFU:
305                         status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu, 
306                                                            ARRAY_SIZE(oids_sfu), 
307                                                            &oids_out, &names_out, &num_names);
308                         break;
309                 case WB_POSIX_MAP_SFU20:
310                         status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu20, 
311                                                            ARRAY_SIZE(oids_sfu20), 
312                                                            &oids_out, &names_out, &num_names);
313                         break;
314                 case WB_POSIX_MAP_RFC2307:
315                         status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_rfc2307, 
316                                                            ARRAY_SIZE(oids_rfc2307), 
317                                                            &oids_out, &names_out, &num_names);
318                         break;
319                 default:
320                         status = ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
321                         break;
322         }
323
324         if (!ADS_ERR_OK(status)) {
325                 DEBUG(3,("ads_check_posix_schema_mapping: failed %s\n", 
326                         ads_errstr(status)));
327                 goto done;
328         }
329
330         for (i=0; i<num_names; i++) {
331
332                 DEBUGADD(10,("\tOID %s has name: %s\n", oids_out[i], names_out[i]));
333
334                 if (strequal(ADS_ATTR_RFC2307_UIDNUMBER_OID, oids_out[i]) ||
335                     strequal(ADS_ATTR_SFU_UIDNUMBER_OID, oids_out[i]) ||
336                     strequal(ADS_ATTR_SFU20_UIDNUMBER_OID, oids_out[i])) {
337                         schema->posix_uidnumber_attr = talloc_strdup(schema, names_out[i]);
338                         continue;                      
339                 }
340
341                 if (strequal(ADS_ATTR_RFC2307_GIDNUMBER_OID, oids_out[i]) ||
342                     strequal(ADS_ATTR_SFU_GIDNUMBER_OID, oids_out[i]) ||
343                     strequal(ADS_ATTR_SFU20_GIDNUMBER_OID, oids_out[i])) {
344                         schema->posix_gidnumber_attr = talloc_strdup(schema, names_out[i]);
345                         continue;               
346                 }
347
348                 if (strequal(ADS_ATTR_RFC2307_HOMEDIR_OID, oids_out[i]) ||
349                     strequal(ADS_ATTR_SFU_HOMEDIR_OID, oids_out[i]) ||
350                     strequal(ADS_ATTR_SFU20_HOMEDIR_OID, oids_out[i])) {
351                         schema->posix_homedir_attr = talloc_strdup(schema, names_out[i]);
352                         continue;                       
353                 }
354
355                 if (strequal(ADS_ATTR_RFC2307_SHELL_OID, oids_out[i]) ||
356                     strequal(ADS_ATTR_SFU_SHELL_OID, oids_out[i]) ||
357                     strequal(ADS_ATTR_SFU20_SHELL_OID, oids_out[i])) {
358                         schema->posix_shell_attr = talloc_strdup(schema, names_out[i]);
359                         continue;                       
360                 }
361
362                 if (strequal(ADS_ATTR_RFC2307_GECOS_OID, oids_out[i]) ||
363                     strequal(ADS_ATTR_SFU_GECOS_OID, oids_out[i]) ||
364                     strequal(ADS_ATTR_SFU20_GECOS_OID, oids_out[i])) {
365                         schema->posix_gecos_attr = talloc_strdup(schema, names_out[i]);
366                 }
367
368                 if (strequal(ADS_ATTR_RFC2307_UID_OID, oids_out[i]) ||
369                     strequal(ADS_ATTR_SFU_UID_OID, oids_out[i]) ||
370                     strequal(ADS_ATTR_SFU20_UID_OID, oids_out[i])) {
371                         schema->posix_uid_attr = talloc_strdup(schema, names_out[i]);
372                 }
373         }
374
375         if (!schema->posix_uidnumber_attr ||
376             !schema->posix_gidnumber_attr ||
377             !schema->posix_homedir_attr ||
378             !schema->posix_shell_attr ||
379             !schema->posix_gecos_attr) {
380                 status = ADS_ERROR(LDAP_NO_MEMORY);
381                 TALLOC_FREE( schema );          
382                 goto done;
383         }
384
385         *s = schema;
386         
387         status = ADS_ERROR(LDAP_SUCCESS);
388         
389 done:
390         TALLOC_FREE(ctx);
391
392         return status;
393 }
394
395 #endif