2672d883066c57e135804d91b100e6f362621e89
[gd/samba-autobuild/.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
23 #ifdef HAVE_LDAP
24
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)
29 {
30         ADS_STATUS status;
31         LDAPMessage *res = NULL;
32         LDAPMessage *msg;
33         char *expr = NULL;
34         const char *attrs[] = { "lDAPDisplayName", "attributeId", NULL };
35         int i = 0, p = 0;
36         
37         if (!ads || !mem_ctx || !names || !count || !OIDs || !OIDs_out) {
38                 return ADS_ERROR(LDAP_PARAM_ERROR);
39         }
40
41         if (num_OIDs == 0 || OIDs[0] == NULL) {
42                 return ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
43         }
44
45         if ((expr = talloc_asprintf(mem_ctx, "(|")) == NULL) {
46                 return ADS_ERROR(LDAP_NO_MEMORY);
47         }
48
49         for (i=0; i<num_OIDs; i++) {
50
51                 if ((expr = talloc_asprintf_append(expr, "(attributeId=%s)", 
52                                                    OIDs[i])) == NULL) {
53                         return ADS_ERROR(LDAP_NO_MEMORY);
54                 }
55         }
56
57         if ((expr = talloc_asprintf_append(expr, ")")) == NULL) {
58                 return ADS_ERROR(LDAP_NO_MEMORY);
59         }
60
61         status = ads_do_search_retry(ads, schema_path, 
62                                      LDAP_SCOPE_SUBTREE, expr, attrs, &res);
63         if (!ADS_ERR_OK(status)) {
64                 return status;
65         }
66
67         *count = ads_count_replies(ads, res);
68         if (*count == 0 || !res) {
69                 status = ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
70                 goto out;
71         }
72
73         if (((*names) = TALLOC_ARRAY(mem_ctx, char *, *count)) == NULL) {
74                 status = ADS_ERROR(LDAP_NO_MEMORY);
75                 goto out;
76         }
77         if (((*OIDs_out) = TALLOC_ARRAY(mem_ctx, char *, *count)) == NULL) {
78                 status = ADS_ERROR(LDAP_NO_MEMORY);
79                 goto out;
80         }
81
82         for (msg = ads_first_entry(ads, res); msg != NULL; 
83              msg = ads_next_entry(ads, msg)) {
84
85                 (*names)[p]     = ads_pull_string(ads, mem_ctx, msg, 
86                                                   "lDAPDisplayName");
87                 (*OIDs_out)[p]  = ads_pull_string(ads, mem_ctx, msg, 
88                                                   "attributeId");
89                 if (((*names)[p] == NULL) || ((*OIDs_out)[p] == NULL)) {
90                         status = ADS_ERROR(LDAP_NO_MEMORY);
91                         goto out;
92                 }
93
94                 p++;
95         }
96
97         if (*count < num_OIDs) {
98                 status = ADS_ERROR_NT(STATUS_SOME_UNMAPPED);
99                 goto out;
100         }
101
102         status = ADS_ERROR(LDAP_SUCCESS);
103 out:
104         ads_msgfree(ads, res);
105
106         return status;
107 }
108
109 const char *ads_get_attrname_by_guid(ADS_STRUCT *ads, 
110                                      const char *schema_path, 
111                                      TALLOC_CTX *mem_ctx, 
112                                      const char *schema_guid)
113 {
114         ADS_STATUS rc;
115         LDAPMessage *res = NULL;
116         char *expr = NULL;
117         const char *attrs[] = { "lDAPDisplayName", NULL };
118         const char *result = NULL;
119         struct GUID guid;
120         char *guid_bin = NULL;
121
122         if (!ads || !mem_ctx || !schema_guid) {
123                 goto done;
124         }
125
126         if (!NT_STATUS_IS_OK(GUID_from_string(schema_guid, &guid))) {
127                 goto done;
128         }
129
130         guid_bin = guid_binstring(&guid);
131         if (!guid_bin) {
132                 goto done;
133         }
134
135         expr = talloc_asprintf(mem_ctx, "(schemaIDGUID=%s)", guid_bin);
136         if (!expr) {
137                 goto done;
138         }
139
140         rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE, 
141                                  expr, attrs, &res);
142         if (!ADS_ERR_OK(rc)) {
143                 goto done;
144         }
145
146         if (ads_count_replies(ads, res) != 1) {
147                 goto done;
148         }
149
150         result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
151
152  done:
153         SAFE_FREE(guid_bin);
154         ads_msgfree(ads, res);
155         return result;
156         
157 }
158
159 const char *ads_get_attrname_by_oid(ADS_STRUCT *ads, const char *schema_path, TALLOC_CTX *mem_ctx, const char * OID)
160 {
161         ADS_STATUS rc;
162         int count = 0;
163         LDAPMessage *res = NULL;
164         char *expr = NULL;
165         const char *attrs[] = { "lDAPDisplayName", NULL };
166         char *result;
167
168         if (ads == NULL || mem_ctx == NULL || OID == NULL) {
169                 goto failed;
170         }
171
172         expr = talloc_asprintf(mem_ctx, "(attributeId=%s)", OID);
173         if (expr == NULL) {
174                 goto failed;
175         }
176
177         rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE, 
178                 expr, attrs, &res);
179         if (!ADS_ERR_OK(rc)) {
180                 goto failed;
181         }
182
183         count = ads_count_replies(ads, res);
184         if (count == 0 || !res) {
185                 goto failed;
186         }
187
188         result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
189         ads_msgfree(ads, res);
190
191         return result;
192         
193 failed:
194         DEBUG(0,("ads_get_attrname_by_oid: failed to retrieve name for oid: %s\n", 
195                 OID));
196         
197         ads_msgfree(ads, res);
198         return NULL;
199 }
200 /*********************************************************************
201 *********************************************************************/
202
203 static ADS_STATUS ads_schema_path(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **schema_path)
204 {
205         ADS_STATUS status;
206         LDAPMessage *res;
207         const char *schema;
208         const char *attrs[] = { "schemaNamingContext", NULL };
209
210         status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
211         if (!ADS_ERR_OK(status)) {
212                 return status;
213         }
214
215         if ( (schema = ads_pull_string(ads, mem_ctx, res, "schemaNamingContext")) == NULL ) {
216                 ads_msgfree(ads, res);
217                 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
218         }
219
220         if ( (*schema_path = talloc_strdup(mem_ctx, schema)) == NULL ) {
221                 ads_msgfree(ads, res);
222                 return ADS_ERROR(LDAP_NO_MEMORY);
223         }
224
225         ads_msgfree(ads, res);
226
227         return status;
228 }
229
230 /**
231  * Check for "Services for Unix" or rfc2307 Schema and load some attributes into the ADS_STRUCT
232  * @param ads connection to ads server
233  * @param enum mapping type
234  * @return ADS_STATUS status of search (False if one or more attributes couldn't be
235  * found in Active Directory)
236  **/ 
237 ADS_STATUS ads_check_posix_schema_mapping(TALLOC_CTX *mem_ctx,
238                                           ADS_STRUCT *ads,
239                                           enum wb_posix_mapping map_type,
240                                           struct posix_schema **s ) 
241 {
242         TALLOC_CTX *ctx = NULL; 
243         ADS_STATUS status;
244         char **oids_out, **names_out;
245         size_t num_names;
246         char *schema_path = NULL;
247         int i;
248         struct posix_schema *schema = NULL;
249
250         const char *oids_sfu[] = {      ADS_ATTR_SFU_UIDNUMBER_OID,
251                                         ADS_ATTR_SFU_GIDNUMBER_OID,
252                                         ADS_ATTR_SFU_HOMEDIR_OID,
253                                         ADS_ATTR_SFU_SHELL_OID,
254                                         ADS_ATTR_SFU_GECOS_OID};
255
256         const char *oids_sfu20[] = {    ADS_ATTR_SFU20_UIDNUMBER_OID,
257                                         ADS_ATTR_SFU20_GIDNUMBER_OID,
258                                         ADS_ATTR_SFU20_HOMEDIR_OID,
259                                         ADS_ATTR_SFU20_SHELL_OID,
260                                         ADS_ATTR_SFU20_GECOS_OID};
261
262         const char *oids_rfc2307[] = {  ADS_ATTR_RFC2307_UIDNUMBER_OID,
263                                         ADS_ATTR_RFC2307_GIDNUMBER_OID,
264                                         ADS_ATTR_RFC2307_HOMEDIR_OID,
265                                         ADS_ATTR_RFC2307_SHELL_OID,
266                                         ADS_ATTR_RFC2307_GECOS_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
369         if (!schema->posix_uidnumber_attr ||
370             !schema->posix_gidnumber_attr ||
371             !schema->posix_homedir_attr ||
372             !schema->posix_shell_attr ||
373             !schema->posix_gecos_attr) {
374                 status = ADS_ERROR(LDAP_NO_MEMORY);
375                 TALLOC_FREE( schema );          
376                 goto done;
377         }
378
379         *s = schema;
380         
381         status = ADS_ERROR(LDAP_SUCCESS);
382         
383 done:
384         TALLOC_FREE(ctx);
385
386         return status;
387 }
388
389 #endif