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 #include "slapi/slapi.h"
43 static void init_group_pblock( Operation *op, Entry *target,
44 Entry *e, struct berval *op_ndn, AttributeDescription *group_at );
45 static int call_group_preop_plugins( Operation *op );
46 static void call_group_postop_plugins( Operation *op );
47 #endif /* LDAP_SLAPI */
50 * If a module is configured as dynamic, its header should not
51 * get included into slapd. While this is a general rule and does
52 * not have much of an effect in UNIX, this rule should be adhered
53 * to for Windows, where dynamic object code should not be implicitly
54 * imported into slapd without appropriate __declspec(dllimport) directives.
58 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
61 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
64 backend_init_controls( BackendInfo *bi )
66 if ( bi->bi_controls ) {
69 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
72 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
73 == LDAP_CONTROL_NOT_FOUND )
75 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
82 bi->bi_ctrls[ cid ] = 1;
89 int backend_init(void)
94 if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
95 /* already initialized */
96 Debug( LDAP_DEBUG_ANY,
97 "backend_init: already initialized\n", 0, 0, 0 );
101 for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ ) {
102 assert( bi->bi_init );
104 rc = bi->bi_init( bi );
107 Debug( LDAP_DEBUG_ANY,
108 "backend_init: initialized for type \"%s\"\n",
110 /* destroy those we've already inited */
115 if ( slap_binfo[nBackendInfo].bi_destroy ) {
116 slap_binfo[nBackendInfo].bi_destroy(
117 &slap_binfo[nBackendInfo] );
123 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
126 if ( nBackendInfo > 0) {
134 Debug( LDAP_DEBUG_ANY,
135 "backend_init: failed\n",
139 #endif /* SLAPD_MODULES */
142 int backend_add(BackendInfo *aBackendInfo)
146 if ( aBackendInfo->bi_init == NULL ) {
147 Debug( LDAP_DEBUG_ANY, "backend_add: "
148 "backend type \"%s\" does not have the (mandatory)init function\n",
149 aBackendInfo->bi_type, 0, 0 );
153 rc = aBackendInfo->bi_init(aBackendInfo);
155 Debug( LDAP_DEBUG_ANY,
156 "backend_add: initialization for type \"%s\" failed\n",
157 aBackendInfo->bi_type, 0, 0 );
161 (void)backend_init_controls( aBackendInfo );
163 /* now add the backend type to the Backend Info List */
164 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
170 backend_set_controls( BackendDB *be )
172 BackendInfo *bi = be->bd_info;
174 /* back-relay takes care of itself; so may do other */
175 if ( overlay_is_over( be ) ) {
176 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
179 if ( bi->bi_controls ) {
180 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
181 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
182 sizeof( be->be_ctrls ) );
183 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
188 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
189 if ( bi->bi_ctrls[ i ] ) {
190 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
200 /* startup a specific backend database */
201 int backend_startup_one(Backend *be)
207 be->be_pending_csn_list = (struct be_pcl *)
208 ch_calloc( 1, sizeof( struct be_pcl ));
210 LDAP_TAILQ_INIT( be->be_pending_csn_list );
212 Debug( LDAP_DEBUG_TRACE,
213 "backend_startup_one: starting \"%s\"\n",
214 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
217 /* set database controls */
218 (void)backend_set_controls( be );
220 if ( be->bd_info->bi_db_open ) {
221 rc = be->bd_info->bi_db_open( be );
223 (void)backend_set_controls( be );
226 Debug( LDAP_DEBUG_ANY,
227 "backend_startup_one: bi_db_open failed! (%d)\n",
235 int backend_startup(Backend *be)
241 if( ! ( nBackendDB > 0 ) ) {
243 Debug( LDAP_DEBUG_ANY,
244 "backend_startup: %d databases to startup.\n",
250 if ( be->bd_info->bi_open ) {
251 rc = be->bd_info->bi_open( be->bd_info );
253 Debug( LDAP_DEBUG_ANY,
254 "backend_startup: bi_open failed!\n",
261 return backend_startup_one( be );
264 /* open frontend, if required */
265 if ( frontendDB->bd_info->bi_db_open ) {
266 rc = frontendDB->bd_info->bi_db_open( frontendDB );
268 Debug( LDAP_DEBUG_ANY,
269 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
275 /* open each backend type */
277 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
279 if( bi->bi_nDB == 0) {
280 /* no database of this type, don't open */
285 rc = bi->bi_open( bi );
287 Debug( LDAP_DEBUG_ANY,
288 "backend_startup: bi_open %d (%s) failed!\n",
294 (void)backend_init_controls( bi );
297 /* open each backend database */
299 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
301 if ( be->be_suffix == NULL ) {
302 Debug( LDAP_DEBUG_ANY,
303 "backend_startup: warning, database %d (%s) "
305 i, be->bd_info->bi_type, 0 );
307 /* append global access controls */
308 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
310 rc = backend_startup_one( be );
318 int backend_num( Backend *be )
323 if( be == NULL ) return -1;
325 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
326 if( be == b2 ) return i;
332 int backend_shutdown( Backend *be )
339 /* shutdown a specific backend database */
341 if ( be->bd_info->bi_nDB == 0 ) {
342 /* no database of this type, we never opened it */
346 if ( be->bd_info->bi_db_close ) {
347 be->bd_info->bi_db_close( be );
350 if( be->bd_info->bi_close ) {
351 be->bd_info->bi_close( be->bd_info );
357 /* close each backend database */
358 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
359 if ( be->bd_info->bi_db_close ) {
360 be->bd_info->bi_db_close( be );
364 Debug( LDAP_DEBUG_ANY,
365 "backend_close: bi_db_close %s failed!\n",
370 /* close each backend type */
371 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
372 if( bi->bi_nDB == 0 ) {
373 /* no database of this type */
382 /* close frontend, if required */
383 if ( frontendDB->bd_info->bi_db_close ) {
384 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
386 Debug( LDAP_DEBUG_ANY,
387 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
395 void backend_destroy_one( BackendDB *bd )
397 LDAP_STAILQ_REMOVE(&backendDB, bd, slap_backend_db, be_next );
399 if ( bd->be_syncinfo ) {
400 syncinfo_free( bd->be_syncinfo );
403 if ( bd->be_pending_csn_list ) {
404 struct slap_csn_entry *csne;
405 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
407 struct slap_csn_entry *tmp_csne = csne;
409 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
410 ch_free( csne->ce_csn.bv_val );
411 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
416 if ( bd->bd_info->bi_db_destroy ) {
417 bd->bd_info->bi_db_destroy( bd );
419 ber_bvarray_free( bd->be_suffix );
420 ber_bvarray_free( bd->be_nsuffix );
421 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
422 free( bd->be_rootdn.bv_val );
424 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
425 free( bd->be_rootndn.bv_val );
427 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
428 free( bd->be_rootpw.bv_val );
430 acl_destroy( bd->be_acl, frontendDB->be_acl );
434 int backend_destroy(void)
440 /* destroy each backend database */
441 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
442 backend_destroy_one( bd );
445 /* destroy each backend type */
446 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
447 if( bi->bi_destroy ) {
448 bi->bi_destroy( bi );
453 LDAP_STAILQ_INIT(&backendInfo);
455 /* destroy frontend database */
458 if ( bd->bd_info->bi_db_destroy ) {
459 bd->bd_info->bi_db_destroy( bd );
461 ber_bvarray_free( bd->be_suffix );
462 ber_bvarray_free( bd->be_nsuffix );
463 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
464 free( bd->be_rootdn.bv_val );
466 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
467 free( bd->be_rootndn.bv_val );
469 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
470 free( bd->be_rootpw.bv_val );
472 acl_destroy( bd->be_acl, frontendDB->be_acl );
478 BackendInfo* backend_info(const char *type)
482 /* search for the backend type */
483 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
484 if( strcasecmp(bi->bi_type, type) == 0 ) {
498 BackendInfo *bi = backend_info(type);
502 fprintf( stderr, "Unrecognized database type (%s)\n", type );
506 be = ch_calloc( 1, sizeof(Backend) );
508 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
512 be->be_def_limit = frontendDB->be_def_limit;
513 be->be_dfltaccess = frontendDB->be_dfltaccess;
515 be->be_restrictops = frontendDB->be_restrictops;
516 be->be_requires = frontendDB->be_requires;
517 be->be_ssf_set = frontendDB->be_ssf_set;
519 be->be_pcl_mutexp = &be->be_pcl_mutex;
520 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
522 /* assign a default depth limit for alias deref */
523 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
525 if ( bi->bi_db_init ) {
526 rc = bi->bi_db_init( be );
530 fprintf( stderr, "database init failed (%s)\n", type );
545 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
546 if ( be->bd_info->bi_db_close ) {
547 be->bd_info->bi_db_close( be );
551 if ( frontendDB->bd_info->bi_db_close ) {
552 (*frontendDB->bd_info->bi_db_close)( frontendDB );
564 ber_len_t len, dnlen = dn->bv_len;
565 Backend *be, *b2 = NULL;
567 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
568 if ( be->be_nsuffix == NULL ) {
572 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
574 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
579 len = be->be_nsuffix[j].bv_len;
582 /* suffix is longer than DN */
587 * input DN is normalized, so the separator check
588 * need not look at escaping
590 if ( len && len < dnlen &&
591 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
596 if ( strcmp( be->be_nsuffix[j].bv_val,
597 &dn->bv_val[dnlen-len] ) == 0 )
602 if( manageDSAit && len == dnlen &&
603 !SLAP_GLUE_SUBORDINATE( be ) ) {
620 struct berval *bvsuffix )
624 if ( be->be_nsuffix == NULL ) {
628 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
629 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
638 be_isroot_dn( Backend *be, struct berval *ndn )
640 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
644 return dn_match( &be->be_rootndn, ndn );
648 be_slurp_update( Operation *op )
650 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
651 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
655 be_shadow_update( Operation *op )
657 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
658 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
662 be_isupdate_dn( Backend *be, struct berval *ndn )
664 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
668 return dn_match( &be->be_update_ndn, ndn );
672 be_root_dn( Backend *be )
674 return &be->be_rootdn;
678 be_isroot( Operation *op )
680 return be_isroot_dn( op->o_bd, &op->o_ndn );
684 be_isroot_pw( Operation *op )
688 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
692 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
697 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
698 op->o_conn->c_sasl_authctx, NULL );
701 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
704 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
716 if ( op->o_bd->be_release ) {
717 /* free and release entry from backend */
718 return op->o_bd->be_release( op, e, rw );
727 backend_unbind( Operation *op, SlapReply *rs )
732 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
733 #if defined( LDAP_SLAPI )
736 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
737 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)be );
738 rc = slapi_int_call_plugins( be,
739 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
742 * A preoperation plugin failure will abort the
745 Debug(LDAP_DEBUG_TRACE,
746 "do_bind: Unbind preoperation plugin failed\n",
751 #endif /* defined( LDAP_SLAPI ) */
753 if ( be->be_unbind ) {
755 be->be_unbind( op, rs );
758 #if defined( LDAP_SLAPI )
759 if ( op->o_pb != NULL && slapi_int_call_plugins( be,
760 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
762 Debug(LDAP_DEBUG_TRACE,
763 "do_unbind: Unbind postoperation plugins failed\n",
766 #endif /* defined( LDAP_SLAPI ) */
774 backend_connection_init(
779 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
780 if ( be->be_connection_init ) {
781 be->be_connection_init( be, conn );
789 backend_connection_destroy(
794 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
795 if ( be->be_connection_destroy ) {
796 be->be_connection_destroy( be, conn);
804 backend_check_controls(
808 LDAPControl **ctrls = op->o_ctrls;
809 rs->sr_err = LDAP_SUCCESS;
812 for( ; *ctrls != NULL ; ctrls++ ) {
815 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
816 case LDAP_CONTROL_NOT_FOUND:
817 /* unrecognized control */
818 if ( (*ctrls)->ldctl_iscritical ) {
819 /* should not be reachable */
820 Debug( LDAP_DEBUG_ANY,
821 "backend_check_controls: unrecognized control: %s\n",
822 (*ctrls)->ldctl_oid, 0, 0 );
827 case LDAP_COMPARE_FALSE:
828 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
829 /* Per RFC 2251 (and LDAPBIS discussions), if the control
830 * is recognized and appropriate for the operation (which
831 * we've already verified), then the server should make
832 * use of the control when performing the operation.
834 * Here we find that operation extended by the control
835 * is unavailable in a particular context, and the control
836 * is marked Critical, hence the return of
837 * unwillingToPerform.
839 rs->sr_text = "critical control unavailable in context";
840 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
845 case LDAP_COMPARE_TRUE:
850 Debug( LDAP_DEBUG_ANY,
851 "backend_check_controls: unable to check control: %s\n",
852 (*ctrls)->ldctl_oid, 0, 0 );
855 rs->sr_text = "unable to check control";
856 rs->sr_err = LDAP_OTHER;
862 /* check should be generalized */
863 if( get_manageDIT(op) && !be_isroot(op)) {
864 rs->sr_text = "requires manager authorization";
865 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
873 backend_check_restrictions(
876 struct berval *opdata )
878 slap_mask_t restrictops;
879 slap_mask_t requires;
881 slap_mask_t exopflag = 0;
888 int rc = SLAP_CB_CONTINUE;
890 if ( op->o_bd->be_chk_controls ) {
891 rc = ( *op->o_bd->be_chk_controls )( op, rs );
894 if ( rc == SLAP_CB_CONTINUE ) {
895 rc = backend_check_controls( op, rs );
898 if ( rc != LDAP_SUCCESS ) {
902 restrictops = op->o_bd->be_restrictops;
903 requires = op->o_bd->be_requires;
904 ssf = &op->o_bd->be_ssf_set;
907 restrictops = frontendDB->be_restrictops;
908 requires = frontendDB->be_requires;
909 ssf = &frontendDB->be_ssf_set;
912 switch( op->o_tag ) {
914 opflag = SLAP_RESTRICT_OP_ADD;
918 opflag = SLAP_RESTRICT_OP_BIND;
921 case LDAP_REQ_COMPARE:
922 opflag = SLAP_RESTRICT_OP_COMPARE;
924 case LDAP_REQ_DELETE:
926 opflag = SLAP_RESTRICT_OP_DELETE;
928 case LDAP_REQ_EXTENDED:
929 opflag = SLAP_RESTRICT_OP_EXTENDED;
932 /* treat unspecified as a modify */
933 opflag = SLAP_RESTRICT_OP_MODIFY;
938 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
941 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
945 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
946 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
950 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
951 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
955 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
956 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
961 /* treat everything else as a modify */
962 opflag = SLAP_RESTRICT_OP_MODIFY;
966 case LDAP_REQ_MODIFY:
968 opflag = SLAP_RESTRICT_OP_MODIFY;
970 case LDAP_REQ_RENAME:
972 opflag = SLAP_RESTRICT_OP_RENAME;
974 case LDAP_REQ_SEARCH:
975 opflag = SLAP_RESTRICT_OP_SEARCH;
977 case LDAP_REQ_UNBIND:
982 rs->sr_text = "restrict operations internal error";
983 rs->sr_err = LDAP_OTHER;
988 /* these checks don't apply to StartTLS */
990 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
991 if( op->o_transport_ssf < ssf->sss_transport ) {
992 rs->sr_text = op->o_transport_ssf
993 ? "stronger transport confidentiality required"
994 : "transport confidentiality required";
998 if( op->o_tls_ssf < ssf->sss_tls ) {
999 rs->sr_text = op->o_tls_ssf
1000 ? "stronger TLS confidentiality required"
1001 : "TLS confidentiality required";
1006 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1007 /* simple bind specific check */
1008 if( op->o_ssf < ssf->sss_simple_bind ) {
1009 rs->sr_text = op->o_ssf
1010 ? "stronger confidentiality required"
1011 : "confidentiality required";
1016 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1017 /* these checks don't apply to SASL bind */
1019 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1020 rs->sr_text = op->o_sasl_ssf
1021 ? "stronger SASL confidentiality required"
1022 : "SASL confidentiality required";
1026 if( op->o_ssf < ssf->sss_ssf ) {
1027 rs->sr_text = op->o_ssf
1028 ? "stronger confidentiality required"
1029 : "confidentiality required";
1035 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1036 rs->sr_text = op->o_transport_ssf
1037 ? "stronger transport confidentiality required for update"
1038 : "transport confidentiality required for update";
1042 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1043 rs->sr_text = op->o_tls_ssf
1044 ? "stronger TLS confidentiality required for update"
1045 : "TLS confidentiality required for update";
1049 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1050 rs->sr_text = op->o_sasl_ssf
1051 ? "stronger SASL confidentiality required for update"
1052 : "SASL confidentiality required for update";
1056 if( op->o_ssf < ssf->sss_update_ssf ) {
1057 rs->sr_text = op->o_ssf
1058 ? "stronger confidentiality required for update"
1059 : "confidentiality required for update";
1063 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1064 BER_BVISEMPTY( &op->o_ndn ) )
1066 rs->sr_text = "modifications require authentication";
1067 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1071 #ifdef SLAP_X_LISTENER_MOD
1072 if ( op->o_conn->c_listener &&
1073 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1074 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1076 /* no "w" mode means readonly */
1077 rs->sr_text = "modifications not allowed on this listener";
1078 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1081 #endif /* SLAP_X_LISTENER_MOD */
1086 /* these checks don't apply to Bind, StartTLS, or Unbind */
1088 if( requires & SLAP_REQUIRE_STRONG ) {
1089 /* should check mechanism */
1090 if( ( op->o_transport_ssf < ssf->sss_transport
1091 && op->o_authtype == LDAP_AUTH_SIMPLE )
1092 || BER_BVISEMPTY( &op->o_dn ) )
1094 rs->sr_text = "strong(er) authentication required";
1095 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1100 if( requires & SLAP_REQUIRE_SASL ) {
1101 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1102 rs->sr_text = "SASL authentication required";
1103 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1108 if( requires & SLAP_REQUIRE_AUTHC ) {
1109 if( BER_BVISEMPTY( &op->o_dn ) ) {
1110 rs->sr_text = "authentication required";
1111 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1116 if( requires & SLAP_REQUIRE_BIND ) {
1118 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1119 version = op->o_conn->c_protocol;
1120 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1123 /* no bind has occurred */
1124 rs->sr_text = "BIND required";
1125 rs->sr_err = LDAP_OPERATIONS_ERROR;
1130 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1131 if( op->o_protocol < LDAP_VERSION3 ) {
1132 /* no bind has occurred */
1133 rs->sr_text = "operation restricted to LDAPv3 clients";
1134 rs->sr_err = LDAP_OPERATIONS_ERROR;
1139 #ifdef SLAP_X_LISTENER_MOD
1140 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1141 if ( op->o_conn->c_listener &&
1142 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1144 /* no "x" mode means bind required */
1145 rs->sr_text = "bind required on this listener";
1146 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1151 if ( !starttls && !updateop ) {
1152 if ( op->o_conn->c_listener &&
1153 !( op->o_conn->c_listener->sl_perms &
1154 ( !BER_BVISEMPTY( &op->o_dn )
1155 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1157 /* no "r" mode means no read */
1158 rs->sr_text = "read not allowed on this listener";
1159 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1163 #endif /* SLAP_X_LISTENER_MOD */
1167 if( ( restrictops & opflag )
1168 || ( exopflag && ( restrictops & exopflag ) ) ) {
1169 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1170 rs->sr_text = "read operations restricted";
1171 } else if ( restrictops & exopflag ) {
1172 rs->sr_text = "extended operation restricted";
1174 rs->sr_text = "operation restricted";
1176 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1180 rs->sr_err = LDAP_SUCCESS;
1184 int backend_check_referrals( Operation *op, SlapReply *rs )
1186 rs->sr_err = LDAP_SUCCESS;
1188 if( op->o_bd->be_chk_referrals ) {
1189 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1191 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1192 send_ldap_result( op, rs );
1204 AttributeDescription *at,
1212 if (op->o_bd == NULL) {
1213 rc = LDAP_NO_SUCH_OBJECT;
1214 } else if ( op->o_bd->be_fetch ) {
1215 rc = ( op->o_bd->be_fetch )( op, ndn,
1218 rc = LDAP_UNWILLING_TO_PERFORM;
1227 struct berval *gr_ndn,
1228 struct berval *op_ndn,
1229 ObjectClass *group_oc,
1230 AttributeDescription *group_at )
1236 Backend *be = op->o_bd;
1238 if ( op->o_abandon ) return SLAPD_ABANDON;
1240 op->o_bd = select_backend( gr_ndn, 0, 0 );
1242 for ( g = op->o_groups; g; g = g->ga_next ) {
1243 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1244 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1248 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1258 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1262 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1266 if ( op->o_pb != NULL ) {
1267 init_group_pblock( op, target, e, op_ndn, group_at );
1269 rc = call_group_preop_plugins( op );
1270 if ( rc == LDAP_SUCCESS ) {
1274 #endif /* LDAP_SLAPI */
1276 a = attr_find( e->e_attrs, group_at );
1278 /* If the attribute is a subtype of labeledURI, treat this as
1279 * a dynamic group ala groupOfURLs
1281 if (is_at_subtype( group_at->ad_type,
1282 slap_schema.si_ad_labeledURI->ad_type ) )
1286 struct berval bv, nbase;
1289 Backend *b2 = op->o_bd;
1291 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1294 op->o_bd = select_backend( op_ndn, 0, 0 );
1295 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1299 rc = LDAP_COMPARE_FALSE;
1300 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1301 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1306 BER_BVZERO( &nbase );
1307 /* host part must be empty */
1308 /* attrs and extensions parts must be empty */
1309 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1310 ludp->lud_attrs || ludp->lud_exts )
1314 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1315 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1316 op->o_tmpmemctx ) != LDAP_SUCCESS )
1320 switch ( ludp->lud_scope ) {
1321 case LDAP_SCOPE_BASE:
1322 if ( !dn_match( &nbase, op_ndn ) ) {
1326 case LDAP_SCOPE_ONELEVEL:
1327 dnParent( op_ndn, &bv );
1328 if ( !dn_match( &nbase, &bv ) ) {
1332 case LDAP_SCOPE_SUBTREE:
1333 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1337 #ifdef LDAP_SCOPE_SUBORDINATE
1338 case LDAP_SCOPE_SUBORDINATE:
1339 if ( dn_match( &nbase, op_ndn ) ||
1340 !dnIsSuffix( op_ndn, &nbase ) )
1346 filter = str2filter_x( op, ludp->lud_filter );
1348 if ( test_filter( NULL, user, filter ) ==
1353 filter_free_x( op, filter );
1356 ldap_free_urldesc( ludp );
1357 if ( !BER_BVISNULL( &nbase ) ) {
1358 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1360 if ( rc == 0 ) break;
1362 if ( user != target ) {
1363 be_entry_release_r( op, user );
1368 rc = value_find_ex( group_at,
1369 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1370 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1371 a->a_nvals, op_ndn, op->o_tmpmemctx );
1372 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1373 rc = LDAP_COMPARE_FALSE;
1376 rc = LDAP_NO_SUCH_ATTRIBUTE;
1379 be_entry_release_r( op, e );
1382 rc = LDAP_NO_SUCH_OBJECT;
1386 if ( op->o_pb ) call_group_postop_plugins( op );
1387 #endif /* LDAP_SLAPI */
1389 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1390 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1392 g->ga_be = op->o_bd;
1393 g->ga_oc = group_oc;
1394 g->ga_at = group_at;
1396 g->ga_len = gr_ndn->bv_len;
1397 strcpy( g->ga_ndn, gr_ndn->bv_val );
1398 g->ga_next = op->o_groups;
1407 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1411 BerVarray *vals = (BerVarray *)c->cac_private;
1412 Operation *op = NULL;
1415 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1420 if ( op->o_conn && access_allowed( op,
1421 e, a->a_desc, NULL, ACL_AUTH,
1422 &c->cac_acl_state ) == 0 ) {
1426 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1428 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1430 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1431 if ( op->o_conn && access_allowed( op,
1434 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1438 &a->a_nvals[i], op->o_tmpmemctx );
1439 if ( !BER_BVISNULL( &v[j] ) ) {
1445 op->o_tmpfree( v, op->o_tmpmemctx );
1449 BER_BVZERO( &v[j] );
1456 #endif /* LDAP_SLAPI */
1463 AttributeDescription *entry_at,
1465 slap_access_t access )
1468 Attribute *a = NULL;
1469 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1470 AccessControlState acl_state = ACL_STATE_INIT;
1471 Backend *be = op->o_bd;
1473 op->o_bd = select_backend( edn, 0, 0 );
1475 if ( target && dn_match( &target->e_nname, edn ) ) {
1479 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1483 a = attr_find( e->e_attrs, entry_at );
1485 SlapReply rs = { 0 };
1486 AttributeName anlist[ 2 ];
1488 anlist[ 0 ].an_name = entry_at->ad_cname;
1489 anlist[ 0 ].an_desc = entry_at;
1490 BER_BVZERO( &anlist[ 1 ].an_name );
1491 rs.sr_attrs = anlist;
1493 /* NOTE: backend_operational() is also called
1494 * when returning results, so it's supposed
1495 * to do no harm to entries */
1497 rc = backend_operational( op, &rs );
1500 if ( rc == LDAP_SUCCESS ) {
1501 if ( rs.sr_operational_attrs ) {
1503 a = rs.sr_operational_attrs;
1506 rc = LDAP_NO_SUCH_ATTRIBUTE;
1514 if ( op->o_conn && access > ACL_NONE &&
1515 access_allowed( op, e, entry_at, NULL,
1516 access, &acl_state ) == 0 )
1518 rc = LDAP_INSUFFICIENT_ACCESS;
1522 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1525 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1527 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1529 if ( op->o_conn && access > ACL_NONE &&
1530 access_allowed( op, e, entry_at,
1537 ber_dupbv_x( &v[j], &a->a_nvals[i],
1539 if ( !BER_BVISNULL( &v[j] ) ) {
1544 op->o_tmpfree( v, op->o_tmpmemctx );
1546 rc = LDAP_INSUFFICIENT_ACCESS;
1549 BER_BVZERO( &v[j] );
1555 else if ( op->o_pb ) {
1556 /* try any computed attributes */
1557 computed_attr_context ctx;
1559 slapi_int_pblock_set_operation( op->o_pb, op );
1561 ctx.cac_pb = op->o_pb;
1562 ctx.cac_attrs = NULL;
1563 ctx.cac_userattrs = 0;
1564 ctx.cac_opattrs = 0;
1565 ctx.cac_acl_state = acl_state;
1566 ctx.cac_private = (void *)vals;
1568 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1570 rc = LDAP_INSUFFICIENT_ACCESS;
1576 #endif /* LDAP_SLAPI */
1577 freeit: if ( e != target ) {
1578 be_entry_release_r( op, e );
1590 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1592 struct berval *nval = (struct berval *)c->cac_private;
1593 Operation *op = NULL;
1595 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1600 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1602 #endif /* LDAP_SLAPI */
1609 AttributeDescription *entry_at,
1610 struct berval *nval,
1611 slap_access_t access,
1615 int rc = LDAP_INSUFFICIENT_ACCESS;
1616 Backend *be = op->o_bd;
1620 assert( op->o_conn );
1622 assert( access > ACL_NONE );
1624 op->o_bd = select_backend( edn, 0, 0 );
1626 if ( target && dn_match( &target->e_nname, edn ) ) {
1630 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1634 Attribute *a = NULL;
1637 if ( entry_at == NULL ) {
1638 entry_at = slap_schema.si_ad_entry;
1641 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1643 if ( access_allowed_mask( op, e, entry_at,
1644 NULL, access, NULL, mask ) == 0 )
1646 rc = LDAP_INSUFFICIENT_ACCESS;
1653 a = attr_find( e->e_attrs, entry_at );
1655 SlapReply rs = { 0 };
1656 AttributeName anlist[ 2 ];
1658 anlist[ 0 ].an_name = entry_at->ad_cname;
1659 anlist[ 0 ].an_desc = entry_at;
1660 BER_BVZERO( &anlist[ 1 ].an_name );
1661 rs.sr_attrs = anlist;
1663 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1665 /* NOTE: backend_operational() is also called
1666 * when returning results, so it's supposed
1667 * to do no harm to entries */
1669 rc = backend_operational( op, &rs );
1672 if ( rc == LDAP_SUCCESS ) {
1673 if ( rs.sr_operational_attrs ) {
1675 a = rs.sr_operational_attrs;
1678 rc = LDAP_NO_SUCH_OBJECT;
1684 if ( access_allowed_mask( op, e, entry_at,
1685 nval, access, NULL, mask ) == 0 )
1687 rc = LDAP_INSUFFICIENT_ACCESS;
1693 else if ( op->o_pb ) {
1694 /* try any computed attributes */
1695 computed_attr_context ctx;
1697 slapi_int_pblock_set_operation( op->o_pb, op );
1699 ctx.cac_pb = op->o_pb;
1700 ctx.cac_attrs = NULL;
1701 ctx.cac_userattrs = 0;
1702 ctx.cac_opattrs = 0;
1703 ctx.cac_private = (void *)nval;
1705 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1707 rc = LDAP_INSUFFICIENT_ACCESS;
1713 #endif /* LDAP_SLAPI */
1715 freeit: if ( e != target ) {
1716 be_entry_release_r( op, e );
1727 int backend_operational(
1735 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1736 /* just count them */ ;
1739 * If operational attributes (allegedly) are required,
1740 * and the backend supports specific operational attributes,
1741 * add them to the attribute list
1743 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1744 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1746 *ap = slap_operational_entryDN( rs->sr_entry );
1747 ap = &(*ap)->a_next;
1750 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1751 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1753 *ap = slap_operational_subschemaSubentry( op->o_bd );
1754 ap = &(*ap)->a_next;
1757 /* Let the overlays have a chance at this */
1759 if ( SLAP_ISOVERLAY( be_orig ) )
1760 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1762 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1763 op->o_bd && op->o_bd->be_operational != NULL )
1765 rc = op->o_bd->be_operational( op, rs );
1773 static void init_group_pblock( Operation *op, Entry *target,
1774 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1776 slapi_int_pblock_set_operation( op->o_pb, op );
1778 slapi_pblock_set( op->o_pb,
1779 SLAPI_X_GROUP_ENTRY, (void *)e );
1780 slapi_pblock_set( op->o_pb,
1781 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1782 slapi_pblock_set( op->o_pb,
1783 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1784 slapi_pblock_set( op->o_pb,
1785 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1788 static int call_group_preop_plugins( Operation *op )
1792 rc = slapi_int_call_plugins( op->o_bd,
1793 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1795 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1796 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1798 rc = LDAP_NO_SUCH_ATTRIBUTE;
1807 static void call_group_postop_plugins( Operation *op )
1809 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1811 #endif /* LDAP_SLAPI */