1 /* database.c - deals with database subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
25 #include <ac/string.h>
26 #include <ac/unistd.h>
29 #include "back-monitor.h"
31 #if defined(LDAP_SLAPI)
33 static int monitor_back_add_plugin( Backend *be, Entry *e );
34 #endif /* defined(LDAP_SLAPI) */
36 #if defined(SLAPD_BDB)
37 #include "../back-bdb/back-bdb.h"
38 #endif /* defined(SLAPD_BDB) */
39 #if defined(SLAPD_HDB)
40 #include "../back-hdb/back-bdb.h"
41 #endif /* defined(SLAPD_HDB) */
42 #if defined(SLAPD_LDAP)
43 #include "../back-ldap/back-ldap.h"
44 #endif /* defined(SLAPD_LDAP) */
45 #if 0 && defined(SLAPD_LDBM)
46 #include "../back-ldbm/back-ldbm.h"
47 #endif /* defined(SLAPD_LDBM) */
49 static struct restricted_ops_t {
52 } restricted_ops[] = {
53 { BER_BVC( "add" ), SLAP_RESTRICT_OP_ADD },
54 { BER_BVC( "bind" ), SLAP_RESTRICT_OP_BIND },
55 { BER_BVC( "compare" ), SLAP_RESTRICT_OP_COMPARE },
56 { BER_BVC( "delete" ), SLAP_RESTRICT_OP_DELETE },
57 { BER_BVC( "extended" ), SLAP_RESTRICT_OP_EXTENDED },
58 { BER_BVC( "modify" ), SLAP_RESTRICT_OP_MODIFY },
59 { BER_BVC( "rename" ), SLAP_RESTRICT_OP_RENAME },
60 { BER_BVC( "search" ), SLAP_RESTRICT_OP_SEARCH },
62 }, restricted_exops[] = {
63 { BER_BVC( LDAP_EXOP_START_TLS ), SLAP_RESTRICT_EXOP_START_TLS },
64 { BER_BVC( LDAP_EXOP_MODIFY_PASSWD ), SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
65 { BER_BVC( LDAP_EXOP_X_WHO_AM_I ), SLAP_RESTRICT_EXOP_WHOAMI },
66 { BER_BVC( LDAP_EXOP_X_CANCEL ), SLAP_RESTRICT_EXOP_CANCEL },
71 init_readOnly( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
73 struct berval *tf = ( ( restrictops & SLAP_RESTRICT_OP_MASK ) == SLAP_RESTRICT_OP_WRITES ) ?
74 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
76 return attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
80 init_restrictedOperation( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
84 for ( i = 0; restricted_ops[ i ].op.bv_val; i++ ) {
85 if ( restrictops & restricted_ops[ i ].tag ) {
86 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
87 &restricted_ops[ i ].op, NULL );
94 for ( i = 0; restricted_exops[ i ].op.bv_val; i++ ) {
95 if ( restrictops & restricted_exops[ i ].tag ) {
96 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
97 &restricted_exops[ i ].op, NULL );
108 monitor_subsys_database_init(
114 Entry *e_database, **ep;
117 monitor_subsys_t *ms_backend,
120 assert( be != NULL );
122 mi = ( monitor_info_t * )be->be_private;
124 ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
125 if ( ms_backend == NULL ) {
126 Debug( LDAP_DEBUG_ANY,
127 "monitor_subsys_database_init: "
129 "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
135 ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
136 if ( ms_overlay == NULL ) {
137 Debug( LDAP_DEBUG_ANY,
138 "monitor_subsys_database_init: "
140 "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
146 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) {
147 Debug( LDAP_DEBUG_ANY,
148 "monitor_subsys_database_init: "
149 "unable to get entry \"%s\"\n",
150 ms->mss_ndn.bv_val, 0, 0 );
154 (void)init_readOnly( mi, e_database, frontendDB->be_restrictops );
155 (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops );
157 mp = ( monitor_entry_t * )e_database->e_private;
158 mp->mp_children = NULL;
159 ep = &mp->mp_children;
161 for ( i = 0; i < nBackendDB; i++ ) {
162 char buf[ BACKMONITOR_BUFSIZE ];
164 slap_overinfo *oi = NULL;
168 be = &backendDB[ i ];
172 if ( overlay_is_over( be ) ) {
173 oi = (slap_overinfo *)be->bd_info->bi_private;
177 /* Subordinates are not exposed as their own naming context */
178 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
182 snprintf( buf, sizeof( buf ),
183 "dn: cn=Database %d,%s\n"
185 "structuralObjectClass: %s\n"
187 "description: This object contains the type of the database.\n"
190 "modifiersName: %s\n"
191 "createTimestamp: %s\n"
192 "modifyTimestamp: %s\n",
195 mi->mi_oc_monitoredObject->soc_cname.bv_val,
196 mi->mi_oc_monitoredObject->soc_cname.bv_val,
198 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
200 mi->mi_creatorsName.bv_val,
201 mi->mi_creatorsName.bv_val,
202 mi->mi_startTime.bv_val,
203 mi->mi_startTime.bv_val );
205 e = str2entry( buf );
207 Debug( LDAP_DEBUG_ANY,
208 "monitor_subsys_database_init: "
209 "unable to create entry \"cn=Database %d,%s\"\n",
210 i, ms->mss_dn.bv_val, 0 );
214 if ( SLAP_MONITOR(be) ) {
215 attr_merge( e, slap_schema.si_ad_monitorContext,
216 be->be_suffix, be->be_nsuffix );
217 attr_merge( e_database, slap_schema.si_ad_monitorContext,
218 be->be_suffix, be->be_nsuffix );
221 attr_merge( e, slap_schema.si_ad_namingContexts,
222 be->be_suffix, be->be_nsuffix );
223 attr_merge( e_database, slap_schema.si_ad_namingContexts,
224 be->be_suffix, be->be_nsuffix );
227 (void)init_readOnly( mi, e, be->be_restrictops );
228 (void)init_restrictedOperation( mi, e, be->be_restrictops );
231 slap_overinst *on = oi->oi_list,
234 for ( ; on; on = on->on_next ) {
238 for ( on2 = on1; on2 != on; on2 = on2->on_next ) {
239 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
248 bv.bv_val = on->on_bi.bi_type;
249 bv.bv_len = strlen( bv.bv_val );
250 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
253 /* find the overlay number, j */
254 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
255 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
261 snprintf( buf, sizeof( buf ),
263 j, ms_overlay->mss_dn.bv_val );
265 bv.bv_len = strlen( buf );
266 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
271 #if defined(SLAPD_BDB) || defined(SLAPD_HDB)
272 if ( strcmp( bi->bi_type, "bdb" ) == 0
273 || strcmp( bi->bi_type, "hdb" ) == 0 )
276 ber_len_t pathlen = 0, len = 0;
277 /* FIXME: need a cleaner solution */
279 #define PATH_MAX 4095
280 #endif /* ! PATH_MAX */
281 char path[ PATH_MAX ] = { '\0' };
284 if ( strcmp( bi->bi_type, "bdb" ) == 0
285 || strcmp( bi->bi_type, "hdb" ) == 0 )
287 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
289 fname = bdb->bi_dbenv_home;
291 } else if ( strcmp( bi->bi_type, "ldbm" ) == 0 ) {
292 struct ldbminfo *ldbm = (struct ldbminfo *) be->be_private;
294 /* FIXME: there's a conflict
295 * between back-bdb.h and back.ldbm.h;
296 * anyway, this code will be moved
297 * to the backends as soon as the
298 * issue with filtering on namingContexts
300 fname = ldbm->li_directory;
304 len = strlen( fname );
305 if ( fname[ 0 ] != '/' ) {
306 /* get full path name */
307 getcwd( path, sizeof( path ) );
308 pathlen = strlen( path );
310 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
316 bv.bv_len = STRLENOF( "file://" ) + pathlen
317 + STRLENOF( "/" ) + len;
318 bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
319 AC_MEMCPY( bv.bv_val, "file://", STRLENOF( "file://" ) );
321 AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) ],
323 bv.bv_val[ STRLENOF( "file://" ) + pathlen ] = '/';
326 AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) + pathlen ],
328 if ( bv.bv_val[ bv.bv_len - 1 ] != '/' ) {
329 bv.bv_val[ bv.bv_len ] = '/';
332 bv.bv_val[ bv.bv_len ] = '\0';
334 attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
337 ch_free( bv.bv_val );
339 #endif /* defined(SLAPD_LDAP) || defined(SLAPD_HDB) */
341 #if defined(SLAPD_LDAP)
342 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
343 struct ldapinfo *li = (struct ldapinfo *)be->be_private;
347 bv.bv_len = strlen( bv.bv_val );
348 attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
351 #endif /* defined(SLAPD_LDAP) */
353 for ( j = 0; j < nBackendInfo; j++ ) {
354 if ( backendInfo[ j ].bi_type == bi->bi_type ) {
357 snprintf( buf, sizeof( buf ),
359 j, ms_backend->mss_dn.bv_val );
361 bv.bv_len = strlen( buf );
362 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
367 /* we must find it! */
370 mp = monitor_entrypriv_create();
374 e->e_private = ( void * )mp;
376 mp->mp_flags = ms->mss_flags
379 if ( monitor_cache_add( mi, e ) ) {
380 Debug( LDAP_DEBUG_ANY,
381 "monitor_subsys_database_init: "
382 "unable to add entry \"cn=Database %d,%s\"\n",
383 i, ms->mss_dn.bv_val, 0 );
387 #if defined(LDAP_SLAPI)
388 monitor_back_add_plugin( be, e );
389 #endif /* defined(LDAP_SLAPI) */
392 Entry **ep_overlay = &mp->mp_children;
393 monitor_entry_t *mp_overlay;
394 slap_overinst *on = oi->oi_list;
397 for ( o = 0; on; o++, on = on->on_next ) {
401 /* find the overlay number, j */
402 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
403 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
409 snprintf( buf, sizeof( buf ),
410 "dn: cn=Overlay %d,cn=Database %d,%s\n"
412 "structuralObjectClass: %s\n"
414 "description: This object contains the type of the overlay.\n"
416 "seeAlso: cn=Overlay %d,%s\n"
418 "modifiersName: %s\n"
419 "createTimestamp: %s\n"
420 "modifyTimestamp: %s\n",
424 mi->mi_oc_monitoredObject->soc_cname.bv_val,
425 mi->mi_oc_monitoredObject->soc_cname.bv_val,
427 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
430 ms_overlay->mss_dn.bv_val,
431 mi->mi_creatorsName.bv_val,
432 mi->mi_creatorsName.bv_val,
433 mi->mi_startTime.bv_val,
434 mi->mi_startTime.bv_val );
436 e_overlay = str2entry( buf );
437 if ( e_overlay == NULL ) {
438 Debug( LDAP_DEBUG_ANY,
439 "monitor_subsys_database_init: "
440 "unable to create entry "
441 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
442 o, i, ms->mss_dn.bv_val );
446 mp_overlay = monitor_entrypriv_create();
447 if ( mp_overlay == NULL ) {
450 e_overlay->e_private = ( void * )mp_overlay;
451 mp_overlay->mp_info = ms;
452 mp_overlay->mp_flags = ms->mss_flags
455 if ( monitor_cache_add( mi, e_overlay ) ) {
456 Debug( LDAP_DEBUG_ANY,
457 "monitor_subsys_database_init: "
458 "unable to add entry "
459 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
460 o, i, ms->mss_dn.bv_val );
464 *ep_overlay = e_overlay;
465 ep_overlay = &mp_overlay->mp_next;
473 monitor_cache_release( mi, e_database );
480 * cur: must not contain the tags corresponding to the values in v
481 * delta: will contain the tags corresponding to the values in v
484 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
486 for ( ; !BER_BVISNULL( v ); v++ ) {
487 struct restricted_ops_t *rops;
490 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
491 rops = restricted_exops;
494 rops = restricted_ops;
497 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
498 if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
502 if ( rops[ i ].tag & *delta ) {
506 if ( rops[ i ].tag & cur ) {
510 cur |= rops[ i ].tag;
511 *delta |= rops[ i ].tag;
516 if ( BER_BVISNULL( &rops[ i ].op ) ) {
517 return LDAP_INVALID_SYNTAX;
525 monitor_subsys_database_modify(
530 monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
532 Attribute *save_attrs, *a;
533 Modifications *modlist = op->oq_modify.rs_modlist;
536 int ro_gotval = 1, i, n;
537 slap_mask_t rp_add = 0, rp_delete = 0, rp_cur;
540 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
542 return LDAP_UNWILLING_TO_PERFORM;
544 if ( n < 0 || n >= nBackendDB )
545 return LDAP_NO_SUCH_OBJECT;
547 /* do not allow some changes on back-monitor (needs work)... */
548 be = &backendDB[ n ];
549 if ( SLAP_MONITOR( be ) )
550 return LDAP_UNWILLING_TO_PERFORM;
552 rp_cur = be->be_restrictops;
554 save_attrs = e->e_attrs;
555 e->e_attrs = attrs_dup( e->e_attrs );
557 for ( ml=modlist; ml; ml=ml->sml_next ) {
558 Modification *mod = &ml->sml_mod;
560 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
563 if ( mod->sm_values ) {
564 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
565 rc = LDAP_CONSTRAINT_VIOLATION;
569 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
572 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
576 rc = LDAP_INVALID_SYNTAX;
581 switch ( mod->sm_op ) {
582 case LDAP_MOD_DELETE:
583 if ( ro_gotval < 1 ) {
584 rc = LDAP_CONSTRAINT_VIOLATION;
589 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
590 rc = LDAP_NO_SUCH_ATTRIBUTE;
594 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
595 rc = LDAP_NO_SUCH_ATTRIBUTE;
601 case LDAP_MOD_REPLACE:
606 if ( ro_gotval > 0 ) {
607 rc = LDAP_CONSTRAINT_VIOLATION;
613 rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
614 rp_cur |= SLAP_RESTRICT_OP_WRITES;
615 rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
617 } else if ( val == 0 ) {
618 rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
619 rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
620 rp_add &= ~SLAP_RESTRICT_OP_WRITES;
629 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
630 slap_mask_t mask = 0;
632 switch ( mod->sm_op ) {
633 case LDAP_MOD_DELETE:
634 if ( mod->sm_values == NULL ) {
640 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
641 if ( rc == LDAP_SUCCESS ) {
646 } else if ( rc == LDAP_OTHER ) {
647 rc = LDAP_NO_SUCH_ATTRIBUTE;
651 case LDAP_MOD_REPLACE:
658 rc = value_mask( mod->sm_values, rp_cur, &mask );
659 if ( rc == LDAP_SUCCESS ) {
664 } else if ( rc == LDAP_OTHER ) {
665 rc = LDAP_TYPE_OR_VALUE_EXISTS;
674 if ( rc != LDAP_SUCCESS ) {
678 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
679 /* accept all operational attributes */
680 attr_delete( &e->e_attrs, mod->sm_desc );
681 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
689 rc = LDAP_UNWILLING_TO_PERFORM;
695 if ( ro_gotval < 1 ) {
696 rc = LDAP_CONSTRAINT_VIOLATION;
700 if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
701 rc = LDAP_CONSTRAINT_VIOLATION;
705 if ( rp_delete & rp_add ) {
710 /* check current value of readOnly */
711 if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
712 tf = (struct berval *)&slap_true_bv;
715 tf = (struct berval *)&slap_false_bv;
718 a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
724 if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
725 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
726 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
729 if ( rc == LDAP_SUCCESS ) {
731 if ( rp_delete == be->be_restrictops ) {
732 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
735 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
741 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
742 if ( rp_delete & restricted_ops[ i ].tag ) {
745 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
748 if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
752 ch_free( a->a_vals[ j ].bv_val );
753 ch_free( a->a_nvals[ j ].bv_val );
755 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
756 a->a_vals[ k - 1 ] = a->a_vals[ k ];
757 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
760 BER_BVZERO( &a->a_vals[ k - 1 ] );
761 BER_BVZERO( &a->a_nvals[ k - 1 ] );
766 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
767 if ( rp_delete & restricted_exops[ i ].tag ) {
770 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
773 if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
777 ch_free( a->a_vals[ j ].bv_val );
778 ch_free( a->a_nvals[ j ].bv_val );
780 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
781 a->a_vals[ k - 1 ] = a->a_vals[ k ];
782 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
785 BER_BVZERO( &a->a_vals[ k - 1 ] );
786 BER_BVZERO( &a->a_nvals[ k - 1 ] );
794 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
795 if ( rp_add & restricted_ops[ i ].tag ) {
796 attr_merge_one( e, mi->mi_ad_restrictedOperation,
797 &restricted_ops[ i ].op, NULL );
801 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
802 if ( rp_add & restricted_exops[ i ].tag ) {
803 attr_merge_one( e, mi->mi_ad_restrictedOperation,
804 &restricted_exops[ i ].op, NULL );
810 be->be_restrictops = rp_cur;
813 if ( rc == LDAP_SUCCESS ) {
814 attrs_free( save_attrs );
817 Attribute *tmp = e->e_attrs;
818 e->e_attrs = save_attrs;
824 #if defined(LDAP_SLAPI)
826 monitor_back_add_plugin( Backend *be, Entry *e_database )
828 Slapi_PBlock *pCurrentPB;
829 int i, rc = LDAP_SUCCESS;
830 monitor_info_t *mi = ( monitor_info_t * )be->be_private;
832 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
834 * LDAP_OTHER is returned if no plugins are installed
842 Slapi_PluginDesc *srchdesc;
843 char buf[ BACKMONITOR_BUFSIZE ];
846 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
848 if ( rc != LDAP_SUCCESS ) {
852 snprintf( buf, sizeof(buf),
853 "plugin %d name: %s; "
859 srchdesc->spd_vendor,
860 srchdesc->spd_version,
861 srchdesc->spd_description );
864 bv.bv_len = strlen( buf );
865 attr_merge_normalize_one( e_database,
866 mi->mi_ad_monitoredInfo, &bv, NULL );
870 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
871 && ( pCurrentPB != NULL ) );
876 #endif /* defined(LDAP_SLAPI) */