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