]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
a88b540cb506390d91870290e3a28bae22fdbb45
[openldap] / servers / slapd / root_dse.c
1 /* root_dse.c - Provides the Root DSA-Specific Entry */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2017 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22
23 #include "slap.h"
24 #include <ldif.h>
25 #include "lber_pvt.h"
26
27 #ifdef LDAP_SLAPI
28 #include "slapi/slapi.h"
29 #endif
30
31 static struct berval    builtin_supportedFeatures[] = {
32         BER_BVC(LDAP_FEATURE_MODIFY_INCREMENT),         /* Modify/increment */
33         BER_BVC(LDAP_FEATURE_ALL_OP_ATTRS),             /* All Op Attrs (+) */
34         BER_BVC(LDAP_FEATURE_OBJECTCLASS_ATTRS),        /* OCs in Attrs List (@class) */
35         BER_BVC(LDAP_FEATURE_ABSOLUTE_FILTERS),         /* (&) and (|) search filters */
36         BER_BVC(LDAP_FEATURE_LANGUAGE_TAG_OPTIONS),     /* Language Tag Options */
37         BER_BVC(LDAP_FEATURE_LANGUAGE_RANGE_OPTIONS),   /* Language Range Options */
38         BER_BVC(LDAP_FEATURE_SUBORDINATE_SCOPE),        /* "children" search scope */
39         BER_BVNULL
40 };
41 static struct berval    *supportedFeatures;
42
43 static Entry    *usr_attr = NULL;
44
45 /*
46  * allow modules to register functions that muck with the root DSE entry
47  */
48
49 typedef struct entry_info_t {
50         SLAP_ENTRY_INFO_FN      func;
51         void                    *arg;
52         struct entry_info_t     *next;
53 } entry_info_t;
54
55 static entry_info_t *extra_info;
56
57 int
58 entry_info_register( SLAP_ENTRY_INFO_FN func, void *arg )
59 {
60         entry_info_t    *ei = ch_calloc( 1, sizeof( entry_info_t ) );
61
62         ei->func = func;
63         ei->arg = arg;
64
65         ei->next = extra_info;
66         extra_info = ei;
67
68         return 0;
69 }
70
71 int
72 entry_info_unregister( SLAP_ENTRY_INFO_FN func, void *arg )
73 {
74         entry_info_t    **eip;
75
76         for ( eip = &extra_info; *eip != NULL; eip = &(*eip)->next ) {
77                 if ( (*eip)->func == func && (*eip)->arg == arg ) {
78                         entry_info_t    *ei = *eip;
79
80                         *eip = ei->next;
81
82                         ch_free( ei );
83
84                         return 0;
85                 }
86         }
87
88         return -1;
89 }
90
91 void
92 entry_info_destroy( void )
93 {
94         entry_info_t    **eip;
95
96         for ( eip = &extra_info; *eip != NULL;  ) {
97                 entry_info_t    *ei = *eip;
98
99                 eip = &(*eip)->next;
100
101                 ch_free( ei );
102         }
103 }
104
105 /*
106  * Allow modules to register supported features
107  */
108
109 static int
110 supported_feature_init( void )
111 {
112         int             i;
113
114         if ( supportedFeatures != NULL ) {
115                 return 0;
116         }
117
118         for ( i = 0; !BER_BVISNULL( &builtin_supportedFeatures[ i ] ); i++ )
119                 ;
120
121         supportedFeatures = ch_calloc( sizeof( struct berval ), i + 1 );
122         if ( supportedFeatures == NULL ) {
123                 return -1;
124         }
125
126         for ( i = 0; !BER_BVISNULL( &builtin_supportedFeatures[ i ] ); i++ ) {
127                 ber_dupbv( &supportedFeatures[ i ], &builtin_supportedFeatures[ i ] );
128         }
129         BER_BVZERO( &supportedFeatures[ i ] );
130
131         return 0;
132 }
133
134 int
135 supported_feature_destroy( void )
136 {
137         int             i;
138
139         if ( supportedFeatures == NULL ) {
140                 return 0;
141         }
142         
143         for ( i = 0; !BER_BVISNULL( &supportedFeatures[ i ] ); i++ ) {
144                 ch_free( supportedFeatures[ i ].bv_val );
145         }
146
147         ch_free( supportedFeatures );
148         supportedFeatures = NULL;
149
150         return 0;
151 }
152
153 int
154 supported_feature_load( struct berval *f )
155 {
156         struct berval   *tmp;
157         int             i;
158
159         supported_feature_init();
160
161         for ( i = 0; !BER_BVISNULL( &supportedFeatures[ i ] ); i++ )
162                 ;
163
164         tmp = ch_realloc( supportedFeatures, sizeof( struct berval ) * ( i + 2 ) );
165         if ( tmp == NULL ) {
166                 return -1;
167         }
168         supportedFeatures = tmp;
169
170         ber_dupbv( &supportedFeatures[ i ], f );
171         BER_BVZERO( &supportedFeatures[ i + 1 ] );
172
173         return 0;
174 }
175
176 int
177 root_dse_info(
178         Connection *conn,
179         Entry **entry,
180         const char **text )
181 {
182         Entry           *e;
183         struct berval val;
184 #ifdef LDAP_SLAPI
185         struct berval *bv;
186 #endif
187         int             i, j;
188         char ** supportedSASLMechanisms;
189         BackendDB *be;
190
191         AttributeDescription *ad_structuralObjectClass
192                 = slap_schema.si_ad_structuralObjectClass;
193         AttributeDescription *ad_objectClass
194                 = slap_schema.si_ad_objectClass;
195         AttributeDescription *ad_namingContexts
196                 = slap_schema.si_ad_namingContexts;
197 #ifdef LDAP_SLAPI
198         AttributeDescription *ad_supportedExtension
199                 = slap_schema.si_ad_supportedExtension;
200 #endif
201         AttributeDescription *ad_supportedLDAPVersion
202                 = slap_schema.si_ad_supportedLDAPVersion;
203         AttributeDescription *ad_supportedSASLMechanisms
204                 = slap_schema.si_ad_supportedSASLMechanisms;
205         AttributeDescription *ad_supportedFeatures
206                 = slap_schema.si_ad_supportedFeatures;
207         AttributeDescription *ad_monitorContext
208                 = slap_schema.si_ad_monitorContext;
209         AttributeDescription *ad_configContext
210                 = slap_schema.si_ad_configContext;
211         AttributeDescription *ad_ref
212                 = slap_schema.si_ad_ref;
213
214         e = entry_alloc();
215         if( e == NULL ) {
216                 Debug( LDAP_DEBUG_ANY,
217                         "root_dse_info: entry_alloc failed", 0, 0, 0 );
218                 return LDAP_OTHER;
219         }
220
221         e->e_attrs = NULL;
222         e->e_name.bv_val = ch_strdup( LDAP_ROOT_DSE );
223         e->e_name.bv_len = sizeof( LDAP_ROOT_DSE )-1;
224         e->e_nname.bv_val = ch_strdup( LDAP_ROOT_DSE );
225         e->e_nname.bv_len = sizeof( LDAP_ROOT_DSE )-1;
226
227         /* the DN is an empty string so no pretty/normalization is needed */
228         assert( !e->e_name.bv_len );
229         assert( !e->e_nname.bv_len );
230
231         e->e_private = NULL;
232
233         /* FIXME: is this really needed? */
234         BER_BVSTR( &val, "top" );
235         if( attr_merge_one( e, ad_objectClass, &val, NULL ) ) {
236 fail:
237                 entry_free( e );
238                 return LDAP_OTHER;
239         }
240
241         BER_BVSTR( &val, "OpenLDAProotDSE" );
242         if( attr_merge_one( e, ad_objectClass, &val, NULL ) ) {
243                 goto fail;
244         }
245         if( attr_merge_one( e, ad_structuralObjectClass, &val, NULL ) ) {
246                 goto fail;
247         }
248
249         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
250                 if ( be->be_suffix == NULL
251                                 || be->be_nsuffix == NULL ) {
252                         /* no suffix! */
253                         continue;
254                 }
255                 if ( SLAP_MONITOR( be )) {
256                         if( attr_merge_one( e, ad_monitorContext,
257                                         &be->be_suffix[0],
258                                         &be->be_nsuffix[0] ) )
259                         {
260                                 goto fail;
261                         }
262                         continue;
263                 }
264                 if ( SLAP_CONFIG( be )) {
265                         if( attr_merge_one( e, ad_configContext,
266                                         &be->be_suffix[0],
267                                         & be->be_nsuffix[0] ) )
268                         {
269                                 goto fail;
270                         }
271                         continue;
272                 }
273                 if ( SLAP_GLUE_SUBORDINATE( be ) && !SLAP_GLUE_ADVERTISE( be ) ) {
274                         continue;
275                 }
276                 for ( j = 0; be->be_suffix[j].bv_val != NULL; j++ ) {
277                         if( attr_merge_one( e, ad_namingContexts,
278                                         &be->be_suffix[j], NULL ) )
279                         {
280                                 goto fail;
281                         }
282                 }
283         }
284
285         /* altServer unsupported */
286
287         /* supportedControl */
288         if ( controls_root_dse_info( e ) != 0 ) {
289                 goto fail;
290         }
291
292         /* supportedExtension */
293         if ( exop_root_dse_info( e ) != 0 ) {
294                 goto fail;
295         }
296
297 #ifdef LDAP_SLAPI
298         /* netscape supportedExtension */
299         for ( i = 0; (bv = slapi_int_get_supported_extop(i)) != NULL; i++ ) {
300                 if( attr_merge_one( e, ad_supportedExtension, bv, NULL ) ) {
301                         goto fail;
302                 }
303         }
304 #endif /* LDAP_SLAPI */
305
306         /* supportedFeatures */
307         if ( supportedFeatures == NULL ) {
308                 supported_feature_init();
309         }
310
311         if( attr_merge( e, ad_supportedFeatures, supportedFeatures, NULL ) ) {
312                 goto fail;
313         }
314
315         /* supportedLDAPVersion */
316                 /* don't publish version 2 as we don't really support it
317                  * (even when configured to accept version 2 Bind requests)
318                  * and the value would never be used by true LDAPv2 (or LDAPv3)
319                  * clients.
320                  */
321         for ( i=LDAP_VERSION3; i<=LDAP_VERSION_MAX; i++ ) {
322                 char buf[sizeof("255")];
323                 snprintf(buf, sizeof buf, "%d", i);
324                 val.bv_val = buf;
325                 val.bv_len = strlen( val.bv_val );
326                 if( attr_merge_one( e, ad_supportedLDAPVersion, &val, NULL ) ) {
327                         goto fail;
328                 }
329         }
330
331         /* supportedSASLMechanism */
332         supportedSASLMechanisms = slap_sasl_mechs( conn );
333
334         if( supportedSASLMechanisms != NULL ) {
335                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
336                         val.bv_val = supportedSASLMechanisms[i];
337                         val.bv_len = strlen( val.bv_val );
338                         if( attr_merge_one( e, ad_supportedSASLMechanisms, &val, NULL ) ) {
339                                 ldap_charray_free( supportedSASLMechanisms );
340                                 goto fail;
341                         }
342                 }
343                 ldap_charray_free( supportedSASLMechanisms );
344         }
345
346         if ( default_referral != NULL ) {
347                 if( attr_merge( e, ad_ref, default_referral, NULL /* FIXME */ ) ) {
348                         goto fail;
349                 }
350         }
351
352         if( usr_attr != NULL) {
353                 Attribute *a;
354                 for( a = usr_attr->e_attrs; a != NULL; a = a->a_next ) {
355                         if( attr_merge( e, a->a_desc, a->a_vals,
356                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
357                         {
358                                 goto fail;
359                         }
360                 }
361         }
362
363         if ( extra_info ) {
364                 entry_info_t    *ei = extra_info;
365
366                 for ( ; ei; ei = ei->next ) {
367                         ei->func( ei->arg, e );
368                 }
369         }
370
371         *entry = e;
372         return LDAP_SUCCESS;
373 }
374
375 int
376 root_dse_init( void )
377 {
378         return 0;
379 }
380
381 int
382 root_dse_destroy( void )
383 {
384         if ( usr_attr ) {
385                 entry_free( usr_attr );
386                 usr_attr = NULL;
387         }
388
389         return 0;
390 }
391
392 /*
393  * Read the entries specified in fname and merge the attributes
394  * to the user defined rootDSE. Note thaat if we find any errors
395  * what so ever, we will discard the entire entries, print an
396  * error message and return.
397  */
398 int
399 root_dse_read_file( const char *fname )
400 {
401         struct LDIFFP   *fp;
402         int rc = 0, lmax = 0, ldifrc;
403         unsigned long lineno = 0;
404         char    *buf = NULL;
405
406         if ( (fp = ldif_open( fname, "r" )) == NULL ) {
407                 Debug( LDAP_DEBUG_ANY,
408                         "root_dse_read_file: could not open rootdse attr file \"%s\" - absolute path?\n",
409                         fname, 0, 0 );
410                 perror( fname );
411                 return EXIT_FAILURE;
412         }
413
414         usr_attr = entry_alloc();
415         if( usr_attr == NULL ) {
416                 Debug( LDAP_DEBUG_ANY,
417                         "root_dse_read_file: entry_alloc failed", 0, 0, 0 );
418                 ldif_close( fp );
419                 return LDAP_OTHER;
420         }
421         usr_attr->e_attrs = NULL;
422
423         while(( ldifrc = ldif_read_record( fp, &lineno, &buf, &lmax )) > 0 ) {
424                 Entry *e = str2entry( buf );
425                 Attribute *a;
426
427                 if( e == NULL ) {
428                         Debug( LDAP_DEBUG_ANY, "root_dse_read_file: "
429                                 "could not parse entry (file=\"%s\" line=%lu)\n",
430                                 fname, lineno, 0 );
431                         rc = LDAP_OTHER;
432                         break;
433                 }
434
435                 /* make sure the DN is the empty DN */
436                 if( e->e_nname.bv_len ) {
437                         Debug( LDAP_DEBUG_ANY,
438                                 "root_dse_read_file: invalid rootDSE "
439                                 "- dn=\"%s\" (file=\"%s\" line=%lu)\n",
440                                 e->e_dn, fname, lineno );
441                         entry_free( e );
442                         rc = LDAP_OTHER;
443                         break;
444                 }
445
446                 /*
447                  * we found a valid entry, so walk thru all the attributes in the
448                  * entry, and add each attribute type and description to the
449                  * usr_attr entry
450                  */
451
452                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
453                         if( attr_merge( usr_attr, a->a_desc, a->a_vals,
454                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
455                         {
456                                 rc = LDAP_OTHER;
457                                 break;
458                         }
459                 }
460
461                 entry_free( e );
462                 if (rc) break;
463         }
464
465         if ( ldifrc < 0 )
466                 rc = LDAP_OTHER;
467
468         if (rc) {
469                 entry_free( usr_attr );
470                 usr_attr = NULL;
471         }
472
473         ch_free( buf );
474
475         ldif_close( fp );
476
477         Debug(LDAP_DEBUG_CONFIG, "rootDSE file=\"%s\" read.\n", fname, 0, 0);
478         return rc;
479 }
480
481 int
482 slap_discover_feature(
483         slap_bindconf   *sb,
484         const char      *attr,
485         const char      *val )
486 {
487         LDAP            *ld = NULL;
488         LDAPMessage     *res = NULL, *entry;
489         int             rc, i;
490         struct berval   bv_val,
491                         **values = NULL;
492         char            *attrs[ 2 ] = { NULL, NULL };
493
494         rc = slap_client_connect( &ld, sb );
495         if ( rc != LDAP_SUCCESS ) {
496                 goto done;
497         }
498
499         attrs[ 0 ] = (char *) attr;
500         rc = ldap_search_ext_s( ld, "", LDAP_SCOPE_BASE, "(objectClass=*)",
501                         attrs, 0, NULL, NULL, NULL, 0, &res );
502         if ( rc != LDAP_SUCCESS ) {
503                 goto done;
504         }
505
506         entry = ldap_first_entry( ld, res );
507         if ( entry == NULL ) {
508                 goto done;
509         }
510
511         values = ldap_get_values_len( ld, entry, attrs[ 0 ] );
512         if ( values == NULL ) {
513                 rc = LDAP_NO_SUCH_ATTRIBUTE;
514                 goto done;
515         }
516
517         ber_str2bv( val, 0, 0, &bv_val );
518         for ( i = 0; values[ i ] != NULL; i++ ) {
519                 if ( bvmatch( &bv_val, values[ i ] ) ) {
520                         rc = LDAP_COMPARE_TRUE;
521                         goto done;
522                 }
523         }
524
525         rc = LDAP_COMPARE_FALSE;
526
527 done:;
528         if ( values != NULL ) {
529                 ldap_value_free_len( values );
530         }
531
532         if ( res != NULL ) {
533                 ldap_msgfree( res );
534         }
535
536         ldap_unbind_ext( ld, NULL, NULL );
537
538         return rc;
539 }
540