1 /* backend.c - routines for dealing with back-end databases */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/string.h>
33 #include <ac/socket.h>
41 * If a module is configured as dynamic, its header should not
42 * get included into slapd. While this is a general rule and does
43 * not have much of an effect in UNIX, this rule should be adhered
44 * to for Windows, where dynamic object code should not be implicitly
45 * imported into slapd without appropriate __declspec(dllimport) directives.
49 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
52 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
55 backend_init_controls( BackendInfo *bi )
57 if ( bi->bi_controls ) {
60 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
63 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
64 == LDAP_CONTROL_NOT_FOUND )
66 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
73 bi->bi_ctrls[ cid ] = 1;
80 int backend_init(void)
85 if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
86 /* already initialized */
87 Debug( LDAP_DEBUG_ANY,
88 "backend_init: already initialized\n", 0, 0, 0 );
92 for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ ) {
93 assert( bi->bi_init != 0 );
95 rc = bi->bi_init( bi );
98 Debug( LDAP_DEBUG_ANY,
99 "backend_init: initialized for type \"%s\"\n",
101 /* destroy those we've already inited */
106 if ( slap_binfo[nBackendInfo].bi_destroy ) {
107 slap_binfo[nBackendInfo].bi_destroy(
108 &slap_binfo[nBackendInfo] );
114 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
117 if ( nBackendInfo > 0) {
125 Debug( LDAP_DEBUG_ANY,
126 "backend_init: failed\n",
130 #endif /* SLAPD_MODULES */
133 int backend_add(BackendInfo *aBackendInfo)
137 if ( aBackendInfo->bi_init == NULL ) {
138 Debug( LDAP_DEBUG_ANY, "backend_add: "
139 "backend type \"%s\" does not have the (mandatory)init function\n",
140 aBackendInfo->bi_type, 0, 0 );
144 rc = aBackendInfo->bi_init(aBackendInfo);
146 Debug( LDAP_DEBUG_ANY,
147 "backend_add: initialization for type \"%s\" failed\n",
148 aBackendInfo->bi_type, 0, 0 );
152 (void)backend_init_controls( aBackendInfo );
154 /* now add the backend type to the Backend Info List */
155 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
161 backend_set_controls( BackendDB *be )
163 BackendInfo *bi = be->bd_info;
165 /* back-relay takes care of itself; so may do other */
166 if ( overlay_is_over( be ) ) {
167 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
170 if ( bi->bi_controls ) {
171 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
172 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
173 sizeof( be->be_ctrls ) );
174 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
179 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
180 if ( bi->bi_ctrls[ i ] ) {
181 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
191 /* startup a specific backend database */
192 int backend_startup_one(Backend *be)
196 assert( be != NULL );
198 be->be_pending_csn_list = (struct be_pcl *)
199 ch_calloc( 1, sizeof( struct be_pcl ) );
201 LDAP_TAILQ_INIT( be->be_pending_csn_list );
203 Debug( LDAP_DEBUG_TRACE,
204 "backend_startup_one: starting \"%s\"\n",
205 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
208 /* set database controls */
209 (void)backend_set_controls( be );
211 if ( be->bd_info->bi_db_open ) {
212 rc = be->bd_info->bi_db_open( be );
214 (void)backend_set_controls( be );
217 Debug( LDAP_DEBUG_ANY,
218 "backend_startup_one: bi_db_open failed! (%d)\n",
226 int backend_startup(Backend *be)
232 if( ! ( nBackendDB > 0 ) ) {
234 Debug( LDAP_DEBUG_ANY,
235 "backend_startup: %d databases to startup.\n",
241 if ( be->bd_info->bi_open ) {
242 rc = be->bd_info->bi_open( be->bd_info );
244 Debug( LDAP_DEBUG_ANY,
245 "backend_startup: bi_open failed!\n",
252 return backend_startup_one( be );
255 /* open frontend, if required */
256 if ( frontendDB->bd_info->bi_db_open ) {
257 rc = frontendDB->bd_info->bi_db_open( frontendDB );
259 Debug( LDAP_DEBUG_ANY,
260 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
266 /* open each backend type */
268 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
270 if( bi->bi_nDB == 0) {
271 /* no database of this type, don't open */
276 rc = bi->bi_open( bi );
278 Debug( LDAP_DEBUG_ANY,
279 "backend_startup: bi_open %d (%s) failed!\n",
285 (void)backend_init_controls( bi );
288 /* open each backend database */
290 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
292 if ( be->be_suffix == NULL ) {
293 Debug( LDAP_DEBUG_ANY,
294 "backend_startup: warning, database %d (%s) "
296 i, be->bd_info->bi_type, 0 );
298 /* append global access controls */
299 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
301 rc = backend_startup_one( be );
309 int backend_num( Backend *be )
314 if( be == NULL ) return -1;
316 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
317 if( be == b2 ) return i;
323 int backend_shutdown( Backend *be )
329 /* shutdown a specific backend database */
331 if ( be->bd_info->bi_nDB == 0 ) {
332 /* no database of this type, we never opened it */
336 if ( be->bd_info->bi_db_close ) {
337 be->bd_info->bi_db_close( be );
340 if( be->bd_info->bi_close ) {
341 be->bd_info->bi_close( be->bd_info );
347 /* close each backend database */
348 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
349 if ( be->bd_info->bi_db_close ) {
350 be->bd_info->bi_db_close( be );
354 Debug( LDAP_DEBUG_ANY,
355 "backend_close: bi_db_close %s failed!\n",
360 /* close each backend type */
361 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
362 if( bi->bi_nDB == 0 ) {
363 /* no database of this type */
372 /* close frontend, if required */
373 if ( frontendDB->bd_info->bi_db_close ) {
374 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
376 Debug( LDAP_DEBUG_ANY,
377 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
385 void backend_destroy_one( BackendDB *bd, int dynamic )
388 LDAP_STAILQ_REMOVE(&backendDB, bd, slap_backend_db, be_next );
391 if ( bd->be_syncinfo ) {
392 syncinfo_free( bd->be_syncinfo );
395 if ( bd->be_pending_csn_list ) {
396 struct slap_csn_entry *csne;
397 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
399 struct slap_csn_entry *tmp_csne = csne;
401 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
402 ch_free( csne->ce_csn.bv_val );
403 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
406 ch_free( bd->be_pending_csn_list );
409 if ( bd->bd_info->bi_db_destroy ) {
410 bd->bd_info->bi_db_destroy( bd );
412 ber_bvarray_free( bd->be_suffix );
413 ber_bvarray_free( bd->be_nsuffix );
414 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
415 free( bd->be_rootdn.bv_val );
417 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
418 free( bd->be_rootndn.bv_val );
420 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
421 free( bd->be_rootpw.bv_val );
423 acl_destroy( bd->be_acl, frontendDB->be_acl );
424 limits_destroy( bd->be_limits );
425 if ( bd->be_replogfile ) {
426 ch_free( bd->be_replogfile );
428 if ( bd->be_replica_argsfile ) {
429 ch_free( bd->be_replica_argsfile );
431 if ( bd->be_replica_pidfile ) {
432 ch_free( bd->be_replica_pidfile );
434 destroy_replica_info( bd );
435 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
436 ch_free( bd->be_update_ndn.bv_val );
438 if ( bd->be_update_refs ) {
439 ber_bvarray_free( bd->be_update_refs );
447 int backend_destroy(void)
452 /* destroy each backend database */
453 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
454 backend_destroy_one( bd, 1 );
457 /* destroy each backend type */
458 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
459 if( bi->bi_destroy ) {
460 bi->bi_destroy( bi );
465 LDAP_STAILQ_INIT(&backendInfo);
467 /* destroy frontend database */
470 if ( bd->bd_info->bi_db_destroy ) {
471 bd->bd_info->bi_db_destroy( bd );
473 ber_bvarray_free( bd->be_suffix );
474 ber_bvarray_free( bd->be_nsuffix );
475 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
476 free( bd->be_rootdn.bv_val );
478 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
479 free( bd->be_rootndn.bv_val );
481 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
482 free( bd->be_rootpw.bv_val );
484 acl_destroy( bd->be_acl, frontendDB->be_acl );
486 if ( bd->be_replogfile != NULL ) {
487 free( bd->be_replogfile );
489 if ( bd->be_replica_argsfile ) {
490 ch_free( bd->be_replica_argsfile );
492 if ( bd->be_replica_pidfile ) {
493 ch_free( bd->be_replica_pidfile );
495 assert( bd->be_replica == NULL );
501 BackendInfo* backend_info(const char *type)
505 /* search for the backend type */
506 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
507 if( strcasecmp(bi->bi_type, type) == 0 ) {
521 BackendInfo *bi = backend_info(type);
525 fprintf( stderr, "Unrecognized database type (%s)\n", type );
529 /* If be is provided, treat it as private. Otherwise allocate
530 * one and add it to the global list.
533 be = ch_calloc( 1, sizeof(Backend) );
535 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
540 be->be_def_limit = frontendDB->be_def_limit;
541 be->be_dfltaccess = frontendDB->be_dfltaccess;
543 be->be_restrictops = frontendDB->be_restrictops;
544 be->be_requires = frontendDB->be_requires;
545 be->be_ssf_set = frontendDB->be_ssf_set;
547 be->be_pcl_mutexp = &be->be_pcl_mutex;
548 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
550 /* assign a default depth limit for alias deref */
551 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
553 if ( bi->bi_db_init ) {
554 rc = bi->bi_db_init( be );
558 fprintf( stderr, "database init failed (%s)\n", type );
572 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
573 if ( be->bd_info->bi_db_close ) {
574 be->bd_info->bi_db_close( be );
578 if ( frontendDB->bd_info->bi_db_close ) {
579 (*frontendDB->bd_info->bi_db_close)( frontendDB );
591 ber_len_t len, dnlen = dn->bv_len;
592 Backend *be, *b2 = NULL;
594 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
595 if ( be->be_nsuffix == NULL ) {
599 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
601 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
606 len = be->be_nsuffix[j].bv_len;
609 /* suffix is longer than DN */
614 * input DN is normalized, so the separator check
615 * need not look at escaping
617 if ( len && len < dnlen &&
618 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
623 if ( strcmp( be->be_nsuffix[j].bv_val,
624 &dn->bv_val[dnlen-len] ) == 0 )
629 if( manageDSAit && len == dnlen &&
630 !SLAP_GLUE_SUBORDINATE( be ) ) {
634 /* If any parts of the tree are glued, use the first
635 * match regardless of manageDSAit. Otherwise use the
638 if( !( SLAP_DBFLAGS( be ) & ( SLAP_DBFLAG_GLUE_INSTANCE |
639 SLAP_DBFLAG_GLUE_SUBORDINATE )))
653 struct berval *bvsuffix )
657 if ( be->be_nsuffix == NULL ) {
661 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
662 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
671 be_isroot_dn( Backend *be, struct berval *ndn )
673 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
677 return dn_match( &be->be_rootndn, ndn );
681 be_slurp_update( Operation *op )
683 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
684 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
688 be_shadow_update( Operation *op )
690 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
691 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
695 be_isupdate_dn( Backend *be, struct berval *ndn )
697 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
701 return dn_match( &be->be_update_ndn, ndn );
705 be_root_dn( Backend *be )
707 return &be->be_rootdn;
711 be_isroot( Operation *op )
713 return be_isroot_dn( op->o_bd, &op->o_ndn );
717 be_isroot_pw( Operation *op )
721 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
725 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
730 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
731 op->o_conn->c_sasl_authctx, NULL );
734 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
737 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
749 if ( op->o_bd->be_release ) {
750 /* free and release entry from backend */
751 return op->o_bd->be_release( op, e, rw );
760 backend_unbind( Operation *op, SlapReply *rs )
764 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
765 if ( be->be_unbind ) {
767 be->be_unbind( op, rs );
775 backend_connection_init(
780 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
781 if ( be->be_connection_init ) {
782 be->be_connection_init( be, conn );
790 backend_connection_destroy(
795 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
796 if ( be->be_connection_destroy ) {
797 be->be_connection_destroy( be, conn);
805 backend_check_controls(
809 LDAPControl **ctrls = op->o_ctrls;
810 rs->sr_err = LDAP_SUCCESS;
813 for( ; *ctrls != NULL ; ctrls++ ) {
816 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
817 case LDAP_CONTROL_NOT_FOUND:
818 /* unrecognized control */
819 if ( (*ctrls)->ldctl_iscritical ) {
820 /* should not be reachable */
821 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
822 "unrecognized critical control: %s\n",
823 (*ctrls)->ldctl_oid, 0, 0 );
826 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
827 "unrecognized non-critical control: %s\n",
828 (*ctrls)->ldctl_oid, 0, 0 );
832 case LDAP_COMPARE_FALSE:
833 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
834 /* Per RFC 2251 (and LDAPBIS discussions), if the control
835 * is recognized and appropriate for the operation (which
836 * we've already verified), then the server should make
837 * use of the control when performing the operation.
839 * Here we find that operation extended by the control
840 * is unavailable in a particular context, and the control
841 * is marked Critical, hence the return of
842 * unwillingToPerform.
844 rs->sr_text = "critical control unavailable in context";
845 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
850 case LDAP_COMPARE_TRUE:
855 Debug( LDAP_DEBUG_ANY,
856 "backend_check_controls: unable to check control: %s\n",
857 (*ctrls)->ldctl_oid, 0, 0 );
860 rs->sr_text = "unable to check control";
861 rs->sr_err = LDAP_OTHER;
867 /* temporarily removed */
869 /* check should be generalized */
870 if( get_manageDIT(op) && !be_isroot(op)) {
871 rs->sr_text = "requires manager authorization";
872 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
881 backend_check_restrictions(
884 struct berval *opdata )
886 slap_mask_t restrictops;
887 slap_mask_t requires;
889 slap_mask_t exopflag = 0;
896 int rc = SLAP_CB_CONTINUE;
898 if ( op->o_bd->be_chk_controls ) {
899 rc = ( *op->o_bd->be_chk_controls )( op, rs );
902 if ( rc == SLAP_CB_CONTINUE ) {
903 rc = backend_check_controls( op, rs );
906 if ( rc != LDAP_SUCCESS ) {
910 restrictops = op->o_bd->be_restrictops;
911 requires = op->o_bd->be_requires;
912 ssf = &op->o_bd->be_ssf_set;
915 restrictops = frontendDB->be_restrictops;
916 requires = frontendDB->be_requires;
917 ssf = &frontendDB->be_ssf_set;
920 switch( op->o_tag ) {
922 opflag = SLAP_RESTRICT_OP_ADD;
926 opflag = SLAP_RESTRICT_OP_BIND;
929 case LDAP_REQ_COMPARE:
930 opflag = SLAP_RESTRICT_OP_COMPARE;
932 case LDAP_REQ_DELETE:
934 opflag = SLAP_RESTRICT_OP_DELETE;
936 case LDAP_REQ_EXTENDED:
937 opflag = SLAP_RESTRICT_OP_EXTENDED;
940 /* treat unspecified as a modify */
941 opflag = SLAP_RESTRICT_OP_MODIFY;
946 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
949 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
953 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
954 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
958 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
959 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
963 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
964 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
969 /* treat everything else as a modify */
970 opflag = SLAP_RESTRICT_OP_MODIFY;
974 case LDAP_REQ_MODIFY:
976 opflag = SLAP_RESTRICT_OP_MODIFY;
978 case LDAP_REQ_RENAME:
980 opflag = SLAP_RESTRICT_OP_RENAME;
982 case LDAP_REQ_SEARCH:
983 opflag = SLAP_RESTRICT_OP_SEARCH;
985 case LDAP_REQ_UNBIND:
990 rs->sr_text = "restrict operations internal error";
991 rs->sr_err = LDAP_OTHER;
996 /* these checks don't apply to StartTLS */
998 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
999 if( op->o_transport_ssf < ssf->sss_transport ) {
1000 rs->sr_text = op->o_transport_ssf
1001 ? "stronger transport confidentiality required"
1002 : "transport confidentiality required";
1006 if( op->o_tls_ssf < ssf->sss_tls ) {
1007 rs->sr_text = op->o_tls_ssf
1008 ? "stronger TLS confidentiality required"
1009 : "TLS confidentiality required";
1014 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1015 /* simple bind specific check */
1016 if( op->o_ssf < ssf->sss_simple_bind ) {
1017 rs->sr_text = op->o_ssf
1018 ? "stronger confidentiality required"
1019 : "confidentiality required";
1024 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1025 /* these checks don't apply to SASL bind */
1027 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1028 rs->sr_text = op->o_sasl_ssf
1029 ? "stronger SASL confidentiality required"
1030 : "SASL confidentiality required";
1034 if( op->o_ssf < ssf->sss_ssf ) {
1035 rs->sr_text = op->o_ssf
1036 ? "stronger confidentiality required"
1037 : "confidentiality required";
1043 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1044 rs->sr_text = op->o_transport_ssf
1045 ? "stronger transport confidentiality required for update"
1046 : "transport confidentiality required for update";
1050 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1051 rs->sr_text = op->o_tls_ssf
1052 ? "stronger TLS confidentiality required for update"
1053 : "TLS confidentiality required for update";
1057 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1058 rs->sr_text = op->o_sasl_ssf
1059 ? "stronger SASL confidentiality required for update"
1060 : "SASL confidentiality required for update";
1064 if( op->o_ssf < ssf->sss_update_ssf ) {
1065 rs->sr_text = op->o_ssf
1066 ? "stronger confidentiality required for update"
1067 : "confidentiality required for update";
1071 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1072 BER_BVISEMPTY( &op->o_ndn ) )
1074 rs->sr_text = "modifications require authentication";
1075 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1079 #ifdef SLAP_X_LISTENER_MOD
1080 if ( op->o_conn->c_listener &&
1081 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1082 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1084 /* no "w" mode means readonly */
1085 rs->sr_text = "modifications not allowed on this listener";
1086 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1089 #endif /* SLAP_X_LISTENER_MOD */
1094 /* these checks don't apply to Bind, StartTLS, or Unbind */
1096 if( requires & SLAP_REQUIRE_STRONG ) {
1097 /* should check mechanism */
1098 if( ( op->o_transport_ssf < ssf->sss_transport
1099 && op->o_authtype == LDAP_AUTH_SIMPLE )
1100 || BER_BVISEMPTY( &op->o_dn ) )
1102 rs->sr_text = "strong(er) authentication required";
1103 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1108 if( requires & SLAP_REQUIRE_SASL ) {
1109 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1110 rs->sr_text = "SASL authentication required";
1111 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1116 if( requires & SLAP_REQUIRE_AUTHC ) {
1117 if( BER_BVISEMPTY( &op->o_dn ) ) {
1118 rs->sr_text = "authentication required";
1119 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1124 if( requires & SLAP_REQUIRE_BIND ) {
1126 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1127 version = op->o_conn->c_protocol;
1128 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1131 /* no bind has occurred */
1132 rs->sr_text = "BIND required";
1133 rs->sr_err = LDAP_OPERATIONS_ERROR;
1138 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1139 if( op->o_protocol < LDAP_VERSION3 ) {
1140 /* no bind has occurred */
1141 rs->sr_text = "operation restricted to LDAPv3 clients";
1142 rs->sr_err = LDAP_OPERATIONS_ERROR;
1147 #ifdef SLAP_X_LISTENER_MOD
1148 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1149 if ( op->o_conn->c_listener &&
1150 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1152 /* no "x" mode means bind required */
1153 rs->sr_text = "bind required on this listener";
1154 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1159 if ( !starttls && !updateop ) {
1160 if ( op->o_conn->c_listener &&
1161 !( op->o_conn->c_listener->sl_perms &
1162 ( !BER_BVISEMPTY( &op->o_dn )
1163 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1165 /* no "r" mode means no read */
1166 rs->sr_text = "read not allowed on this listener";
1167 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1171 #endif /* SLAP_X_LISTENER_MOD */
1175 if( ( restrictops & opflag )
1176 || ( exopflag && ( restrictops & exopflag ) ) ) {
1177 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1178 rs->sr_text = "read operations restricted";
1179 } else if ( restrictops & exopflag ) {
1180 rs->sr_text = "extended operation restricted";
1182 rs->sr_text = "operation restricted";
1184 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1188 rs->sr_err = LDAP_SUCCESS;
1192 int backend_check_referrals( Operation *op, SlapReply *rs )
1194 rs->sr_err = LDAP_SUCCESS;
1196 if( op->o_bd->be_chk_referrals ) {
1197 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1199 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1200 send_ldap_result( op, rs );
1212 AttributeDescription *at,
1218 if ( op->o_bd == NULL ) {
1219 return LDAP_NO_SUCH_OBJECT;
1222 if ( op->o_bd->be_fetch ) {
1223 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1226 return LDAP_UNWILLING_TO_PERFORM;
1233 struct berval *gr_ndn,
1234 struct berval *op_ndn,
1235 ObjectClass *group_oc,
1236 AttributeDescription *group_at )
1239 void *o_priv = op->o_private, *e_priv = NULL;
1243 Backend *be = op->o_bd;
1245 op->o_bd = select_backend( gr_ndn, 0, 0 );
1247 for ( g = op->o_groups; g; g = g->ga_next ) {
1248 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1249 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1253 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1263 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1267 op->o_private = NULL;
1268 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1269 e_priv = op->o_private;
1270 op->o_private = o_priv;
1273 a = attr_find( e->e_attrs, group_at );
1275 /* If the attribute is a subtype of labeledURI, treat this as
1276 * a dynamic group ala groupOfURLs
1278 if (is_at_subtype( group_at->ad_type,
1279 slap_schema.si_ad_labeledURI->ad_type ) )
1283 struct berval bv, nbase;
1286 void *user_priv = NULL;
1287 Backend *b2 = op->o_bd;
1289 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1292 op->o_bd = select_backend( op_ndn, 0, 0 );
1293 op->o_private = NULL;
1294 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1295 user_priv = op->o_private;
1296 op->o_private = o_priv;
1300 rc = LDAP_COMPARE_FALSE;
1301 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1302 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1307 BER_BVZERO( &nbase );
1308 /* host part must be empty */
1309 /* attrs and extensions parts must be empty */
1310 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1311 ludp->lud_attrs || ludp->lud_exts )
1315 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1316 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1317 op->o_tmpmemctx ) != LDAP_SUCCESS )
1321 switch ( ludp->lud_scope ) {
1322 case LDAP_SCOPE_BASE:
1323 if ( !dn_match( &nbase, op_ndn ) ) {
1327 case LDAP_SCOPE_ONELEVEL:
1328 dnParent( op_ndn, &bv );
1329 if ( !dn_match( &nbase, &bv ) ) {
1333 case LDAP_SCOPE_SUBTREE:
1334 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1338 case LDAP_SCOPE_SUBORDINATE:
1339 if ( dn_match( &nbase, op_ndn ) ||
1340 !dnIsSuffix( op_ndn, &nbase ) )
1345 filter = str2filter_x( op, ludp->lud_filter );
1347 if ( test_filter( NULL, user, filter ) ==
1352 filter_free_x( op, filter );
1355 ldap_free_urldesc( ludp );
1356 if ( !BER_BVISNULL( &nbase ) ) {
1357 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1359 if ( rc == 0 ) break;
1361 if ( user != target ) {
1362 op->o_private = user_priv;
1363 be_entry_release_r( op, user );
1364 op->o_private = o_priv;
1369 rc = value_find_ex( group_at,
1370 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1371 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1372 a->a_nvals, op_ndn, op->o_tmpmemctx );
1373 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1374 rc = LDAP_COMPARE_FALSE;
1377 rc = LDAP_NO_SUCH_ATTRIBUTE;
1379 if ( e != target ) {
1380 op->o_private = e_priv;
1381 be_entry_release_r( op, e );
1382 op->o_private = o_priv;
1385 rc = LDAP_NO_SUCH_OBJECT;
1388 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1389 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1391 g->ga_be = op->o_bd;
1392 g->ga_oc = group_oc;
1393 g->ga_at = group_at;
1395 g->ga_len = gr_ndn->bv_len;
1396 strcpy( g->ga_ndn, gr_ndn->bv_val );
1397 g->ga_next = op->o_groups;
1409 struct berval *gr_ndn,
1410 struct berval *op_ndn,
1411 ObjectClass *group_oc,
1412 AttributeDescription *group_at )
1417 if ( op->o_abandon ) {
1418 return SLAPD_ABANDON;
1422 op->o_bd = frontendDB;
1423 #ifdef SLAP_OVERLAY_ACCESS
1424 rc = frontendDB->be_group( op, target, gr_ndn,
1425 op_ndn, group_oc, group_at );
1426 #else /* ! SLAP_OVERLAY_ACCESS */
1427 rc = fe_acl_group( op, target, gr_ndn,
1428 op_ndn, group_oc, group_at );
1429 #endif /* ! SLAP_OVERLAY_ACCESS */
1440 AttributeDescription *entry_at,
1442 slap_access_t access )
1445 void *o_priv = op->o_private, *e_priv = NULL;
1446 Attribute *a = NULL;
1447 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1448 AccessControlState acl_state = ACL_STATE_INIT;
1449 Backend *be = op->o_bd;
1451 op->o_bd = select_backend( edn, 0, 0 );
1453 if ( target && dn_match( &target->e_nname, edn ) ) {
1457 op->o_private = NULL;
1458 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1459 e_priv = op->o_private;
1460 op->o_private = o_priv;
1464 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1465 assert( vals == NULL );
1468 if ( op->o_conn && access > ACL_NONE &&
1469 access_allowed( op, e, entry_at, NULL,
1470 access, &acl_state ) == 0 )
1472 rc = LDAP_INSUFFICIENT_ACCESS;
1477 a = attr_find( e->e_attrs, entry_at );
1479 SlapReply rs = { 0 };
1480 AttributeName anlist[ 2 ];
1482 anlist[ 0 ].an_name = entry_at->ad_cname;
1483 anlist[ 0 ].an_desc = entry_at;
1484 BER_BVZERO( &anlist[ 1 ].an_name );
1485 rs.sr_attrs = anlist;
1487 /* NOTE: backend_operational() is also called
1488 * when returning results, so it's supposed
1489 * to do no harm to entries */
1491 rc = backend_operational( op, &rs );
1494 if ( rc == LDAP_SUCCESS ) {
1495 if ( rs.sr_operational_attrs ) {
1497 a = rs.sr_operational_attrs;
1500 rc = LDAP_NO_SUCH_ATTRIBUTE;
1508 if ( op->o_conn && access > ACL_NONE &&
1509 access_allowed( op, e, entry_at, NULL,
1510 access, &acl_state ) == 0 )
1512 rc = LDAP_INSUFFICIENT_ACCESS;
1516 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1519 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1521 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1523 if ( op->o_conn && access > ACL_NONE &&
1524 access_allowed( op, e, entry_at,
1531 ber_dupbv_x( &v[j], &a->a_nvals[i],
1533 if ( !BER_BVISNULL( &v[j] ) ) {
1538 op->o_tmpfree( v, op->o_tmpmemctx );
1540 rc = LDAP_INSUFFICIENT_ACCESS;
1543 BER_BVZERO( &v[j] );
1548 freeit: if ( e != target ) {
1549 op->o_private = e_priv;
1550 be_entry_release_r( op, e );
1551 op->o_private = o_priv;
1567 AttributeDescription *entry_at,
1569 slap_access_t access )
1575 op->o_bd = frontendDB;
1576 #ifdef SLAP_OVERLAY_ACCESS
1577 rc = frontendDB->be_attribute( op, target, edn,
1578 entry_at, vals, access );
1579 #else /* !SLAP_OVERLAY_ACCESS */
1580 rc = fe_acl_attribute( op, target, edn,
1581 entry_at, vals, access );
1582 #endif /* !SLAP_OVERLAY_ACCESS */
1593 AttributeDescription *entry_at,
1594 struct berval *nval,
1595 slap_access_t access,
1599 void *o_priv = op->o_private, *e_priv = NULL;
1600 int rc = LDAP_INSUFFICIENT_ACCESS;
1601 Backend *be = op->o_bd;
1604 assert( op != NULL );
1605 assert( op->o_conn != NULL );
1606 assert( edn != NULL );
1607 assert( access > ACL_NONE );
1609 op->o_bd = select_backend( edn, 0, 0 );
1611 if ( target && dn_match( &target->e_nname, edn ) ) {
1615 op->o_private = NULL;
1616 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1617 e_priv = op->o_private;
1618 op->o_private = o_priv;
1622 Attribute *a = NULL;
1625 if ( entry_at == NULL ) {
1626 entry_at = slap_schema.si_ad_entry;
1629 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1631 if ( access_allowed_mask( op, e, entry_at,
1632 NULL, access, NULL, mask ) == 0 )
1634 rc = LDAP_INSUFFICIENT_ACCESS;
1641 a = attr_find( e->e_attrs, entry_at );
1643 SlapReply rs = { 0 };
1644 AttributeName anlist[ 2 ];
1646 anlist[ 0 ].an_name = entry_at->ad_cname;
1647 anlist[ 0 ].an_desc = entry_at;
1648 BER_BVZERO( &anlist[ 1 ].an_name );
1649 rs.sr_attrs = anlist;
1651 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1653 /* NOTE: backend_operational() is also called
1654 * when returning results, so it's supposed
1655 * to do no harm to entries */
1657 rc = backend_operational( op, &rs );
1660 if ( rc == LDAP_SUCCESS ) {
1661 if ( rs.sr_operational_attrs ) {
1663 a = rs.sr_operational_attrs;
1666 rc = LDAP_NO_SUCH_OBJECT;
1672 if ( access_allowed_mask( op, e, entry_at,
1673 nval, access, NULL, mask ) == 0 )
1675 rc = LDAP_INSUFFICIENT_ACCESS;
1681 freeit: if ( e != target ) {
1682 op->o_private = e_priv;
1683 be_entry_release_r( op, e );
1684 op->o_private = o_priv;
1704 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1705 /* just count them */ ;
1708 * If operational attributes (allegedly) are required,
1709 * and the backend supports specific operational attributes,
1710 * add them to the attribute list
1712 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1713 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1714 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1716 *ap = slap_operational_entryDN( rs->sr_entry );
1717 ap = &(*ap)->a_next;
1720 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1721 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1722 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1724 *ap = slap_operational_subschemaSubentry( op->o_bd );
1725 ap = &(*ap)->a_next;
1728 if ( op->o_bd != NULL )
1730 /* Let the overlays have a chance at this */
1732 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
1733 if ( !be_match( op->o_bd, frontendDB ) &&
1734 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1735 op->o_bd != NULL && op->o_bd->be_operational != NULL )
1737 rc = op->o_bd->be_operational( op, rs );
1745 int backend_operational( Operation *op, SlapReply *rs )
1750 /* Moved this into the frontend so global overlays are called */
1753 op->o_bd = frontendDB;
1754 rc = frontendDB->be_operational( op, rs );