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