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-2007 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",
251 /* append global access controls */
252 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
253 return backend_startup_one( be );
256 /* open frontend, if required */
257 if ( frontendDB->bd_info->bi_db_open ) {
258 rc = frontendDB->bd_info->bi_db_open( frontendDB );
260 Debug( LDAP_DEBUG_ANY,
261 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
267 /* open each backend type */
269 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
271 if( bi->bi_nDB == 0) {
272 /* no database of this type, don't open */
277 rc = bi->bi_open( bi );
279 Debug( LDAP_DEBUG_ANY,
280 "backend_startup: bi_open %d (%s) failed!\n",
286 (void)backend_init_controls( bi );
289 /* open each backend database */
291 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
293 if ( be->be_suffix == NULL ) {
294 Debug( LDAP_DEBUG_ANY,
295 "backend_startup: warning, database %d (%s) "
297 i, be->bd_info->bi_type, 0 );
299 /* append global access controls */
300 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
302 rc = backend_startup_one( be );
310 int backend_num( Backend *be )
315 if( be == NULL ) return -1;
317 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
318 if( be == b2 ) return i;
324 int backend_shutdown( Backend *be )
330 /* shutdown a specific backend database */
332 if ( be->bd_info->bi_nDB == 0 ) {
333 /* no database of this type, we never opened it */
337 if ( be->bd_info->bi_db_close ) {
338 be->bd_info->bi_db_close( be );
341 if( be->bd_info->bi_close ) {
342 be->bd_info->bi_close( be->bd_info );
348 /* close each backend database */
349 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
350 if ( be->bd_info->bi_db_close ) {
351 be->bd_info->bi_db_close( be );
355 Debug( LDAP_DEBUG_ANY,
356 "backend_close: bi_db_close %s failed!\n",
361 /* close each backend type */
362 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
363 if( bi->bi_nDB == 0 ) {
364 /* no database of this type */
373 /* close frontend, if required */
374 if ( frontendDB->bd_info->bi_db_close ) {
375 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
377 Debug( LDAP_DEBUG_ANY,
378 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
386 void backend_destroy_one( BackendDB *bd, int dynamic )
389 LDAP_STAILQ_REMOVE(&backendDB, bd, slap_backend_db, be_next );
392 if ( bd->be_syncinfo ) {
393 syncinfo_free( bd->be_syncinfo );
396 if ( bd->be_pending_csn_list ) {
397 struct slap_csn_entry *csne;
398 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
400 struct slap_csn_entry *tmp_csne = csne;
402 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
403 ch_free( csne->ce_csn.bv_val );
404 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
407 ch_free( bd->be_pending_csn_list );
410 if ( bd->bd_info->bi_db_destroy ) {
411 bd->bd_info->bi_db_destroy( bd );
413 ber_bvarray_free( bd->be_suffix );
414 ber_bvarray_free( bd->be_nsuffix );
415 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
416 free( bd->be_rootdn.bv_val );
418 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
419 free( bd->be_rootndn.bv_val );
421 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
422 free( bd->be_rootpw.bv_val );
424 acl_destroy( bd->be_acl, frontendDB->be_acl );
425 limits_destroy( bd->be_limits );
426 if ( bd->be_replogfile ) {
427 ch_free( bd->be_replogfile );
429 destroy_replica_info( bd );
430 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
431 ch_free( bd->be_update_ndn.bv_val );
433 if ( bd->be_update_refs ) {
434 ber_bvarray_free( bd->be_update_refs );
442 int backend_destroy(void)
447 /* destroy each backend database */
448 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
449 backend_destroy_one( bd, 1 );
452 /* destroy each backend type */
453 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
454 if( bi->bi_destroy ) {
455 bi->bi_destroy( bi );
460 LDAP_STAILQ_INIT(&backendInfo);
462 /* destroy frontend database */
465 if ( bd->bd_info->bi_db_destroy ) {
466 bd->bd_info->bi_db_destroy( bd );
468 ber_bvarray_free( bd->be_suffix );
469 ber_bvarray_free( bd->be_nsuffix );
470 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
471 free( bd->be_rootdn.bv_val );
473 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
474 free( bd->be_rootndn.bv_val );
476 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
477 free( bd->be_rootpw.bv_val );
479 acl_destroy( bd->be_acl, frontendDB->be_acl );
481 if ( bd->be_replogfile != NULL ) {
482 free( bd->be_replogfile );
484 assert( bd->be_replica == NULL );
490 BackendInfo* backend_info(const char *type)
494 /* search for the backend type */
495 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
496 if( strcasecmp(bi->bi_type, type) == 0 ) {
510 BackendInfo *bi = backend_info(type);
514 fprintf( stderr, "Unrecognized database type (%s)\n", type );
518 /* If be is provided, treat it as private. Otherwise allocate
519 * one and add it to the global list.
522 be = ch_calloc( 1, sizeof(Backend) );
524 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
529 be->be_def_limit = frontendDB->be_def_limit;
530 be->be_dfltaccess = frontendDB->be_dfltaccess;
532 be->be_restrictops = frontendDB->be_restrictops;
533 be->be_requires = frontendDB->be_requires;
534 be->be_ssf_set = frontendDB->be_ssf_set;
536 be->be_pcl_mutexp = &be->be_pcl_mutex;
537 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
539 /* assign a default depth limit for alias deref */
540 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
542 if ( bi->bi_db_init ) {
543 rc = bi->bi_db_init( be );
547 fprintf( stderr, "database init failed (%s)\n", type );
561 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
562 if ( be->bd_info->bi_db_close ) {
563 be->bd_info->bi_db_close( be );
567 if ( frontendDB->bd_info->bi_db_close ) {
568 (*frontendDB->bd_info->bi_db_close)( frontendDB );
576 int manageDSAit, /* unused since ITS#4986 */
580 ber_len_t len, dnlen = dn->bv_len;
583 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
584 if ( be->be_nsuffix == NULL ) {
588 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
590 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
595 len = be->be_nsuffix[j].bv_len;
598 /* suffix is longer than DN */
603 * input DN is normalized, so the separator check
604 * need not look at escaping
606 if ( len && len < dnlen &&
607 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
612 if ( strcmp( be->be_nsuffix[j].bv_val,
613 &dn->bv_val[dnlen-len] ) == 0 )
626 struct berval *bvsuffix )
630 if ( be->be_nsuffix == NULL ) {
634 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
635 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
644 be_isroot_dn( Backend *be, struct berval *ndn )
646 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
650 return dn_match( &be->be_rootndn, ndn );
654 be_slurp_update( Operation *op )
656 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
657 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
661 be_shadow_update( Operation *op )
663 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
664 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
668 be_isupdate_dn( Backend *be, struct berval *ndn )
670 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
674 return dn_match( &be->be_update_ndn, ndn );
678 be_root_dn( Backend *be )
680 return &be->be_rootdn;
684 be_isroot( Operation *op )
686 return be_isroot_dn( op->o_bd, &op->o_ndn );
690 be_isroot_pw( Operation *op )
694 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
698 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
703 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
704 op->o_conn->c_sasl_authctx, NULL );
707 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
710 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
722 if ( op->o_bd->be_release ) {
723 /* free and release entry from backend */
724 return op->o_bd->be_release( op, e, rw );
733 backend_unbind( Operation *op, SlapReply *rs )
737 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
738 if ( be->be_unbind ) {
740 be->be_unbind( op, rs );
748 backend_connection_init(
753 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
754 if ( be->be_connection_init ) {
755 be->be_connection_init( be, conn );
763 backend_connection_destroy(
768 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
769 if ( be->be_connection_destroy ) {
770 be->be_connection_destroy( be, conn);
778 backend_check_controls(
782 LDAPControl **ctrls = op->o_ctrls;
783 rs->sr_err = LDAP_SUCCESS;
786 for( ; *ctrls != NULL ; ctrls++ ) {
789 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
790 case LDAP_CONTROL_NOT_FOUND:
791 /* unrecognized control */
792 if ( (*ctrls)->ldctl_iscritical ) {
793 /* should not be reachable */
794 Debug( LDAP_DEBUG_ANY,
795 "backend_check_controls: unrecognized control: %s\n",
796 (*ctrls)->ldctl_oid, 0, 0 );
801 case LDAP_COMPARE_FALSE:
802 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
803 /* Per RFC 2251 (and LDAPBIS discussions), if the control
804 * is recognized and appropriate for the operation (which
805 * we've already verified), then the server should make
806 * use of the control when performing the operation.
808 * Here we find that operation extended by the control
809 * is unavailable in a particular context, and the control
810 * is marked Critical, hence the return of
811 * unwillingToPerform.
813 rs->sr_text = "critical control unavailable in context";
814 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
819 case LDAP_COMPARE_TRUE:
824 Debug( LDAP_DEBUG_ANY,
825 "backend_check_controls: unable to check control: %s\n",
826 (*ctrls)->ldctl_oid, 0, 0 );
829 rs->sr_text = "unable to check control";
830 rs->sr_err = LDAP_OTHER;
836 /* temporarily removed */
838 /* check should be generalized */
839 if( get_manageDIT(op) && !be_isroot(op)) {
840 rs->sr_text = "requires manager authorization";
841 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
850 backend_check_restrictions(
853 struct berval *opdata )
855 slap_mask_t restrictops;
856 slap_mask_t requires;
858 slap_mask_t exopflag = 0;
865 int rc = SLAP_CB_CONTINUE;
867 if ( op->o_bd->be_chk_controls ) {
868 rc = ( *op->o_bd->be_chk_controls )( op, rs );
871 if ( rc == SLAP_CB_CONTINUE ) {
872 rc = backend_check_controls( op, rs );
875 if ( rc != LDAP_SUCCESS ) {
879 restrictops = op->o_bd->be_restrictops;
880 requires = op->o_bd->be_requires;
881 ssf = &op->o_bd->be_ssf_set;
884 restrictops = frontendDB->be_restrictops;
885 requires = frontendDB->be_requires;
886 ssf = &frontendDB->be_ssf_set;
889 switch( op->o_tag ) {
891 opflag = SLAP_RESTRICT_OP_ADD;
895 opflag = SLAP_RESTRICT_OP_BIND;
898 case LDAP_REQ_COMPARE:
899 opflag = SLAP_RESTRICT_OP_COMPARE;
901 case LDAP_REQ_DELETE:
903 opflag = SLAP_RESTRICT_OP_DELETE;
905 case LDAP_REQ_EXTENDED:
906 opflag = SLAP_RESTRICT_OP_EXTENDED;
909 /* treat unspecified as a modify */
910 opflag = SLAP_RESTRICT_OP_MODIFY;
915 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
918 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
922 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
923 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
927 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
928 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
932 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
933 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
938 /* treat everything else as a modify */
939 opflag = SLAP_RESTRICT_OP_MODIFY;
943 case LDAP_REQ_MODIFY:
945 opflag = SLAP_RESTRICT_OP_MODIFY;
947 case LDAP_REQ_RENAME:
949 opflag = SLAP_RESTRICT_OP_RENAME;
951 case LDAP_REQ_SEARCH:
952 opflag = SLAP_RESTRICT_OP_SEARCH;
954 case LDAP_REQ_UNBIND:
959 rs->sr_text = "restrict operations internal error";
960 rs->sr_err = LDAP_OTHER;
965 /* these checks don't apply to StartTLS */
967 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
968 if( op->o_transport_ssf < ssf->sss_transport ) {
969 rs->sr_text = op->o_transport_ssf
970 ? "stronger transport confidentiality required"
971 : "transport confidentiality required";
975 if( op->o_tls_ssf < ssf->sss_tls ) {
976 rs->sr_text = op->o_tls_ssf
977 ? "stronger TLS confidentiality required"
978 : "TLS confidentiality required";
983 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
984 /* simple bind specific check */
985 if( op->o_ssf < ssf->sss_simple_bind ) {
986 rs->sr_text = op->o_ssf
987 ? "stronger confidentiality required"
988 : "confidentiality required";
993 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
994 /* these checks don't apply to SASL bind */
996 if( op->o_sasl_ssf < ssf->sss_sasl ) {
997 rs->sr_text = op->o_sasl_ssf
998 ? "stronger SASL confidentiality required"
999 : "SASL confidentiality required";
1003 if( op->o_ssf < ssf->sss_ssf ) {
1004 rs->sr_text = op->o_ssf
1005 ? "stronger confidentiality required"
1006 : "confidentiality required";
1012 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1013 rs->sr_text = op->o_transport_ssf
1014 ? "stronger transport confidentiality required for update"
1015 : "transport confidentiality required for update";
1019 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1020 rs->sr_text = op->o_tls_ssf
1021 ? "stronger TLS confidentiality required for update"
1022 : "TLS confidentiality required for update";
1026 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1027 rs->sr_text = op->o_sasl_ssf
1028 ? "stronger SASL confidentiality required for update"
1029 : "SASL confidentiality required for update";
1033 if( op->o_ssf < ssf->sss_update_ssf ) {
1034 rs->sr_text = op->o_ssf
1035 ? "stronger confidentiality required for update"
1036 : "confidentiality required for update";
1040 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1041 BER_BVISEMPTY( &op->o_ndn ) )
1043 rs->sr_text = "modifications require authentication";
1044 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1048 #ifdef SLAP_X_LISTENER_MOD
1049 if ( op->o_conn->c_listener &&
1050 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1051 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1053 /* no "w" mode means readonly */
1054 rs->sr_text = "modifications not allowed on this listener";
1055 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1058 #endif /* SLAP_X_LISTENER_MOD */
1063 /* these checks don't apply to Bind, StartTLS, or Unbind */
1065 if( requires & SLAP_REQUIRE_STRONG ) {
1066 /* should check mechanism */
1067 if( ( op->o_transport_ssf < ssf->sss_transport
1068 && op->o_authtype == LDAP_AUTH_SIMPLE )
1069 || BER_BVISEMPTY( &op->o_dn ) )
1071 rs->sr_text = "strong(er) authentication required";
1072 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1077 if( requires & SLAP_REQUIRE_SASL ) {
1078 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1079 rs->sr_text = "SASL authentication required";
1080 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1085 if( requires & SLAP_REQUIRE_AUTHC ) {
1086 if( BER_BVISEMPTY( &op->o_dn ) ) {
1087 rs->sr_text = "authentication required";
1088 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1093 if( requires & SLAP_REQUIRE_BIND ) {
1095 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1096 version = op->o_conn->c_protocol;
1097 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1100 /* no bind has occurred */
1101 rs->sr_text = "BIND required";
1102 rs->sr_err = LDAP_OPERATIONS_ERROR;
1107 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1108 if( op->o_protocol < LDAP_VERSION3 ) {
1109 /* no bind has occurred */
1110 rs->sr_text = "operation restricted to LDAPv3 clients";
1111 rs->sr_err = LDAP_OPERATIONS_ERROR;
1116 #ifdef SLAP_X_LISTENER_MOD
1117 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1118 if ( op->o_conn->c_listener &&
1119 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1121 /* no "x" mode means bind required */
1122 rs->sr_text = "bind required on this listener";
1123 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1128 if ( !starttls && !updateop ) {
1129 if ( op->o_conn->c_listener &&
1130 !( op->o_conn->c_listener->sl_perms &
1131 ( !BER_BVISEMPTY( &op->o_dn )
1132 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1134 /* no "r" mode means no read */
1135 rs->sr_text = "read not allowed on this listener";
1136 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1140 #endif /* SLAP_X_LISTENER_MOD */
1144 if( ( restrictops & opflag )
1145 || ( exopflag && ( restrictops & exopflag ) ) ) {
1146 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1147 rs->sr_text = "read operations restricted";
1148 } else if ( restrictops & exopflag ) {
1149 rs->sr_text = "extended operation restricted";
1151 rs->sr_text = "operation restricted";
1153 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1157 rs->sr_err = LDAP_SUCCESS;
1161 int backend_check_referrals( Operation *op, SlapReply *rs )
1163 rs->sr_err = LDAP_SUCCESS;
1165 if( op->o_bd->be_chk_referrals ) {
1166 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1168 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1169 send_ldap_result( op, rs );
1181 AttributeDescription *at,
1187 if ( op->o_bd == NULL ) {
1188 return LDAP_NO_SUCH_OBJECT;
1191 if ( op->o_bd->be_fetch ) {
1192 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1195 return LDAP_UNWILLING_TO_PERFORM;
1202 struct berval *gr_ndn,
1203 struct berval *op_ndn,
1204 ObjectClass *group_oc,
1205 AttributeDescription *group_at )
1208 void *o_priv = op->o_private, *e_priv = NULL;
1212 Backend *be = op->o_bd;
1214 op->o_bd = select_backend( gr_ndn, 0, 0 );
1216 for ( g = op->o_groups; g; g = g->ga_next ) {
1217 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1218 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1222 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1232 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1236 op->o_private = NULL;
1237 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1238 e_priv = op->o_private;
1239 op->o_private = o_priv;
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 void *user_priv = NULL;
1256 Backend *b2 = op->o_bd;
1258 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1261 op->o_bd = select_backend( op_ndn, 0, 0 );
1262 op->o_private = NULL;
1263 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1264 user_priv = op->o_private;
1265 op->o_private = o_priv;
1269 rc = LDAP_COMPARE_FALSE;
1270 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1271 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1276 BER_BVZERO( &nbase );
1277 /* host part must be empty */
1278 /* attrs and extensions parts must be empty */
1279 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1280 ludp->lud_attrs || ludp->lud_exts )
1284 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1285 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1286 op->o_tmpmemctx ) != LDAP_SUCCESS )
1290 switch ( ludp->lud_scope ) {
1291 case LDAP_SCOPE_BASE:
1292 if ( !dn_match( &nbase, op_ndn ) ) {
1296 case LDAP_SCOPE_ONELEVEL:
1297 dnParent( op_ndn, &bv );
1298 if ( !dn_match( &nbase, &bv ) ) {
1302 case LDAP_SCOPE_SUBTREE:
1303 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1307 case LDAP_SCOPE_SUBORDINATE:
1308 if ( dn_match( &nbase, op_ndn ) ||
1309 !dnIsSuffix( op_ndn, &nbase ) )
1314 filter = str2filter_x( op, ludp->lud_filter );
1316 if ( test_filter( NULL, user, filter ) ==
1321 filter_free_x( op, filter );
1324 ldap_free_urldesc( ludp );
1325 if ( !BER_BVISNULL( &nbase ) ) {
1326 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1328 if ( rc == 0 ) break;
1330 if ( user != target ) {
1331 op->o_private = user_priv;
1332 be_entry_release_r( op, user );
1333 op->o_private = o_priv;
1338 rc = value_find_ex( group_at,
1339 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1340 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1341 a->a_nvals, op_ndn, op->o_tmpmemctx );
1342 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1343 rc = LDAP_COMPARE_FALSE;
1346 rc = LDAP_NO_SUCH_ATTRIBUTE;
1348 if ( e != target ) {
1349 op->o_private = e_priv;
1350 be_entry_release_r( op, e );
1351 op->o_private = o_priv;
1354 rc = LDAP_NO_SUCH_OBJECT;
1357 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1358 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1360 g->ga_be = op->o_bd;
1361 g->ga_oc = group_oc;
1362 g->ga_at = group_at;
1364 g->ga_len = gr_ndn->bv_len;
1365 strcpy( g->ga_ndn, gr_ndn->bv_val );
1366 g->ga_next = op->o_groups;
1378 struct berval *gr_ndn,
1379 struct berval *op_ndn,
1380 ObjectClass *group_oc,
1381 AttributeDescription *group_at )
1386 if ( op->o_abandon ) {
1387 return SLAPD_ABANDON;
1391 op->o_bd = frontendDB;
1392 #ifdef SLAP_OVERLAY_ACCESS
1393 rc = frontendDB->be_group( op, target, gr_ndn,
1394 op_ndn, group_oc, group_at );
1395 #else /* ! SLAP_OVERLAY_ACCESS */
1396 rc = fe_acl_group( op, target, gr_ndn,
1397 op_ndn, group_oc, group_at );
1398 #endif /* ! SLAP_OVERLAY_ACCESS */
1409 AttributeDescription *entry_at,
1411 slap_access_t access )
1414 void *o_priv = op->o_private, *e_priv = NULL;
1415 Attribute *a = NULL;
1416 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1417 AccessControlState acl_state = ACL_STATE_INIT;
1418 Backend *be = op->o_bd;
1420 op->o_bd = select_backend( edn, 0, 0 );
1422 if ( target && dn_match( &target->e_nname, edn ) ) {
1426 op->o_private = NULL;
1427 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1428 e_priv = op->o_private;
1429 op->o_private = o_priv;
1433 a = attr_find( e->e_attrs, entry_at );
1435 SlapReply rs = { 0 };
1436 AttributeName anlist[ 2 ];
1438 anlist[ 0 ].an_name = entry_at->ad_cname;
1439 anlist[ 0 ].an_desc = entry_at;
1440 BER_BVZERO( &anlist[ 1 ].an_name );
1441 rs.sr_attrs = anlist;
1443 /* NOTE: backend_operational() is also called
1444 * when returning results, so it's supposed
1445 * to do no harm to entries */
1447 rc = backend_operational( op, &rs );
1450 if ( rc == LDAP_SUCCESS ) {
1451 if ( rs.sr_operational_attrs ) {
1453 a = rs.sr_operational_attrs;
1456 rc = LDAP_NO_SUCH_ATTRIBUTE;
1464 if ( op->o_conn && access > ACL_NONE &&
1465 access_allowed( op, e, entry_at, NULL,
1466 access, &acl_state ) == 0 )
1468 rc = LDAP_INSUFFICIENT_ACCESS;
1472 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1475 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1477 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1479 if ( op->o_conn && access > ACL_NONE &&
1480 access_allowed( op, e, entry_at,
1487 ber_dupbv_x( &v[j], &a->a_nvals[i],
1489 if ( !BER_BVISNULL( &v[j] ) ) {
1494 op->o_tmpfree( v, op->o_tmpmemctx );
1496 rc = LDAP_INSUFFICIENT_ACCESS;
1499 BER_BVZERO( &v[j] );
1504 freeit: if ( e != target ) {
1505 op->o_private = e_priv;
1506 be_entry_release_r( op, e );
1507 op->o_private = o_priv;
1523 AttributeDescription *entry_at,
1525 slap_access_t access )
1531 op->o_bd = frontendDB;
1532 #ifdef SLAP_OVERLAY_ACCESS
1533 rc = frontendDB->be_attribute( op, target, edn,
1534 entry_at, vals, access );
1535 #else /* !SLAP_OVERLAY_ACCESS */
1536 rc = fe_acl_attribute( op, target, edn,
1537 entry_at, vals, access );
1538 #endif /* !SLAP_OVERLAY_ACCESS */
1549 AttributeDescription *entry_at,
1550 struct berval *nval,
1551 slap_access_t access,
1555 void *o_priv = op->o_private, *e_priv = NULL;
1556 int rc = LDAP_INSUFFICIENT_ACCESS;
1557 Backend *be = op->o_bd;
1560 assert( op != NULL );
1561 assert( op->o_conn != NULL );
1562 assert( edn != NULL );
1563 assert( access > ACL_NONE );
1565 op->o_bd = select_backend( edn, 0, 0 );
1567 if ( target && dn_match( &target->e_nname, edn ) ) {
1571 op->o_private = NULL;
1572 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1573 e_priv = op->o_private;
1574 op->o_private = o_priv;
1578 Attribute *a = NULL;
1581 if ( entry_at == NULL ) {
1582 entry_at = slap_schema.si_ad_entry;
1585 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1587 if ( access_allowed_mask( op, e, entry_at,
1588 NULL, access, NULL, mask ) == 0 )
1590 rc = LDAP_INSUFFICIENT_ACCESS;
1597 a = attr_find( e->e_attrs, entry_at );
1599 SlapReply rs = { 0 };
1600 AttributeName anlist[ 2 ];
1602 anlist[ 0 ].an_name = entry_at->ad_cname;
1603 anlist[ 0 ].an_desc = entry_at;
1604 BER_BVZERO( &anlist[ 1 ].an_name );
1605 rs.sr_attrs = anlist;
1607 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1609 /* NOTE: backend_operational() is also called
1610 * when returning results, so it's supposed
1611 * to do no harm to entries */
1613 rc = backend_operational( op, &rs );
1616 if ( rc == LDAP_SUCCESS ) {
1617 if ( rs.sr_operational_attrs ) {
1619 a = rs.sr_operational_attrs;
1622 rc = LDAP_NO_SUCH_OBJECT;
1628 if ( access_allowed_mask( op, e, entry_at,
1629 nval, access, NULL, mask ) == 0 )
1631 rc = LDAP_INSUFFICIENT_ACCESS;
1637 freeit: if ( e != target ) {
1638 op->o_private = e_priv;
1639 be_entry_release_r( op, e );
1640 op->o_private = o_priv;
1660 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1661 /* just count them */ ;
1664 * If operational attributes (allegedly) are required,
1665 * and the backend supports specific operational attributes,
1666 * add them to the attribute list
1668 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1669 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1670 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1672 *ap = slap_operational_entryDN( rs->sr_entry );
1673 ap = &(*ap)->a_next;
1676 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1677 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1678 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1680 *ap = slap_operational_subschemaSubentry( op->o_bd );
1681 ap = &(*ap)->a_next;
1684 if ( op->o_bd != NULL )
1686 /* Let the overlays have a chance at this */
1688 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
1689 if ( !be_match( op->o_bd, frontendDB ) &&
1690 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1691 op->o_bd != NULL && op->o_bd->be_operational != NULL )
1693 rc = op->o_bd->be_operational( op, rs );
1701 int backend_operational( Operation *op, SlapReply *rs )
1706 /* Moved this into the frontend so global overlays are called */
1709 op->o_bd = frontendDB;
1710 rc = frontendDB->be_operational( op, rs );