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 );
430 int backend_destroy(void)
435 /* destroy each backend database */
436 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
437 backend_destroy_one( bd, 1 );
440 /* destroy each backend type */
441 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
442 if( bi->bi_destroy ) {
443 bi->bi_destroy( bi );
448 LDAP_STAILQ_INIT(&backendInfo);
450 /* destroy frontend database */
453 if ( bd->bd_info->bi_db_destroy ) {
454 bd->bd_info->bi_db_destroy( bd );
456 ber_bvarray_free( bd->be_suffix );
457 ber_bvarray_free( bd->be_nsuffix );
458 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
459 free( bd->be_rootdn.bv_val );
461 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
462 free( bd->be_rootndn.bv_val );
464 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
465 free( bd->be_rootpw.bv_val );
467 acl_destroy( bd->be_acl, frontendDB->be_acl );
473 BackendInfo* backend_info(const char *type)
477 /* search for the backend type */
478 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
479 if( strcasecmp(bi->bi_type, type) == 0 ) {
493 BackendInfo *bi = backend_info(type);
497 fprintf( stderr, "Unrecognized database type (%s)\n", type );
501 be = ch_calloc( 1, sizeof(Backend) );
503 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
507 be->be_def_limit = frontendDB->be_def_limit;
508 be->be_dfltaccess = frontendDB->be_dfltaccess;
510 be->be_restrictops = frontendDB->be_restrictops;
511 be->be_requires = frontendDB->be_requires;
512 be->be_ssf_set = frontendDB->be_ssf_set;
514 be->be_pcl_mutexp = &be->be_pcl_mutex;
515 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
517 /* assign a default depth limit for alias deref */
518 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
520 if ( bi->bi_db_init ) {
521 rc = bi->bi_db_init( be );
525 fprintf( stderr, "database init failed (%s)\n", type );
539 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
540 if ( be->bd_info->bi_db_close ) {
541 be->bd_info->bi_db_close( be );
545 if ( frontendDB->bd_info->bi_db_close ) {
546 (*frontendDB->bd_info->bi_db_close)( frontendDB );
558 ber_len_t len, dnlen = dn->bv_len;
559 Backend *be, *b2 = NULL;
561 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
562 if ( be->be_nsuffix == NULL ) {
566 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
568 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
573 len = be->be_nsuffix[j].bv_len;
576 /* suffix is longer than DN */
581 * input DN is normalized, so the separator check
582 * need not look at escaping
584 if ( len && len < dnlen &&
585 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
590 if ( strcmp( be->be_nsuffix[j].bv_val,
591 &dn->bv_val[dnlen-len] ) == 0 )
596 if( manageDSAit && len == dnlen &&
597 !SLAP_GLUE_SUBORDINATE( be ) ) {
614 struct berval *bvsuffix )
618 if ( be->be_nsuffix == NULL ) {
622 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
623 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
632 be_isroot_dn( Backend *be, struct berval *ndn )
634 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
638 return dn_match( &be->be_rootndn, ndn );
642 be_slurp_update( Operation *op )
644 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
645 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
649 be_shadow_update( Operation *op )
651 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
652 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
656 be_isupdate_dn( Backend *be, struct berval *ndn )
658 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
662 return dn_match( &be->be_update_ndn, ndn );
666 be_root_dn( Backend *be )
668 return &be->be_rootdn;
672 be_isroot( Operation *op )
674 return be_isroot_dn( op->o_bd, &op->o_ndn );
678 be_isroot_pw( Operation *op )
682 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
686 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
691 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
692 op->o_conn->c_sasl_authctx, NULL );
695 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
698 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
710 if ( op->o_bd->be_release ) {
711 /* free and release entry from backend */
712 return op->o_bd->be_release( op, e, rw );
721 backend_unbind( Operation *op, SlapReply *rs )
725 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
726 if ( be->be_unbind ) {
728 be->be_unbind( op, rs );
736 backend_connection_init(
741 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
742 if ( be->be_connection_init ) {
743 be->be_connection_init( be, conn );
751 backend_connection_destroy(
756 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
757 if ( be->be_connection_destroy ) {
758 be->be_connection_destroy( be, conn);
766 backend_check_controls(
770 LDAPControl **ctrls = op->o_ctrls;
771 rs->sr_err = LDAP_SUCCESS;
774 for( ; *ctrls != NULL ; ctrls++ ) {
777 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
778 case LDAP_CONTROL_NOT_FOUND:
779 /* unrecognized control */
780 if ( (*ctrls)->ldctl_iscritical ) {
781 /* should not be reachable */
782 Debug( LDAP_DEBUG_ANY,
783 "backend_check_controls: unrecognized control: %s\n",
784 (*ctrls)->ldctl_oid, 0, 0 );
789 case LDAP_COMPARE_FALSE:
790 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
791 /* Per RFC 2251 (and LDAPBIS discussions), if the control
792 * is recognized and appropriate for the operation (which
793 * we've already verified), then the server should make
794 * use of the control when performing the operation.
796 * Here we find that operation extended by the control
797 * is unavailable in a particular context, and the control
798 * is marked Critical, hence the return of
799 * unwillingToPerform.
801 rs->sr_text = "critical control unavailable in context";
802 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
807 case LDAP_COMPARE_TRUE:
812 Debug( LDAP_DEBUG_ANY,
813 "backend_check_controls: unable to check control: %s\n",
814 (*ctrls)->ldctl_oid, 0, 0 );
817 rs->sr_text = "unable to check control";
818 rs->sr_err = LDAP_OTHER;
824 /* check should be generalized */
825 if( get_manageDIT(op) && !be_isroot(op)) {
826 rs->sr_text = "requires manager authorization";
827 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
835 backend_check_restrictions(
838 struct berval *opdata )
840 slap_mask_t restrictops;
841 slap_mask_t requires;
843 slap_mask_t exopflag = 0;
850 int rc = SLAP_CB_CONTINUE;
852 if ( op->o_bd->be_chk_controls ) {
853 rc = ( *op->o_bd->be_chk_controls )( op, rs );
856 if ( rc == SLAP_CB_CONTINUE ) {
857 rc = backend_check_controls( op, rs );
860 if ( rc != LDAP_SUCCESS ) {
864 restrictops = op->o_bd->be_restrictops;
865 requires = op->o_bd->be_requires;
866 ssf = &op->o_bd->be_ssf_set;
869 restrictops = frontendDB->be_restrictops;
870 requires = frontendDB->be_requires;
871 ssf = &frontendDB->be_ssf_set;
874 switch( op->o_tag ) {
876 opflag = SLAP_RESTRICT_OP_ADD;
880 opflag = SLAP_RESTRICT_OP_BIND;
883 case LDAP_REQ_COMPARE:
884 opflag = SLAP_RESTRICT_OP_COMPARE;
886 case LDAP_REQ_DELETE:
888 opflag = SLAP_RESTRICT_OP_DELETE;
890 case LDAP_REQ_EXTENDED:
891 opflag = SLAP_RESTRICT_OP_EXTENDED;
894 /* treat unspecified as a modify */
895 opflag = SLAP_RESTRICT_OP_MODIFY;
900 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
903 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
907 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
908 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
912 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
913 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
917 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
918 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
923 /* treat everything else as a modify */
924 opflag = SLAP_RESTRICT_OP_MODIFY;
928 case LDAP_REQ_MODIFY:
930 opflag = SLAP_RESTRICT_OP_MODIFY;
932 case LDAP_REQ_RENAME:
934 opflag = SLAP_RESTRICT_OP_RENAME;
936 case LDAP_REQ_SEARCH:
937 opflag = SLAP_RESTRICT_OP_SEARCH;
939 case LDAP_REQ_UNBIND:
944 rs->sr_text = "restrict operations internal error";
945 rs->sr_err = LDAP_OTHER;
950 /* these checks don't apply to StartTLS */
952 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
953 if( op->o_transport_ssf < ssf->sss_transport ) {
954 rs->sr_text = op->o_transport_ssf
955 ? "stronger transport confidentiality required"
956 : "transport confidentiality required";
960 if( op->o_tls_ssf < ssf->sss_tls ) {
961 rs->sr_text = op->o_tls_ssf
962 ? "stronger TLS confidentiality required"
963 : "TLS confidentiality required";
968 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
969 /* simple bind specific check */
970 if( op->o_ssf < ssf->sss_simple_bind ) {
971 rs->sr_text = op->o_ssf
972 ? "stronger confidentiality required"
973 : "confidentiality required";
978 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
979 /* these checks don't apply to SASL bind */
981 if( op->o_sasl_ssf < ssf->sss_sasl ) {
982 rs->sr_text = op->o_sasl_ssf
983 ? "stronger SASL confidentiality required"
984 : "SASL confidentiality required";
988 if( op->o_ssf < ssf->sss_ssf ) {
989 rs->sr_text = op->o_ssf
990 ? "stronger confidentiality required"
991 : "confidentiality required";
997 if( op->o_transport_ssf < ssf->sss_update_transport ) {
998 rs->sr_text = op->o_transport_ssf
999 ? "stronger transport confidentiality required for update"
1000 : "transport confidentiality required for update";
1004 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1005 rs->sr_text = op->o_tls_ssf
1006 ? "stronger TLS confidentiality required for update"
1007 : "TLS confidentiality required for update";
1011 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1012 rs->sr_text = op->o_sasl_ssf
1013 ? "stronger SASL confidentiality required for update"
1014 : "SASL confidentiality required for update";
1018 if( op->o_ssf < ssf->sss_update_ssf ) {
1019 rs->sr_text = op->o_ssf
1020 ? "stronger confidentiality required for update"
1021 : "confidentiality required for update";
1025 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1026 BER_BVISEMPTY( &op->o_ndn ) )
1028 rs->sr_text = "modifications require authentication";
1029 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1033 #ifdef SLAP_X_LISTENER_MOD
1034 if ( op->o_conn->c_listener &&
1035 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1036 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1038 /* no "w" mode means readonly */
1039 rs->sr_text = "modifications not allowed on this listener";
1040 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1043 #endif /* SLAP_X_LISTENER_MOD */
1048 /* these checks don't apply to Bind, StartTLS, or Unbind */
1050 if( requires & SLAP_REQUIRE_STRONG ) {
1051 /* should check mechanism */
1052 if( ( op->o_transport_ssf < ssf->sss_transport
1053 && op->o_authtype == LDAP_AUTH_SIMPLE )
1054 || BER_BVISEMPTY( &op->o_dn ) )
1056 rs->sr_text = "strong(er) authentication required";
1057 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1062 if( requires & SLAP_REQUIRE_SASL ) {
1063 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1064 rs->sr_text = "SASL authentication required";
1065 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1070 if( requires & SLAP_REQUIRE_AUTHC ) {
1071 if( BER_BVISEMPTY( &op->o_dn ) ) {
1072 rs->sr_text = "authentication required";
1073 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1078 if( requires & SLAP_REQUIRE_BIND ) {
1080 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1081 version = op->o_conn->c_protocol;
1082 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1085 /* no bind has occurred */
1086 rs->sr_text = "BIND required";
1087 rs->sr_err = LDAP_OPERATIONS_ERROR;
1092 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1093 if( op->o_protocol < LDAP_VERSION3 ) {
1094 /* no bind has occurred */
1095 rs->sr_text = "operation restricted to LDAPv3 clients";
1096 rs->sr_err = LDAP_OPERATIONS_ERROR;
1101 #ifdef SLAP_X_LISTENER_MOD
1102 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1103 if ( op->o_conn->c_listener &&
1104 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1106 /* no "x" mode means bind required */
1107 rs->sr_text = "bind required on this listener";
1108 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1113 if ( !starttls && !updateop ) {
1114 if ( op->o_conn->c_listener &&
1115 !( op->o_conn->c_listener->sl_perms &
1116 ( !BER_BVISEMPTY( &op->o_dn )
1117 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1119 /* no "r" mode means no read */
1120 rs->sr_text = "read not allowed on this listener";
1121 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1125 #endif /* SLAP_X_LISTENER_MOD */
1129 if( ( restrictops & opflag )
1130 || ( exopflag && ( restrictops & exopflag ) ) ) {
1131 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1132 rs->sr_text = "read operations restricted";
1133 } else if ( restrictops & exopflag ) {
1134 rs->sr_text = "extended operation restricted";
1136 rs->sr_text = "operation restricted";
1138 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1142 rs->sr_err = LDAP_SUCCESS;
1146 int backend_check_referrals( Operation *op, SlapReply *rs )
1148 rs->sr_err = LDAP_SUCCESS;
1150 if( op->o_bd->be_chk_referrals ) {
1151 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1153 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1154 send_ldap_result( op, rs );
1166 AttributeDescription *at,
1174 if (op->o_bd == NULL) {
1175 rc = LDAP_NO_SUCH_OBJECT;
1176 } else if ( op->o_bd->be_fetch ) {
1177 rc = ( op->o_bd->be_fetch )( op, ndn,
1180 rc = LDAP_UNWILLING_TO_PERFORM;
1189 struct berval *gr_ndn,
1190 struct berval *op_ndn,
1191 ObjectClass *group_oc,
1192 AttributeDescription *group_at )
1198 Backend *be = op->o_bd;
1200 op->o_bd = select_backend( gr_ndn, 0, 0 );
1202 for ( g = op->o_groups; g; g = g->ga_next ) {
1203 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1204 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1208 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1218 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1222 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1225 a = attr_find( e->e_attrs, group_at );
1227 /* If the attribute is a subtype of labeledURI, treat this as
1228 * a dynamic group ala groupOfURLs
1230 if (is_at_subtype( group_at->ad_type,
1231 slap_schema.si_ad_labeledURI->ad_type ) )
1235 struct berval bv, nbase;
1238 Backend *b2 = op->o_bd;
1240 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1243 op->o_bd = select_backend( op_ndn, 0, 0 );
1244 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1248 rc = LDAP_COMPARE_FALSE;
1249 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1250 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1255 BER_BVZERO( &nbase );
1256 /* host part must be empty */
1257 /* attrs and extensions parts must be empty */
1258 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1259 ludp->lud_attrs || ludp->lud_exts )
1263 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1264 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1265 op->o_tmpmemctx ) != LDAP_SUCCESS )
1269 switch ( ludp->lud_scope ) {
1270 case LDAP_SCOPE_BASE:
1271 if ( !dn_match( &nbase, op_ndn ) ) {
1275 case LDAP_SCOPE_ONELEVEL:
1276 dnParent( op_ndn, &bv );
1277 if ( !dn_match( &nbase, &bv ) ) {
1281 case LDAP_SCOPE_SUBTREE:
1282 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1286 #ifdef LDAP_SCOPE_SUBORDINATE
1287 case LDAP_SCOPE_SUBORDINATE:
1288 if ( dn_match( &nbase, op_ndn ) ||
1289 !dnIsSuffix( op_ndn, &nbase ) )
1295 filter = str2filter_x( op, ludp->lud_filter );
1297 if ( test_filter( NULL, user, filter ) ==
1302 filter_free_x( op, filter );
1305 ldap_free_urldesc( ludp );
1306 if ( !BER_BVISNULL( &nbase ) ) {
1307 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1309 if ( rc == 0 ) break;
1311 if ( user != target ) {
1312 be_entry_release_r( op, user );
1317 rc = value_find_ex( group_at,
1318 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1319 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1320 a->a_nvals, op_ndn, op->o_tmpmemctx );
1321 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1322 rc = LDAP_COMPARE_FALSE;
1325 rc = LDAP_NO_SUCH_ATTRIBUTE;
1328 be_entry_release_r( op, e );
1331 rc = LDAP_NO_SUCH_OBJECT;
1334 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1335 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1337 g->ga_be = op->o_bd;
1338 g->ga_oc = group_oc;
1339 g->ga_at = group_at;
1341 g->ga_len = gr_ndn->bv_len;
1342 strcpy( g->ga_ndn, gr_ndn->bv_val );
1343 g->ga_next = op->o_groups;
1355 struct berval *gr_ndn,
1356 struct berval *op_ndn,
1357 ObjectClass *group_oc,
1358 AttributeDescription *group_at )
1363 if ( op->o_abandon ) {
1364 return SLAPD_ABANDON;
1368 op->o_bd = frontendDB;
1369 rc = frontendDB->be_group( op, target, gr_ndn,
1370 op_ndn, group_oc, group_at );
1381 AttributeDescription *entry_at,
1383 slap_access_t access )
1386 Attribute *a = NULL;
1387 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1388 AccessControlState acl_state = ACL_STATE_INIT;
1389 Backend *be = op->o_bd;
1391 op->o_bd = select_backend( edn, 0, 0 );
1393 if ( target && dn_match( &target->e_nname, edn ) ) {
1397 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1401 a = attr_find( e->e_attrs, entry_at );
1403 SlapReply rs = { 0 };
1404 AttributeName anlist[ 2 ];
1406 anlist[ 0 ].an_name = entry_at->ad_cname;
1407 anlist[ 0 ].an_desc = entry_at;
1408 BER_BVZERO( &anlist[ 1 ].an_name );
1409 rs.sr_attrs = anlist;
1411 /* NOTE: backend_operational() is also called
1412 * when returning results, so it's supposed
1413 * to do no harm to entries */
1415 rc = backend_operational( op, &rs );
1418 if ( rc == LDAP_SUCCESS ) {
1419 if ( rs.sr_operational_attrs ) {
1421 a = rs.sr_operational_attrs;
1424 rc = LDAP_NO_SUCH_ATTRIBUTE;
1432 if ( op->o_conn && access > ACL_NONE &&
1433 access_allowed( op, e, entry_at, NULL,
1434 access, &acl_state ) == 0 )
1436 rc = LDAP_INSUFFICIENT_ACCESS;
1440 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1443 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1445 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1447 if ( op->o_conn && access > ACL_NONE &&
1448 access_allowed( op, e, entry_at,
1455 ber_dupbv_x( &v[j], &a->a_nvals[i],
1457 if ( !BER_BVISNULL( &v[j] ) ) {
1462 op->o_tmpfree( v, op->o_tmpmemctx );
1464 rc = LDAP_INSUFFICIENT_ACCESS;
1467 BER_BVZERO( &v[j] );
1472 freeit: if ( e != target ) {
1473 be_entry_release_r( op, e );
1489 AttributeDescription *entry_at,
1491 slap_access_t access )
1497 op->o_bd = frontendDB;
1498 rc = frontendDB->be_attribute( op, target, edn,
1499 entry_at, vals, access );
1510 AttributeDescription *entry_at,
1511 struct berval *nval,
1512 slap_access_t access,
1516 int rc = LDAP_INSUFFICIENT_ACCESS;
1517 Backend *be = op->o_bd;
1520 assert( op != NULL );
1521 assert( op->o_conn != NULL );
1522 assert( edn != NULL );
1523 assert( access > ACL_NONE );
1525 op->o_bd = select_backend( edn, 0, 0 );
1527 if ( target && dn_match( &target->e_nname, edn ) ) {
1531 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1535 Attribute *a = NULL;
1538 if ( entry_at == NULL ) {
1539 entry_at = slap_schema.si_ad_entry;
1542 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1544 if ( access_allowed_mask( op, e, entry_at,
1545 NULL, access, NULL, mask ) == 0 )
1547 rc = LDAP_INSUFFICIENT_ACCESS;
1554 a = attr_find( e->e_attrs, entry_at );
1556 SlapReply rs = { 0 };
1557 AttributeName anlist[ 2 ];
1559 anlist[ 0 ].an_name = entry_at->ad_cname;
1560 anlist[ 0 ].an_desc = entry_at;
1561 BER_BVZERO( &anlist[ 1 ].an_name );
1562 rs.sr_attrs = anlist;
1564 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1566 /* NOTE: backend_operational() is also called
1567 * when returning results, so it's supposed
1568 * to do no harm to entries */
1570 rc = backend_operational( op, &rs );
1573 if ( rc == LDAP_SUCCESS ) {
1574 if ( rs.sr_operational_attrs ) {
1576 a = rs.sr_operational_attrs;
1579 rc = LDAP_NO_SUCH_OBJECT;
1585 if ( access_allowed_mask( op, e, entry_at,
1586 nval, access, NULL, mask ) == 0 )
1588 rc = LDAP_INSUFFICIENT_ACCESS;
1594 freeit: if ( e != target ) {
1595 be_entry_release_r( op, e );
1615 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1616 /* just count them */ ;
1619 * If operational attributes (allegedly) are required,
1620 * and the backend supports specific operational attributes,
1621 * add them to the attribute list
1623 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1624 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1626 *ap = slap_operational_entryDN( rs->sr_entry );
1627 ap = &(*ap)->a_next;
1630 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1631 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1633 *ap = slap_operational_subschemaSubentry( op->o_bd );
1634 ap = &(*ap)->a_next;
1637 if ( op->o_bd != NULL && op->o_bd != frontendDB )
1639 /* Let the overlays have a chance at this */
1641 op->o_bd = select_backend( &op->o_ndn, 0, 0 );
1643 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1644 op->o_bd != NULL && op->o_bd->be_operational != NULL )
1646 rc = op->o_bd->be_operational( op, rs );
1654 int backend_operational( Operation *op, SlapReply *rs )
1659 /* Moved this into the frontend so global overlays are called */
1662 op->o_bd = frontendDB;
1663 rc = frontendDB->be_operational( op, rs );