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 );
429 int backend_destroy(void)
434 /* destroy each backend database */
435 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
436 backend_destroy_one( bd, 1 );
439 /* destroy each backend type */
440 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
441 if( bi->bi_destroy ) {
442 bi->bi_destroy( bi );
447 LDAP_STAILQ_INIT(&backendInfo);
449 /* destroy frontend database */
452 if ( bd->bd_info->bi_db_destroy ) {
453 bd->bd_info->bi_db_destroy( bd );
455 ber_bvarray_free( bd->be_suffix );
456 ber_bvarray_free( bd->be_nsuffix );
457 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
458 free( bd->be_rootdn.bv_val );
460 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
461 free( bd->be_rootndn.bv_val );
463 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
464 free( bd->be_rootpw.bv_val );
466 acl_destroy( bd->be_acl, frontendDB->be_acl );
472 BackendInfo* backend_info(const char *type)
476 /* search for the backend type */
477 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
478 if( strcasecmp(bi->bi_type, type) == 0 ) {
492 BackendInfo *bi = backend_info(type);
496 fprintf( stderr, "Unrecognized database type (%s)\n", type );
500 be = ch_calloc( 1, sizeof(Backend) );
502 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
506 be->be_def_limit = frontendDB->be_def_limit;
507 be->be_dfltaccess = frontendDB->be_dfltaccess;
509 be->be_restrictops = frontendDB->be_restrictops;
510 be->be_requires = frontendDB->be_requires;
511 be->be_ssf_set = frontendDB->be_ssf_set;
513 be->be_pcl_mutexp = &be->be_pcl_mutex;
514 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
516 /* assign a default depth limit for alias deref */
517 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
519 if ( bi->bi_db_init ) {
520 rc = bi->bi_db_init( be );
524 fprintf( stderr, "database init failed (%s)\n", type );
538 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
539 if ( be->bd_info->bi_db_close ) {
540 be->bd_info->bi_db_close( be );
544 if ( frontendDB->bd_info->bi_db_close ) {
545 (*frontendDB->bd_info->bi_db_close)( frontendDB );
557 ber_len_t len, dnlen = dn->bv_len;
558 Backend *be, *b2 = NULL;
560 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
561 if ( be->be_nsuffix == NULL ) {
565 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
567 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
572 len = be->be_nsuffix[j].bv_len;
575 /* suffix is longer than DN */
580 * input DN is normalized, so the separator check
581 * need not look at escaping
583 if ( len && len < dnlen &&
584 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
589 if ( strcmp( be->be_nsuffix[j].bv_val,
590 &dn->bv_val[dnlen-len] ) == 0 )
595 if( manageDSAit && len == dnlen &&
596 !SLAP_GLUE_SUBORDINATE( be ) ) {
613 struct berval *bvsuffix )
617 if ( be->be_nsuffix == NULL ) {
621 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
622 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
631 be_isroot_dn( Backend *be, struct berval *ndn )
633 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
637 return dn_match( &be->be_rootndn, ndn );
641 be_slurp_update( Operation *op )
643 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
644 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
648 be_shadow_update( Operation *op )
650 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
651 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
655 be_isupdate_dn( Backend *be, struct berval *ndn )
657 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
661 return dn_match( &be->be_update_ndn, ndn );
665 be_root_dn( Backend *be )
667 return &be->be_rootdn;
671 be_isroot( Operation *op )
673 return be_isroot_dn( op->o_bd, &op->o_ndn );
677 be_isroot_pw( Operation *op )
681 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
685 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
690 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
691 op->o_conn->c_sasl_authctx, NULL );
694 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
697 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
709 if ( op->o_bd->be_release ) {
710 /* free and release entry from backend */
711 return op->o_bd->be_release( op, e, rw );
720 backend_unbind( Operation *op, SlapReply *rs )
724 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
725 if ( be->be_unbind ) {
727 be->be_unbind( op, rs );
735 backend_connection_init(
740 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
741 if ( be->be_connection_init ) {
742 be->be_connection_init( be, conn );
750 backend_connection_destroy(
755 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
756 if ( be->be_connection_destroy ) {
757 be->be_connection_destroy( be, conn);
765 backend_check_controls(
769 LDAPControl **ctrls = op->o_ctrls;
770 rs->sr_err = LDAP_SUCCESS;
773 for( ; *ctrls != NULL ; ctrls++ ) {
776 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
777 case LDAP_CONTROL_NOT_FOUND:
778 /* unrecognized control */
779 if ( (*ctrls)->ldctl_iscritical ) {
780 /* should not be reachable */
781 Debug( LDAP_DEBUG_ANY,
782 "backend_check_controls: unrecognized control: %s\n",
783 (*ctrls)->ldctl_oid, 0, 0 );
788 case LDAP_COMPARE_FALSE:
789 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
790 /* Per RFC 2251 (and LDAPBIS discussions), if the control
791 * is recognized and appropriate for the operation (which
792 * we've already verified), then the server should make
793 * use of the control when performing the operation.
795 * Here we find that operation extended by the control
796 * is unavailable in a particular context, and the control
797 * is marked Critical, hence the return of
798 * unwillingToPerform.
800 rs->sr_text = "critical control unavailable in context";
801 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
806 case LDAP_COMPARE_TRUE:
811 Debug( LDAP_DEBUG_ANY,
812 "backend_check_controls: unable to check control: %s\n",
813 (*ctrls)->ldctl_oid, 0, 0 );
816 rs->sr_text = "unable to check control";
817 rs->sr_err = LDAP_OTHER;
823 /* check should be generalized */
824 if( get_manageDIT(op) && !be_isroot(op)) {
825 rs->sr_text = "requires manager authorization";
826 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
834 backend_check_restrictions(
837 struct berval *opdata )
839 slap_mask_t restrictops;
840 slap_mask_t requires;
842 slap_mask_t exopflag = 0;
849 int rc = SLAP_CB_CONTINUE;
851 if ( op->o_bd->be_chk_controls ) {
852 rc = ( *op->o_bd->be_chk_controls )( op, rs );
855 if ( rc == SLAP_CB_CONTINUE ) {
856 rc = backend_check_controls( op, rs );
859 if ( rc != LDAP_SUCCESS ) {
863 restrictops = op->o_bd->be_restrictops;
864 requires = op->o_bd->be_requires;
865 ssf = &op->o_bd->be_ssf_set;
868 restrictops = frontendDB->be_restrictops;
869 requires = frontendDB->be_requires;
870 ssf = &frontendDB->be_ssf_set;
873 switch( op->o_tag ) {
875 opflag = SLAP_RESTRICT_OP_ADD;
879 opflag = SLAP_RESTRICT_OP_BIND;
882 case LDAP_REQ_COMPARE:
883 opflag = SLAP_RESTRICT_OP_COMPARE;
885 case LDAP_REQ_DELETE:
887 opflag = SLAP_RESTRICT_OP_DELETE;
889 case LDAP_REQ_EXTENDED:
890 opflag = SLAP_RESTRICT_OP_EXTENDED;
893 /* treat unspecified as a modify */
894 opflag = SLAP_RESTRICT_OP_MODIFY;
899 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
902 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
906 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
907 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
911 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
912 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
916 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
917 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
922 /* treat everything else as a modify */
923 opflag = SLAP_RESTRICT_OP_MODIFY;
927 case LDAP_REQ_MODIFY:
929 opflag = SLAP_RESTRICT_OP_MODIFY;
931 case LDAP_REQ_RENAME:
933 opflag = SLAP_RESTRICT_OP_RENAME;
935 case LDAP_REQ_SEARCH:
936 opflag = SLAP_RESTRICT_OP_SEARCH;
938 case LDAP_REQ_UNBIND:
943 rs->sr_text = "restrict operations internal error";
944 rs->sr_err = LDAP_OTHER;
949 /* these checks don't apply to StartTLS */
951 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
952 if( op->o_transport_ssf < ssf->sss_transport ) {
953 rs->sr_text = op->o_transport_ssf
954 ? "stronger transport confidentiality required"
955 : "transport confidentiality required";
959 if( op->o_tls_ssf < ssf->sss_tls ) {
960 rs->sr_text = op->o_tls_ssf
961 ? "stronger TLS confidentiality required"
962 : "TLS confidentiality required";
967 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
968 /* simple bind specific check */
969 if( op->o_ssf < ssf->sss_simple_bind ) {
970 rs->sr_text = op->o_ssf
971 ? "stronger confidentiality required"
972 : "confidentiality required";
977 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
978 /* these checks don't apply to SASL bind */
980 if( op->o_sasl_ssf < ssf->sss_sasl ) {
981 rs->sr_text = op->o_sasl_ssf
982 ? "stronger SASL confidentiality required"
983 : "SASL confidentiality required";
987 if( op->o_ssf < ssf->sss_ssf ) {
988 rs->sr_text = op->o_ssf
989 ? "stronger confidentiality required"
990 : "confidentiality required";
996 if( op->o_transport_ssf < ssf->sss_update_transport ) {
997 rs->sr_text = op->o_transport_ssf
998 ? "stronger transport confidentiality required for update"
999 : "transport confidentiality required for update";
1003 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1004 rs->sr_text = op->o_tls_ssf
1005 ? "stronger TLS confidentiality required for update"
1006 : "TLS confidentiality required for update";
1010 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1011 rs->sr_text = op->o_sasl_ssf
1012 ? "stronger SASL confidentiality required for update"
1013 : "SASL confidentiality required for update";
1017 if( op->o_ssf < ssf->sss_update_ssf ) {
1018 rs->sr_text = op->o_ssf
1019 ? "stronger confidentiality required for update"
1020 : "confidentiality required for update";
1024 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1025 BER_BVISEMPTY( &op->o_ndn ) )
1027 rs->sr_text = "modifications require authentication";
1028 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1032 #ifdef SLAP_X_LISTENER_MOD
1033 if ( op->o_conn->c_listener &&
1034 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1035 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1037 /* no "w" mode means readonly */
1038 rs->sr_text = "modifications not allowed on this listener";
1039 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1042 #endif /* SLAP_X_LISTENER_MOD */
1047 /* these checks don't apply to Bind, StartTLS, or Unbind */
1049 if( requires & SLAP_REQUIRE_STRONG ) {
1050 /* should check mechanism */
1051 if( ( op->o_transport_ssf < ssf->sss_transport
1052 && op->o_authtype == LDAP_AUTH_SIMPLE )
1053 || BER_BVISEMPTY( &op->o_dn ) )
1055 rs->sr_text = "strong(er) authentication required";
1056 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1061 if( requires & SLAP_REQUIRE_SASL ) {
1062 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1063 rs->sr_text = "SASL authentication required";
1064 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1069 if( requires & SLAP_REQUIRE_AUTHC ) {
1070 if( BER_BVISEMPTY( &op->o_dn ) ) {
1071 rs->sr_text = "authentication required";
1072 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1077 if( requires & SLAP_REQUIRE_BIND ) {
1079 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1080 version = op->o_conn->c_protocol;
1081 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1084 /* no bind has occurred */
1085 rs->sr_text = "BIND required";
1086 rs->sr_err = LDAP_OPERATIONS_ERROR;
1091 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1092 if( op->o_protocol < LDAP_VERSION3 ) {
1093 /* no bind has occurred */
1094 rs->sr_text = "operation restricted to LDAPv3 clients";
1095 rs->sr_err = LDAP_OPERATIONS_ERROR;
1100 #ifdef SLAP_X_LISTENER_MOD
1101 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1102 if ( op->o_conn->c_listener &&
1103 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1105 /* no "x" mode means bind required */
1106 rs->sr_text = "bind required on this listener";
1107 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1112 if ( !starttls && !updateop ) {
1113 if ( op->o_conn->c_listener &&
1114 !( op->o_conn->c_listener->sl_perms &
1115 ( !BER_BVISEMPTY( &op->o_dn )
1116 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1118 /* no "r" mode means no read */
1119 rs->sr_text = "read not allowed on this listener";
1120 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1124 #endif /* SLAP_X_LISTENER_MOD */
1128 if( ( restrictops & opflag )
1129 || ( exopflag && ( restrictops & exopflag ) ) ) {
1130 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1131 rs->sr_text = "read operations restricted";
1132 } else if ( restrictops & exopflag ) {
1133 rs->sr_text = "extended operation restricted";
1135 rs->sr_text = "operation restricted";
1137 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1141 rs->sr_err = LDAP_SUCCESS;
1145 int backend_check_referrals( Operation *op, SlapReply *rs )
1147 rs->sr_err = LDAP_SUCCESS;
1149 if( op->o_bd->be_chk_referrals ) {
1150 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1152 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1153 send_ldap_result( op, rs );
1165 AttributeDescription *at,
1173 if (op->o_bd == NULL) {
1174 rc = LDAP_NO_SUCH_OBJECT;
1175 } else if ( op->o_bd->be_fetch ) {
1176 rc = ( op->o_bd->be_fetch )( op, ndn,
1179 rc = LDAP_UNWILLING_TO_PERFORM;
1188 struct berval *gr_ndn,
1189 struct berval *op_ndn,
1190 ObjectClass *group_oc,
1191 AttributeDescription *group_at )
1197 Backend *be = op->o_bd;
1199 if ( op->o_abandon ) return SLAPD_ABANDON;
1201 op->o_bd = select_backend( gr_ndn, 0, 0 );
1203 for ( g = op->o_groups; g; g = g->ga_next ) {
1204 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1205 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1209 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1219 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1223 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1226 a = attr_find( e->e_attrs, group_at );
1228 /* If the attribute is a subtype of labeledURI, treat this as
1229 * a dynamic group ala groupOfURLs
1231 if (is_at_subtype( group_at->ad_type,
1232 slap_schema.si_ad_labeledURI->ad_type ) )
1236 struct berval bv, nbase;
1239 Backend *b2 = op->o_bd;
1241 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1244 op->o_bd = select_backend( op_ndn, 0, 0 );
1245 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1249 rc = LDAP_COMPARE_FALSE;
1250 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1251 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1256 BER_BVZERO( &nbase );
1257 /* host part must be empty */
1258 /* attrs and extensions parts must be empty */
1259 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1260 ludp->lud_attrs || ludp->lud_exts )
1264 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1265 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1266 op->o_tmpmemctx ) != LDAP_SUCCESS )
1270 switch ( ludp->lud_scope ) {
1271 case LDAP_SCOPE_BASE:
1272 if ( !dn_match( &nbase, op_ndn ) ) {
1276 case LDAP_SCOPE_ONELEVEL:
1277 dnParent( op_ndn, &bv );
1278 if ( !dn_match( &nbase, &bv ) ) {
1282 case LDAP_SCOPE_SUBTREE:
1283 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1287 #ifdef LDAP_SCOPE_SUBORDINATE
1288 case LDAP_SCOPE_SUBORDINATE:
1289 if ( dn_match( &nbase, op_ndn ) ||
1290 !dnIsSuffix( op_ndn, &nbase ) )
1296 filter = str2filter_x( op, ludp->lud_filter );
1298 if ( test_filter( NULL, user, filter ) ==
1303 filter_free_x( op, filter );
1306 ldap_free_urldesc( ludp );
1307 if ( !BER_BVISNULL( &nbase ) ) {
1308 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1310 if ( rc == 0 ) break;
1312 if ( user != target ) {
1313 be_entry_release_r( op, user );
1318 rc = value_find_ex( group_at,
1319 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1320 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1321 a->a_nvals, op_ndn, op->o_tmpmemctx );
1322 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1323 rc = LDAP_COMPARE_FALSE;
1326 rc = LDAP_NO_SUCH_ATTRIBUTE;
1329 be_entry_release_r( op, e );
1332 rc = LDAP_NO_SUCH_OBJECT;
1335 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1336 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1338 g->ga_be = op->o_bd;
1339 g->ga_oc = group_oc;
1340 g->ga_at = group_at;
1342 g->ga_len = gr_ndn->bv_len;
1343 strcpy( g->ga_ndn, gr_ndn->bv_val );
1344 g->ga_next = op->o_groups;
1357 AttributeDescription *entry_at,
1359 slap_access_t access )
1362 Attribute *a = NULL;
1363 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1364 AccessControlState acl_state = ACL_STATE_INIT;
1365 Backend *be = op->o_bd;
1367 op->o_bd = select_backend( edn, 0, 0 );
1369 if ( target && dn_match( &target->e_nname, edn ) ) {
1373 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1377 a = attr_find( e->e_attrs, entry_at );
1379 SlapReply rs = { 0 };
1380 AttributeName anlist[ 2 ];
1382 anlist[ 0 ].an_name = entry_at->ad_cname;
1383 anlist[ 0 ].an_desc = entry_at;
1384 BER_BVZERO( &anlist[ 1 ].an_name );
1385 rs.sr_attrs = anlist;
1387 /* NOTE: backend_operational() is also called
1388 * when returning results, so it's supposed
1389 * to do no harm to entries */
1391 rc = backend_operational( op, &rs );
1394 if ( rc == LDAP_SUCCESS ) {
1395 if ( rs.sr_operational_attrs ) {
1397 a = rs.sr_operational_attrs;
1400 rc = LDAP_NO_SUCH_ATTRIBUTE;
1408 if ( op->o_conn && access > ACL_NONE &&
1409 access_allowed( op, e, entry_at, NULL,
1410 access, &acl_state ) == 0 )
1412 rc = LDAP_INSUFFICIENT_ACCESS;
1416 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1419 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1421 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1423 if ( op->o_conn && access > ACL_NONE &&
1424 access_allowed( op, e, entry_at,
1431 ber_dupbv_x( &v[j], &a->a_nvals[i],
1433 if ( !BER_BVISNULL( &v[j] ) ) {
1438 op->o_tmpfree( v, op->o_tmpmemctx );
1440 rc = LDAP_INSUFFICIENT_ACCESS;
1443 BER_BVZERO( &v[j] );
1448 freeit: if ( e != target ) {
1449 be_entry_release_r( op, e );
1465 AttributeDescription *entry_at,
1466 struct berval *nval,
1467 slap_access_t access,
1471 int rc = LDAP_INSUFFICIENT_ACCESS;
1472 Backend *be = op->o_bd;
1475 assert( op != NULL );
1476 assert( op->o_conn != NULL );
1477 assert( edn != NULL );
1478 assert( access > ACL_NONE );
1480 op->o_bd = select_backend( edn, 0, 0 );
1482 if ( target && dn_match( &target->e_nname, edn ) ) {
1486 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1490 Attribute *a = NULL;
1493 if ( entry_at == NULL ) {
1494 entry_at = slap_schema.si_ad_entry;
1497 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1499 if ( access_allowed_mask( op, e, entry_at,
1500 NULL, access, NULL, mask ) == 0 )
1502 rc = LDAP_INSUFFICIENT_ACCESS;
1509 a = attr_find( e->e_attrs, entry_at );
1511 SlapReply rs = { 0 };
1512 AttributeName anlist[ 2 ];
1514 anlist[ 0 ].an_name = entry_at->ad_cname;
1515 anlist[ 0 ].an_desc = entry_at;
1516 BER_BVZERO( &anlist[ 1 ].an_name );
1517 rs.sr_attrs = anlist;
1519 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1521 /* NOTE: backend_operational() is also called
1522 * when returning results, so it's supposed
1523 * to do no harm to entries */
1525 rc = backend_operational( op, &rs );
1528 if ( rc == LDAP_SUCCESS ) {
1529 if ( rs.sr_operational_attrs ) {
1531 a = rs.sr_operational_attrs;
1534 rc = LDAP_NO_SUCH_OBJECT;
1540 if ( access_allowed_mask( op, e, entry_at,
1541 nval, access, NULL, mask ) == 0 )
1543 rc = LDAP_INSUFFICIENT_ACCESS;
1549 freeit: if ( e != target ) {
1550 be_entry_release_r( op, e );
1561 int backend_operational(
1569 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1570 /* just count them */ ;
1573 * If operational attributes (allegedly) are required,
1574 * and the backend supports specific operational attributes,
1575 * add them to the attribute list
1577 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1578 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1580 *ap = slap_operational_entryDN( rs->sr_entry );
1581 ap = &(*ap)->a_next;
1584 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1585 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1587 *ap = slap_operational_subschemaSubentry( op->o_bd );
1588 ap = &(*ap)->a_next;
1591 if ( op->o_bd != NULL )
1593 /* Let the overlays have a chance at this */
1595 if ( SLAP_ISOVERLAY( be_orig ) )
1596 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1598 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1599 op->o_bd && op->o_bd->be_operational != NULL )
1601 rc = op->o_bd->be_operational( op, rs );