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-2005 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 destroy_replica_info( bd );
429 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
430 ch_free( bd->be_update_ndn.bv_val );
432 if ( bd->be_update_refs ) {
433 ber_bvarray_free( bd->be_update_refs );
441 int backend_destroy(void)
446 /* destroy each backend database */
447 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
448 backend_destroy_one( bd, 1 );
451 /* destroy each backend type */
452 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
453 if( bi->bi_destroy ) {
454 bi->bi_destroy( bi );
459 LDAP_STAILQ_INIT(&backendInfo);
461 /* destroy frontend database */
464 if ( bd->bd_info->bi_db_destroy ) {
465 bd->bd_info->bi_db_destroy( bd );
467 ber_bvarray_free( bd->be_suffix );
468 ber_bvarray_free( bd->be_nsuffix );
469 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
470 free( bd->be_rootdn.bv_val );
472 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
473 free( bd->be_rootndn.bv_val );
475 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
476 free( bd->be_rootpw.bv_val );
478 acl_destroy( bd->be_acl, frontendDB->be_acl );
480 if ( bd->be_replogfile != NULL ) {
481 free( bd->be_replogfile );
483 assert( bd->be_replica == NULL );
489 BackendInfo* backend_info(const char *type)
493 /* search for the backend type */
494 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
495 if( strcasecmp(bi->bi_type, type) == 0 ) {
509 BackendInfo *bi = backend_info(type);
513 fprintf( stderr, "Unrecognized database type (%s)\n", type );
517 be = ch_calloc( 1, sizeof(Backend) );
519 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
523 be->be_def_limit = frontendDB->be_def_limit;
524 be->be_dfltaccess = frontendDB->be_dfltaccess;
526 be->be_restrictops = frontendDB->be_restrictops;
527 be->be_requires = frontendDB->be_requires;
528 be->be_ssf_set = frontendDB->be_ssf_set;
530 be->be_pcl_mutexp = &be->be_pcl_mutex;
531 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
533 /* assign a default depth limit for alias deref */
534 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
536 if ( bi->bi_db_init ) {
537 rc = bi->bi_db_init( be );
541 fprintf( stderr, "database init failed (%s)\n", type );
555 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
556 if ( be->bd_info->bi_db_close ) {
557 be->bd_info->bi_db_close( be );
561 if ( frontendDB->bd_info->bi_db_close ) {
562 (*frontendDB->bd_info->bi_db_close)( frontendDB );
574 ber_len_t len, dnlen = dn->bv_len;
575 Backend *be, *b2 = NULL;
577 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
578 if ( be->be_nsuffix == NULL ) {
582 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
584 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
589 len = be->be_nsuffix[j].bv_len;
592 /* suffix is longer than DN */
597 * input DN is normalized, so the separator check
598 * need not look at escaping
600 if ( len && len < dnlen &&
601 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
606 if ( strcmp( be->be_nsuffix[j].bv_val,
607 &dn->bv_val[dnlen-len] ) == 0 )
612 if( manageDSAit && len == dnlen &&
613 !SLAP_GLUE_SUBORDINATE( be ) ) {
630 struct berval *bvsuffix )
634 if ( be->be_nsuffix == NULL ) {
638 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
639 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
648 be_isroot_dn( Backend *be, struct berval *ndn )
650 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
654 return dn_match( &be->be_rootndn, ndn );
658 be_slurp_update( Operation *op )
660 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
661 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
665 be_shadow_update( Operation *op )
667 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
668 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
672 be_isupdate_dn( Backend *be, struct berval *ndn )
674 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
678 return dn_match( &be->be_update_ndn, ndn );
682 be_root_dn( Backend *be )
684 return &be->be_rootdn;
688 be_isroot( Operation *op )
690 return be_isroot_dn( op->o_bd, &op->o_ndn );
694 be_isroot_pw( Operation *op )
698 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
702 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
707 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
708 op->o_conn->c_sasl_authctx, NULL );
711 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
714 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
726 if ( op->o_bd->be_release ) {
727 /* free and release entry from backend */
728 return op->o_bd->be_release( op, e, rw );
737 backend_unbind( Operation *op, SlapReply *rs )
741 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
742 if ( be->be_unbind ) {
744 be->be_unbind( op, rs );
752 backend_connection_init(
757 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
758 if ( be->be_connection_init ) {
759 be->be_connection_init( be, conn );
767 backend_connection_destroy(
772 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
773 if ( be->be_connection_destroy ) {
774 be->be_connection_destroy( be, conn);
782 backend_check_controls(
786 LDAPControl **ctrls = op->o_ctrls;
787 rs->sr_err = LDAP_SUCCESS;
790 for( ; *ctrls != NULL ; ctrls++ ) {
793 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
794 case LDAP_CONTROL_NOT_FOUND:
795 /* unrecognized control */
796 if ( (*ctrls)->ldctl_iscritical ) {
797 /* should not be reachable */
798 Debug( LDAP_DEBUG_ANY,
799 "backend_check_controls: unrecognized control: %s\n",
800 (*ctrls)->ldctl_oid, 0, 0 );
805 case LDAP_COMPARE_FALSE:
806 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
807 /* Per RFC 2251 (and LDAPBIS discussions), if the control
808 * is recognized and appropriate for the operation (which
809 * we've already verified), then the server should make
810 * use of the control when performing the operation.
812 * Here we find that operation extended by the control
813 * is unavailable in a particular context, and the control
814 * is marked Critical, hence the return of
815 * unwillingToPerform.
817 rs->sr_text = "critical control unavailable in context";
818 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
823 case LDAP_COMPARE_TRUE:
828 Debug( LDAP_DEBUG_ANY,
829 "backend_check_controls: unable to check control: %s\n",
830 (*ctrls)->ldctl_oid, 0, 0 );
833 rs->sr_text = "unable to check control";
834 rs->sr_err = LDAP_OTHER;
840 /* temporarily removed */
842 /* check should be generalized */
843 if( get_manageDIT(op) && !be_isroot(op)) {
844 rs->sr_text = "requires manager authorization";
845 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
854 backend_check_restrictions(
857 struct berval *opdata )
859 slap_mask_t restrictops;
860 slap_mask_t requires;
862 slap_mask_t exopflag = 0;
869 int rc = SLAP_CB_CONTINUE;
871 if ( op->o_bd->be_chk_controls ) {
872 rc = ( *op->o_bd->be_chk_controls )( op, rs );
875 if ( rc == SLAP_CB_CONTINUE ) {
876 rc = backend_check_controls( op, rs );
879 if ( rc != LDAP_SUCCESS ) {
883 restrictops = op->o_bd->be_restrictops;
884 requires = op->o_bd->be_requires;
885 ssf = &op->o_bd->be_ssf_set;
888 restrictops = frontendDB->be_restrictops;
889 requires = frontendDB->be_requires;
890 ssf = &frontendDB->be_ssf_set;
893 switch( op->o_tag ) {
895 opflag = SLAP_RESTRICT_OP_ADD;
899 opflag = SLAP_RESTRICT_OP_BIND;
902 case LDAP_REQ_COMPARE:
903 opflag = SLAP_RESTRICT_OP_COMPARE;
905 case LDAP_REQ_DELETE:
907 opflag = SLAP_RESTRICT_OP_DELETE;
909 case LDAP_REQ_EXTENDED:
910 opflag = SLAP_RESTRICT_OP_EXTENDED;
913 /* treat unspecified as a modify */
914 opflag = SLAP_RESTRICT_OP_MODIFY;
919 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
922 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
926 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
927 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
931 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
932 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
936 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
937 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
942 /* treat everything else as a modify */
943 opflag = SLAP_RESTRICT_OP_MODIFY;
947 case LDAP_REQ_MODIFY:
949 opflag = SLAP_RESTRICT_OP_MODIFY;
951 case LDAP_REQ_RENAME:
953 opflag = SLAP_RESTRICT_OP_RENAME;
955 case LDAP_REQ_SEARCH:
956 opflag = SLAP_RESTRICT_OP_SEARCH;
958 case LDAP_REQ_UNBIND:
963 rs->sr_text = "restrict operations internal error";
964 rs->sr_err = LDAP_OTHER;
969 /* these checks don't apply to StartTLS */
971 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
972 if( op->o_transport_ssf < ssf->sss_transport ) {
973 rs->sr_text = op->o_transport_ssf
974 ? "stronger transport confidentiality required"
975 : "transport confidentiality required";
979 if( op->o_tls_ssf < ssf->sss_tls ) {
980 rs->sr_text = op->o_tls_ssf
981 ? "stronger TLS confidentiality required"
982 : "TLS confidentiality required";
987 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
988 /* simple bind specific check */
989 if( op->o_ssf < ssf->sss_simple_bind ) {
990 rs->sr_text = op->o_ssf
991 ? "stronger confidentiality required"
992 : "confidentiality required";
997 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
998 /* these checks don't apply to SASL bind */
1000 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1001 rs->sr_text = op->o_sasl_ssf
1002 ? "stronger SASL confidentiality required"
1003 : "SASL confidentiality required";
1007 if( op->o_ssf < ssf->sss_ssf ) {
1008 rs->sr_text = op->o_ssf
1009 ? "stronger confidentiality required"
1010 : "confidentiality required";
1016 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1017 rs->sr_text = op->o_transport_ssf
1018 ? "stronger transport confidentiality required for update"
1019 : "transport confidentiality required for update";
1023 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1024 rs->sr_text = op->o_tls_ssf
1025 ? "stronger TLS confidentiality required for update"
1026 : "TLS confidentiality required for update";
1030 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1031 rs->sr_text = op->o_sasl_ssf
1032 ? "stronger SASL confidentiality required for update"
1033 : "SASL confidentiality required for update";
1037 if( op->o_ssf < ssf->sss_update_ssf ) {
1038 rs->sr_text = op->o_ssf
1039 ? "stronger confidentiality required for update"
1040 : "confidentiality required for update";
1044 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1045 BER_BVISEMPTY( &op->o_ndn ) )
1047 rs->sr_text = "modifications require authentication";
1048 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1052 #ifdef SLAP_X_LISTENER_MOD
1053 if ( op->o_conn->c_listener &&
1054 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1055 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1057 /* no "w" mode means readonly */
1058 rs->sr_text = "modifications not allowed on this listener";
1059 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1062 #endif /* SLAP_X_LISTENER_MOD */
1067 /* these checks don't apply to Bind, StartTLS, or Unbind */
1069 if( requires & SLAP_REQUIRE_STRONG ) {
1070 /* should check mechanism */
1071 if( ( op->o_transport_ssf < ssf->sss_transport
1072 && op->o_authtype == LDAP_AUTH_SIMPLE )
1073 || BER_BVISEMPTY( &op->o_dn ) )
1075 rs->sr_text = "strong(er) authentication required";
1076 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1081 if( requires & SLAP_REQUIRE_SASL ) {
1082 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1083 rs->sr_text = "SASL authentication required";
1084 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1089 if( requires & SLAP_REQUIRE_AUTHC ) {
1090 if( BER_BVISEMPTY( &op->o_dn ) ) {
1091 rs->sr_text = "authentication required";
1092 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1097 if( requires & SLAP_REQUIRE_BIND ) {
1099 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1100 version = op->o_conn->c_protocol;
1101 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1104 /* no bind has occurred */
1105 rs->sr_text = "BIND required";
1106 rs->sr_err = LDAP_OPERATIONS_ERROR;
1111 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1112 if( op->o_protocol < LDAP_VERSION3 ) {
1113 /* no bind has occurred */
1114 rs->sr_text = "operation restricted to LDAPv3 clients";
1115 rs->sr_err = LDAP_OPERATIONS_ERROR;
1120 #ifdef SLAP_X_LISTENER_MOD
1121 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1122 if ( op->o_conn->c_listener &&
1123 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1125 /* no "x" mode means bind required */
1126 rs->sr_text = "bind required on this listener";
1127 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1132 if ( !starttls && !updateop ) {
1133 if ( op->o_conn->c_listener &&
1134 !( op->o_conn->c_listener->sl_perms &
1135 ( !BER_BVISEMPTY( &op->o_dn )
1136 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1138 /* no "r" mode means no read */
1139 rs->sr_text = "read not allowed on this listener";
1140 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1144 #endif /* SLAP_X_LISTENER_MOD */
1148 if( ( restrictops & opflag )
1149 || ( exopflag && ( restrictops & exopflag ) ) ) {
1150 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1151 rs->sr_text = "read operations restricted";
1152 } else if ( restrictops & exopflag ) {
1153 rs->sr_text = "extended operation restricted";
1155 rs->sr_text = "operation restricted";
1157 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1161 rs->sr_err = LDAP_SUCCESS;
1165 int backend_check_referrals( Operation *op, SlapReply *rs )
1167 rs->sr_err = LDAP_SUCCESS;
1169 if( op->o_bd->be_chk_referrals ) {
1170 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1172 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1173 send_ldap_result( op, rs );
1185 AttributeDescription *at,
1191 if ( op->o_bd == NULL ) {
1192 return LDAP_NO_SUCH_OBJECT;
1195 if ( op->o_bd->be_fetch ) {
1196 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1199 return LDAP_UNWILLING_TO_PERFORM;
1206 struct berval *gr_ndn,
1207 struct berval *op_ndn,
1208 ObjectClass *group_oc,
1209 AttributeDescription *group_at )
1215 Backend *be = op->o_bd;
1217 op->o_bd = select_backend( gr_ndn, 0, 0 );
1219 for ( g = op->o_groups; g; g = g->ga_next ) {
1220 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1221 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1225 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1235 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1239 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1242 a = attr_find( e->e_attrs, group_at );
1244 /* If the attribute is a subtype of labeledURI, treat this as
1245 * a dynamic group ala groupOfURLs
1247 if (is_at_subtype( group_at->ad_type,
1248 slap_schema.si_ad_labeledURI->ad_type ) )
1252 struct berval bv, nbase;
1255 Backend *b2 = op->o_bd;
1257 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1260 op->o_bd = select_backend( op_ndn, 0, 0 );
1261 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1265 rc = LDAP_COMPARE_FALSE;
1266 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1267 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1272 BER_BVZERO( &nbase );
1273 /* host part must be empty */
1274 /* attrs and extensions parts must be empty */
1275 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1276 ludp->lud_attrs || ludp->lud_exts )
1280 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1281 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1282 op->o_tmpmemctx ) != LDAP_SUCCESS )
1286 switch ( ludp->lud_scope ) {
1287 case LDAP_SCOPE_BASE:
1288 if ( !dn_match( &nbase, op_ndn ) ) {
1292 case LDAP_SCOPE_ONELEVEL:
1293 dnParent( op_ndn, &bv );
1294 if ( !dn_match( &nbase, &bv ) ) {
1298 case LDAP_SCOPE_SUBTREE:
1299 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1303 #ifdef LDAP_SCOPE_SUBORDINATE
1304 case LDAP_SCOPE_SUBORDINATE:
1305 if ( dn_match( &nbase, op_ndn ) ||
1306 !dnIsSuffix( op_ndn, &nbase ) )
1312 filter = str2filter_x( op, ludp->lud_filter );
1314 if ( test_filter( NULL, user, filter ) ==
1319 filter_free_x( op, filter );
1322 ldap_free_urldesc( ludp );
1323 if ( !BER_BVISNULL( &nbase ) ) {
1324 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1326 if ( rc == 0 ) break;
1328 if ( user != target ) {
1329 be_entry_release_r( op, user );
1334 rc = value_find_ex( group_at,
1335 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1336 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1337 a->a_nvals, op_ndn, op->o_tmpmemctx );
1338 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1339 rc = LDAP_COMPARE_FALSE;
1342 rc = LDAP_NO_SUCH_ATTRIBUTE;
1345 be_entry_release_r( op, e );
1348 rc = LDAP_NO_SUCH_OBJECT;
1351 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1352 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1354 g->ga_be = op->o_bd;
1355 g->ga_oc = group_oc;
1356 g->ga_at = group_at;
1358 g->ga_len = gr_ndn->bv_len;
1359 strcpy( g->ga_ndn, gr_ndn->bv_val );
1360 g->ga_next = op->o_groups;
1372 struct berval *gr_ndn,
1373 struct berval *op_ndn,
1374 ObjectClass *group_oc,
1375 AttributeDescription *group_at )
1380 if ( op->o_abandon ) {
1381 return SLAPD_ABANDON;
1385 op->o_bd = frontendDB;
1386 #ifdef SLAP_OVERLAY_ACCESS
1387 rc = frontendDB->be_group( op, target, gr_ndn,
1388 op_ndn, group_oc, group_at );
1389 #else /* ! SLAP_OVERLAY_ACCESS */
1390 rc = fe_acl_group( op, target, gr_ndn,
1391 op_ndn, group_oc, group_at );
1392 #endif /* ! SLAP_OVERLAY_ACCESS */
1403 AttributeDescription *entry_at,
1405 slap_access_t access )
1408 Attribute *a = NULL;
1409 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1410 AccessControlState acl_state = ACL_STATE_INIT;
1411 Backend *be = op->o_bd;
1413 op->o_bd = select_backend( edn, 0, 0 );
1415 if ( target && dn_match( &target->e_nname, edn ) ) {
1419 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1423 a = attr_find( e->e_attrs, entry_at );
1425 SlapReply rs = { 0 };
1426 AttributeName anlist[ 2 ];
1428 anlist[ 0 ].an_name = entry_at->ad_cname;
1429 anlist[ 0 ].an_desc = entry_at;
1430 BER_BVZERO( &anlist[ 1 ].an_name );
1431 rs.sr_attrs = anlist;
1433 /* NOTE: backend_operational() is also called
1434 * when returning results, so it's supposed
1435 * to do no harm to entries */
1437 rc = backend_operational( op, &rs );
1440 if ( rc == LDAP_SUCCESS ) {
1441 if ( rs.sr_operational_attrs ) {
1443 a = rs.sr_operational_attrs;
1446 rc = LDAP_NO_SUCH_ATTRIBUTE;
1454 if ( op->o_conn && access > ACL_NONE &&
1455 access_allowed( op, e, entry_at, NULL,
1456 access, &acl_state ) == 0 )
1458 rc = LDAP_INSUFFICIENT_ACCESS;
1462 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1465 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1467 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1469 if ( op->o_conn && access > ACL_NONE &&
1470 access_allowed( op, e, entry_at,
1477 ber_dupbv_x( &v[j], &a->a_nvals[i],
1479 if ( !BER_BVISNULL( &v[j] ) ) {
1484 op->o_tmpfree( v, op->o_tmpmemctx );
1486 rc = LDAP_INSUFFICIENT_ACCESS;
1489 BER_BVZERO( &v[j] );
1494 freeit: if ( e != target ) {
1495 be_entry_release_r( op, e );
1511 AttributeDescription *entry_at,
1513 slap_access_t access )
1519 op->o_bd = frontendDB;
1520 #ifdef SLAP_OVERLAY_ACCESS
1521 rc = frontendDB->be_attribute( op, target, edn,
1522 entry_at, vals, access );
1523 #else /* !SLAP_OVERLAY_ACCESS */
1524 rc = fe_acl_attribute( op, target, edn,
1525 entry_at, vals, access );
1526 #endif /* !SLAP_OVERLAY_ACCESS */
1537 AttributeDescription *entry_at,
1538 struct berval *nval,
1539 slap_access_t access,
1543 int rc = LDAP_INSUFFICIENT_ACCESS;
1544 Backend *be = op->o_bd;
1547 assert( op != NULL );
1548 assert( op->o_conn != NULL );
1549 assert( edn != NULL );
1550 assert( access > ACL_NONE );
1552 op->o_bd = select_backend( edn, 0, 0 );
1554 if ( target && dn_match( &target->e_nname, edn ) ) {
1558 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1562 Attribute *a = NULL;
1565 if ( entry_at == NULL ) {
1566 entry_at = slap_schema.si_ad_entry;
1569 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1571 if ( access_allowed_mask( op, e, entry_at,
1572 NULL, access, NULL, mask ) == 0 )
1574 rc = LDAP_INSUFFICIENT_ACCESS;
1581 a = attr_find( e->e_attrs, entry_at );
1583 SlapReply rs = { 0 };
1584 AttributeName anlist[ 2 ];
1586 anlist[ 0 ].an_name = entry_at->ad_cname;
1587 anlist[ 0 ].an_desc = entry_at;
1588 BER_BVZERO( &anlist[ 1 ].an_name );
1589 rs.sr_attrs = anlist;
1591 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1593 /* NOTE: backend_operational() is also called
1594 * when returning results, so it's supposed
1595 * to do no harm to entries */
1597 rc = backend_operational( op, &rs );
1600 if ( rc == LDAP_SUCCESS ) {
1601 if ( rs.sr_operational_attrs ) {
1603 a = rs.sr_operational_attrs;
1606 rc = LDAP_NO_SUCH_OBJECT;
1612 if ( access_allowed_mask( op, e, entry_at,
1613 nval, access, NULL, mask ) == 0 )
1615 rc = LDAP_INSUFFICIENT_ACCESS;
1621 freeit: if ( e != target ) {
1622 be_entry_release_r( op, e );
1642 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1643 /* just count them */ ;
1646 * If operational attributes (allegedly) are required,
1647 * and the backend supports specific operational attributes,
1648 * add them to the attribute list
1650 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1651 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1653 *ap = slap_operational_entryDN( rs->sr_entry );
1654 ap = &(*ap)->a_next;
1657 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1658 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1660 *ap = slap_operational_subschemaSubentry( op->o_bd );
1661 ap = &(*ap)->a_next;
1664 if ( op->o_bd != NULL )
1666 /* Let the overlays have a chance at this */
1668 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
1669 if ( !be_match( op->o_bd, frontendDB ) &&
1670 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1671 op->o_bd != NULL && op->o_bd->be_operational != NULL )
1673 rc = op->o_bd->be_operational( op, rs );
1681 int backend_operational( Operation *op, SlapReply *rs )
1686 /* Moved this into the frontend so global overlays are called */
1689 op->o_bd = frontendDB;
1690 rc = frontendDB->be_operational( op, rs );