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