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>
43 #include "slapi/slapi.h"
45 static void init_group_pblock( Operation *op, Entry *target,
46 Entry *e, struct berval *op_ndn, AttributeDescription *group_at );
47 static int call_group_preop_plugins( Operation *op );
48 static void call_group_postop_plugins( Operation *op );
49 #endif /* LDAP_SLAPI */
52 * If a module is configured as dynamic, its header should not
53 * get included into slapd. While this is a general rule and does
54 * not have much of an effect in UNIX, this rule should be adhered
55 * to for Windows, where dynamic object code should not be implicitly
56 * imported into slapd without appropriate __declspec(dllimport) directives.
60 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
63 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
66 backend_init_controls( BackendInfo *bi )
68 if ( bi->bi_controls ) {
71 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
74 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
75 == LDAP_CONTROL_NOT_FOUND )
77 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
84 bi->bi_ctrls[ cid ] = 1;
91 int backend_init(void)
96 if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
97 /* already initialized */
98 Debug( LDAP_DEBUG_ANY,
99 "backend_init: already initialized\n", 0, 0, 0 );
103 for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ )
105 assert( bi->bi_init );
107 rc = bi->bi_init( bi );
110 Debug( LDAP_DEBUG_ANY,
111 "backend_init: initialized for type \"%s\"\n",
113 /* destroy those we've already inited */
118 if ( slap_binfo[nBackendInfo].bi_destroy ) {
119 slap_binfo[nBackendInfo].bi_destroy(
120 &slap_binfo[nBackendInfo] );
126 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
129 if ( nBackendInfo > 0) {
137 Debug( LDAP_DEBUG_ANY,
138 "backend_init: failed\n",
142 #endif /* SLAPD_MODULES */
145 int backend_add(BackendInfo *aBackendInfo)
149 if ( aBackendInfo->bi_init == NULL ) {
150 Debug( LDAP_DEBUG_ANY, "backend_add: "
151 "backend type \"%s\" does not have the (mandatory)init function\n",
152 aBackendInfo->bi_type, 0, 0 );
156 rc = aBackendInfo->bi_init(aBackendInfo);
158 Debug( LDAP_DEBUG_ANY,
159 "backend_add: initialization for type \"%s\" failed\n",
160 aBackendInfo->bi_type, 0, 0 );
164 (void)backend_init_controls( aBackendInfo );
166 /* now add the backend type to the Backend Info List */
167 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
173 backend_set_controls( BackendDB *be )
175 BackendInfo *bi = be->bd_info;
177 /* back-relay takes care of itself; so may do other */
178 if ( overlay_is_over( be ) ) {
179 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
182 if ( bi->bi_controls ) {
183 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
184 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
185 sizeof( be->be_ctrls ) );
186 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
191 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
192 if ( bi->bi_ctrls[ i ] ) {
193 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
203 /* startup a specific backend database */
204 int backend_startup_one(Backend *be)
210 be->be_pending_csn_list = (struct be_pcl *)
211 ch_calloc( 1, sizeof( struct be_pcl ));
213 LDAP_TAILQ_INIT( be->be_pending_csn_list );
215 Debug( LDAP_DEBUG_TRACE,
216 "backend_startup_one: starting \"%s\"\n",
217 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
220 /* set database controls */
221 (void)backend_set_controls( be );
223 if ( be->bd_info->bi_db_open ) {
224 rc = be->bd_info->bi_db_open( be );
226 (void)backend_set_controls( be );
229 Debug( LDAP_DEBUG_ANY,
230 "backend_startup_one: bi_db_open failed! (%d)\n",
238 int backend_startup(Backend *be)
244 if( ! ( nBackendDB > 0 ) ) {
246 Debug( LDAP_DEBUG_ANY,
247 "backend_startup: %d databases to startup.\n",
253 if ( be->bd_info->bi_open ) {
254 rc = be->bd_info->bi_open( be->bd_info );
256 Debug( LDAP_DEBUG_ANY,
257 "backend_startup: bi_open failed!\n",
264 return backend_startup_one( be );
267 /* open frontend, if required */
268 if ( frontendDB->bd_info->bi_db_open ) {
269 rc = frontendDB->bd_info->bi_db_open( frontendDB );
271 Debug( LDAP_DEBUG_ANY,
272 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
278 /* open each backend type */
280 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
282 if( bi->bi_nDB == 0) {
283 /* no database of this type, don't open */
288 rc = bi->bi_open( bi );
290 Debug( LDAP_DEBUG_ANY,
291 "backend_startup: bi_open %d (%s) failed!\n",
297 (void)backend_init_controls( bi );
300 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
301 LDAP_STAILQ_INIT( &slapd_rq.task_list );
302 LDAP_STAILQ_INIT( &slapd_rq.run_list );
304 /* open each backend database */
306 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
308 if ( be->be_suffix == NULL ) {
309 Debug( LDAP_DEBUG_ANY,
310 "backend_startup: warning, database %d (%s) "
312 i, be->bd_info->bi_type, 0 );
314 /* append global access controls */
315 acl_append( &be->be_acl, frontendDB->be_acl );
317 rc = backend_startup_one( be );
322 if ( be->be_syncinfo ) {
325 if ( !( be->be_search && be->be_add &&
326 be->be_modify && be->be_delete )) {
327 Debug( LDAP_DEBUG_ANY,
328 "backend_startup: database(%d) does not support "
329 "operations required for syncrepl", i, 0, 0 );
334 si = be->be_syncinfo;
337 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
338 ldap_pvt_runqueue_insert( &slapd_rq,
339 si->si_interval, do_syncrepl, (void *) si );
340 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
348 int backend_num( Backend *be )
353 if( be == NULL ) return -1;
355 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
356 if( be == b2 ) return i;
362 int backend_shutdown( Backend *be )
369 /* shutdown a specific backend database */
371 if ( be->bd_info->bi_nDB == 0 ) {
372 /* no database of this type, we never opened it */
376 if ( be->bd_info->bi_db_close ) {
377 be->bd_info->bi_db_close( be );
380 if( be->bd_info->bi_close ) {
381 be->bd_info->bi_close( be->bd_info );
387 /* close each backend database */
388 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
389 if ( be->bd_info->bi_db_close ) {
390 be->bd_info->bi_db_close( be );
394 Debug( LDAP_DEBUG_ANY,
395 "backend_close: bi_db_close %s failed!\n",
400 /* close each backend type */
401 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
402 if( bi->bi_nDB == 0 ) {
403 /* no database of this type */
412 /* close frontend, if required */
413 if ( frontendDB->bd_info->bi_db_close ) {
414 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
416 Debug( LDAP_DEBUG_ANY,
417 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
425 int backend_destroy(void)
430 struct slap_csn_entry *csne;
432 /* destroy each backend database */
433 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
434 LDAP_STAILQ_REMOVE_HEAD(&backendDB, be_next);
436 if ( bd->be_syncinfo ) {
437 syncinfo_free( bd->be_syncinfo );
440 if ( bd->be_pending_csn_list ) {
441 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
443 struct slap_csn_entry *tmp_csne = csne;
445 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
446 ch_free( csne->ce_csn.bv_val );
447 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
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 );
470 /* destroy each backend type */
471 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
472 if( bi->bi_destroy ) {
473 bi->bi_destroy( bi );
478 LDAP_STAILQ_INIT(&backendInfo);
480 /* destroy frontend database */
483 if ( bd->bd_info->bi_db_destroy ) {
484 bd->bd_info->bi_db_destroy( bd );
486 ber_bvarray_free( bd->be_suffix );
487 ber_bvarray_free( bd->be_nsuffix );
488 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
489 free( bd->be_rootdn.bv_val );
491 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
492 free( bd->be_rootndn.bv_val );
494 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
495 free( bd->be_rootpw.bv_val );
497 acl_destroy( bd->be_acl, frontendDB->be_acl );
503 BackendInfo* backend_info(const char *type)
507 /* search for the backend type */
508 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
509 if( strcasecmp(bi->bi_type, type) == 0 ) {
523 BackendInfo *bi = backend_info(type);
527 fprintf( stderr, "Unrecognized database type (%s)\n", type );
531 be = ch_calloc( 1, sizeof(Backend) );
533 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
537 be->be_def_limit = frontendDB->be_def_limit;
538 be->be_dfltaccess = frontendDB->be_dfltaccess;
540 be->be_restrictops = frontendDB->be_restrictops;
541 be->be_requires = frontendDB->be_requires;
542 be->be_ssf_set = frontendDB->be_ssf_set;
544 be->be_pcl_mutexp = &be->be_pcl_mutex;
545 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
547 /* assign a default depth limit for alias deref */
548 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
550 if ( bi->bi_db_init ) {
551 rc = bi->bi_db_init( be );
555 fprintf( stderr, "database init failed (%s)\n", type );
570 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
571 if ( be->bd_info->bi_db_close ) {
572 be->bd_info->bi_db_close( be );
576 if ( frontendDB->bd_info->bi_db_close ) {
577 (*frontendDB->bd_info->bi_db_close)( frontendDB );
589 ber_len_t len, dnlen = dn->bv_len;
590 Backend *be, *b2 = NULL;
592 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
593 if ( be->be_nsuffix == NULL ) {
597 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
599 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
604 len = be->be_nsuffix[j].bv_len;
607 /* suffix is longer than DN */
612 * input DN is normalized, so the separator check
613 * need not look at escaping
615 if ( len && len < dnlen &&
616 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
621 if ( strcmp( be->be_nsuffix[j].bv_val,
622 &dn->bv_val[dnlen-len] ) == 0 )
627 if( manageDSAit && len == dnlen &&
628 !SLAP_GLUE_SUBORDINATE( be ) ) {
645 struct berval *bvsuffix )
649 if ( be->be_nsuffix == NULL ) {
653 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
654 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
663 be_isroot_dn( Backend *be, struct berval *ndn )
665 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
669 return dn_match( &be->be_rootndn, ndn );
673 be_slurp_update( Operation *op )
675 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
676 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
680 be_shadow_update( Operation *op )
682 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
683 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
687 be_isupdate_dn( Backend *be, struct berval *ndn )
689 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
693 return dn_match( &be->be_update_ndn, ndn );
697 be_root_dn( Backend *be )
699 return &be->be_rootdn;
703 be_isroot( Operation *op )
705 return be_isroot_dn( op->o_bd, &op->o_ndn );
709 be_isroot_pw( Operation *op )
713 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
717 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
722 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
723 op->o_conn->c_sasl_authctx, NULL );
726 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
729 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
741 if ( op->o_bd->be_release ) {
742 /* free and release entry from backend */
743 return op->o_bd->be_release( op, e, rw );
752 backend_unbind( Operation *op, SlapReply *rs )
757 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
758 #if defined( LDAP_SLAPI )
761 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
762 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)be );
763 rc = slapi_int_call_plugins( be,
764 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
767 * A preoperation plugin failure will abort the
770 Debug(LDAP_DEBUG_TRACE,
771 "do_bind: Unbind preoperation plugin failed\n",
776 #endif /* defined( LDAP_SLAPI ) */
778 if ( be->be_unbind ) {
780 be->be_unbind( op, rs );
783 #if defined( LDAP_SLAPI )
784 if ( op->o_pb != NULL && slapi_int_call_plugins( be,
785 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
787 Debug(LDAP_DEBUG_TRACE,
788 "do_unbind: Unbind postoperation plugins failed\n",
791 #endif /* defined( LDAP_SLAPI ) */
799 backend_connection_init(
804 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
805 if ( be->be_connection_init ) {
806 be->be_connection_init( be, conn);
814 backend_connection_destroy(
819 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
820 if ( be->be_connection_destroy ) {
821 be->be_connection_destroy( be, conn);
829 backend_check_controls(
833 LDAPControl **ctrls = op->o_ctrls;
834 rs->sr_err = LDAP_SUCCESS;
837 for( ; *ctrls != NULL ; ctrls++ ) {
840 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
841 case LDAP_CONTROL_NOT_FOUND:
842 /* unrecognized control */
843 if ( (*ctrls)->ldctl_iscritical ) {
844 /* should not be reachable */
845 Debug( LDAP_DEBUG_ANY,
846 "backend_check_controls: unrecognized control: %s\n",
847 (*ctrls)->ldctl_oid, 0, 0 );
852 case LDAP_COMPARE_FALSE:
853 if ( !op->o_bd->be_ctrls[ cid ] )
855 /* Per RFC 2251 (and LDAPBIS discussions), if the control
856 * is recognized and appropriate for the operation (which
857 * we've already verified), then the server should make
858 * use of the control when performing the operation.
860 * Here we find that operation extended by the control
861 * is not unavailable in a particular context, hence the
862 * return of unwillingToPerform.
864 rs->sr_text = "control unavailable in context";
865 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
870 case LDAP_COMPARE_TRUE:
875 rs->sr_text = "unable to check control";
876 rs->sr_err = LDAP_OTHER;
887 backend_check_restrictions(
890 struct berval *opdata )
892 slap_mask_t restrictops;
893 slap_mask_t requires;
895 slap_mask_t exopflag = 0;
902 int rc = SLAP_CB_CONTINUE;
904 if ( op->o_bd->be_chk_controls ) {
905 rc = ( *op->o_bd->be_chk_controls )( op, rs );
908 if ( rc == SLAP_CB_CONTINUE ) {
909 rc = backend_check_controls( op, rs );
912 if ( rc != LDAP_SUCCESS ) {
916 restrictops = op->o_bd->be_restrictops;
917 requires = op->o_bd->be_requires;
918 ssf = &op->o_bd->be_ssf_set;
921 restrictops = frontendDB->be_restrictops;
922 requires = frontendDB->be_requires;
923 ssf = &frontendDB->be_ssf_set;
926 switch( op->o_tag ) {
928 opflag = SLAP_RESTRICT_OP_ADD;
932 opflag = SLAP_RESTRICT_OP_BIND;
935 case LDAP_REQ_COMPARE:
936 opflag = SLAP_RESTRICT_OP_COMPARE;
938 case LDAP_REQ_DELETE:
940 opflag = SLAP_RESTRICT_OP_DELETE;
942 case LDAP_REQ_EXTENDED:
943 opflag = SLAP_RESTRICT_OP_EXTENDED;
946 /* treat unspecified as a modify */
947 opflag = SLAP_RESTRICT_OP_MODIFY;
952 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
955 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
959 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
960 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
964 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
965 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
969 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
970 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
975 /* treat everything else as a modify */
976 opflag = SLAP_RESTRICT_OP_MODIFY;
980 case LDAP_REQ_MODIFY:
982 opflag = SLAP_RESTRICT_OP_MODIFY;
984 case LDAP_REQ_RENAME:
986 opflag = SLAP_RESTRICT_OP_RENAME;
988 case LDAP_REQ_SEARCH:
989 opflag = SLAP_RESTRICT_OP_SEARCH;
991 case LDAP_REQ_UNBIND:
996 rs->sr_text = "restrict operations internal error";
997 rs->sr_err = LDAP_OTHER;
1002 /* these checks don't apply to StartTLS */
1004 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1005 if( op->o_transport_ssf < ssf->sss_transport ) {
1006 rs->sr_text = op->o_transport_ssf
1007 ? "stronger transport confidentiality required"
1008 : "transport confidentiality required";
1012 if( op->o_tls_ssf < ssf->sss_tls ) {
1013 rs->sr_text = op->o_tls_ssf
1014 ? "stronger TLS confidentiality required"
1015 : "TLS confidentiality required";
1020 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1021 /* simple bind specific check */
1022 if( op->o_ssf < ssf->sss_simple_bind ) {
1023 rs->sr_text = op->o_ssf
1024 ? "stronger confidentiality required"
1025 : "confidentiality required";
1030 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1031 /* these checks don't apply to SASL bind */
1033 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1034 rs->sr_text = op->o_sasl_ssf
1035 ? "stronger SASL confidentiality required"
1036 : "SASL confidentiality required";
1040 if( op->o_ssf < ssf->sss_ssf ) {
1041 rs->sr_text = op->o_ssf
1042 ? "stronger confidentiality required"
1043 : "confidentiality required";
1049 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1050 rs->sr_text = op->o_transport_ssf
1051 ? "stronger transport confidentiality required for update"
1052 : "transport confidentiality required for update";
1056 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1057 rs->sr_text = op->o_tls_ssf
1058 ? "stronger TLS confidentiality required for update"
1059 : "TLS confidentiality required for update";
1063 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1064 rs->sr_text = op->o_sasl_ssf
1065 ? "stronger SASL confidentiality required for update"
1066 : "SASL confidentiality required for update";
1070 if( op->o_ssf < ssf->sss_update_ssf ) {
1071 rs->sr_text = op->o_ssf
1072 ? "stronger confidentiality required for update"
1073 : "confidentiality required for update";
1077 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1078 BER_BVISEMPTY( &op->o_ndn ) )
1080 rs->sr_text = "modifications require authentication";
1081 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1085 #ifdef SLAP_X_LISTENER_MOD
1086 if ( op->o_conn->c_listener &&
1087 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1088 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1090 /* no "w" mode means readonly */
1091 rs->sr_text = "modifications not allowed on this listener";
1092 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1095 #endif /* SLAP_X_LISTENER_MOD */
1100 /* these checks don't apply to Bind, StartTLS, or Unbind */
1102 if( requires & SLAP_REQUIRE_STRONG ) {
1103 /* should check mechanism */
1104 if( ( op->o_transport_ssf < ssf->sss_transport
1105 && op->o_authtype == LDAP_AUTH_SIMPLE )
1106 || BER_BVISEMPTY( &op->o_dn ) )
1108 rs->sr_text = "strong(er) authentication required";
1109 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1114 if( requires & SLAP_REQUIRE_SASL ) {
1115 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1116 rs->sr_text = "SASL authentication required";
1117 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1122 if( requires & SLAP_REQUIRE_AUTHC ) {
1123 if( BER_BVISEMPTY( &op->o_dn ) ) {
1124 rs->sr_text = "authentication required";
1125 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1130 if( requires & SLAP_REQUIRE_BIND ) {
1132 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1133 version = op->o_conn->c_protocol;
1134 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1137 /* no bind has occurred */
1138 rs->sr_text = "BIND required";
1139 rs->sr_err = LDAP_OPERATIONS_ERROR;
1144 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1145 if( op->o_protocol < LDAP_VERSION3 ) {
1146 /* no bind has occurred */
1147 rs->sr_text = "operation restricted to LDAPv3 clients";
1148 rs->sr_err = LDAP_OPERATIONS_ERROR;
1153 #ifdef SLAP_X_LISTENER_MOD
1154 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1155 if ( op->o_conn->c_listener &&
1156 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1158 /* no "x" mode means bind required */
1159 rs->sr_text = "bind required on this listener";
1160 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1165 if ( !starttls && !updateop ) {
1166 if ( op->o_conn->c_listener &&
1167 !( op->o_conn->c_listener->sl_perms &
1168 ( !BER_BVISEMPTY( &op->o_dn )
1169 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1171 /* no "r" mode means no read */
1172 rs->sr_text = "read not allowed on this listener";
1173 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1177 #endif /* SLAP_X_LISTENER_MOD */
1181 if( ( restrictops & opflag )
1182 || ( exopflag && ( restrictops & exopflag ) ) ) {
1183 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1184 rs->sr_text = "read operations restricted";
1185 } else if ( restrictops & exopflag ) {
1186 rs->sr_text = "extended operation restricted";
1188 rs->sr_text = "operation restricted";
1190 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1194 rs->sr_err = LDAP_SUCCESS;
1198 int backend_check_referrals( Operation *op, SlapReply *rs )
1200 rs->sr_err = LDAP_SUCCESS;
1202 if( op->o_bd->be_chk_referrals ) {
1203 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1205 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1206 send_ldap_result( op, rs );
1218 AttributeDescription *at,
1226 if (op->o_bd == NULL) {
1227 rc = LDAP_NO_SUCH_OBJECT;
1228 } else if ( op->o_bd->be_fetch ) {
1229 rc = ( op->o_bd->be_fetch )( op, ndn,
1232 rc = LDAP_UNWILLING_TO_PERFORM;
1241 struct berval *gr_ndn,
1242 struct berval *op_ndn,
1243 ObjectClass *group_oc,
1244 AttributeDescription *group_at )
1250 Backend *be = op->o_bd;
1252 if ( op->o_abandon ) return SLAPD_ABANDON;
1254 op->o_bd = select_backend( gr_ndn, 0, 0 );
1256 for ( g = op->o_groups; g; g = g->ga_next ) {
1257 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1258 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1262 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1272 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1276 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1280 if ( op->o_pb != NULL ) {
1281 init_group_pblock( op, target, e, op_ndn, group_at );
1283 rc = call_group_preop_plugins( op );
1284 if ( rc == LDAP_SUCCESS ) {
1288 #endif /* LDAP_SLAPI */
1290 a = attr_find( e->e_attrs, group_at );
1292 /* If the attribute is a subtype of labeledURI, treat this as
1293 * a dynamic group ala groupOfURLs
1295 if (is_at_subtype( group_at->ad_type,
1296 slap_schema.si_ad_labeledURI->ad_type ) )
1300 struct berval bv, nbase;
1303 Backend *b2 = op->o_bd;
1305 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1308 op->o_bd = select_backend( op_ndn, 0, 0 );
1309 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1314 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1315 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1320 BER_BVZERO( &nbase );
1321 /* host part must be empty */
1322 /* attrs and extensions parts must be empty */
1323 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1324 ludp->lud_attrs || ludp->lud_exts )
1328 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1329 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1330 op->o_tmpmemctx ) != LDAP_SUCCESS )
1334 switch ( ludp->lud_scope ) {
1335 case LDAP_SCOPE_BASE:
1336 if ( !dn_match( &nbase, op_ndn ) ) {
1340 case LDAP_SCOPE_ONELEVEL:
1341 dnParent( op_ndn, &bv );
1342 if ( !dn_match( &nbase, &bv ) ) {
1346 case LDAP_SCOPE_SUBTREE:
1347 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1351 #ifdef LDAP_SCOPE_SUBORDINATE
1352 case LDAP_SCOPE_SUBORDINATE:
1353 if ( dn_match( &nbase, op_ndn ) ||
1354 !dnIsSuffix( op_ndn, &nbase ) )
1360 filter = str2filter_x( op, ludp->lud_filter );
1362 if ( test_filter( NULL, user, filter ) ==
1367 filter_free_x( op, filter );
1370 ldap_free_urldesc( ludp );
1371 if ( !BER_BVISNULL( &nbase ) ) {
1372 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1374 if ( rc == 0 ) break;
1376 if ( user != target ) {
1377 be_entry_release_r( op, user );
1382 rc = value_find_ex( group_at,
1383 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1384 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1385 a->a_nvals, op_ndn, op->o_tmpmemctx );
1388 rc = LDAP_NO_SUCH_ATTRIBUTE;
1391 be_entry_release_r( op, e );
1394 rc = LDAP_NO_SUCH_OBJECT;
1398 if ( op->o_pb ) call_group_postop_plugins( op );
1399 #endif /* LDAP_SLAPI */
1401 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1402 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1404 g->ga_be = op->o_bd;
1405 g->ga_oc = group_oc;
1406 g->ga_at = group_at;
1408 g->ga_len = gr_ndn->bv_len;
1409 strcpy( g->ga_ndn, gr_ndn->bv_val );
1410 g->ga_next = op->o_groups;
1419 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1423 BerVarray *vals = (BerVarray *)c->cac_private;
1424 Operation *op = NULL;
1427 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1432 if ( op->o_conn && access_allowed( op,
1433 e, a->a_desc, NULL, ACL_AUTH,
1434 &c->cac_acl_state ) == 0 ) {
1438 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1440 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1442 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1443 if ( op->o_conn && access_allowed( op,
1446 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1450 &a->a_nvals[i], op->o_tmpmemctx );
1451 if ( !BER_BVISNULL( &v[j] ) ) {
1457 op->o_tmpfree( v, op->o_tmpmemctx );
1461 BER_BVZERO( &v[j] );
1468 #endif /* LDAP_SLAPI */
1475 AttributeDescription *entry_at,
1477 slap_access_t access )
1480 Attribute *a = NULL;
1481 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1482 AccessControlState acl_state = ACL_STATE_INIT;
1483 Backend *be = op->o_bd;
1485 op->o_bd = select_backend( edn, 0, 0 );
1487 if ( target && dn_match( &target->e_nname, edn ) ) {
1491 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1495 a = attr_find( e->e_attrs, entry_at );
1497 SlapReply rs = { 0 };
1498 AttributeName anlist[ 2 ];
1500 anlist[ 0 ].an_name = entry_at->ad_cname;
1501 anlist[ 0 ].an_desc = entry_at;
1502 BER_BVZERO( &anlist[ 1 ].an_name );
1503 rs.sr_attrs = anlist;
1505 /* NOTE: backend_operational() is also called
1506 * when returning results, so it's supposed
1507 * to do no harm to entries */
1509 rc = backend_operational( op, &rs );
1512 if ( rc == LDAP_SUCCESS ) {
1513 if ( rs.sr_operational_attrs ) {
1515 a = rs.sr_operational_attrs;
1518 rc = LDAP_NO_SUCH_ATTRIBUTE;
1526 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1527 e, entry_at, NULL, access,
1528 &acl_state ) == 0 ) {
1529 rc = LDAP_INSUFFICIENT_ACCESS;
1533 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1536 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1538 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1540 if ( op->o_conn && access > ACL_NONE &&
1541 access_allowed( op, e,
1549 ber_dupbv_x( &v[j], &a->a_nvals[i],
1551 if ( !BER_BVISNULL( &v[j] ) ) {
1556 op->o_tmpfree( v, op->o_tmpmemctx );
1558 rc = LDAP_INSUFFICIENT_ACCESS;
1561 BER_BVZERO( &v[j] );
1567 else if ( op->o_pb ) {
1568 /* try any computed attributes */
1569 computed_attr_context ctx;
1571 slapi_int_pblock_set_operation( op->o_pb, op );
1573 ctx.cac_pb = op->o_pb;
1574 ctx.cac_attrs = NULL;
1575 ctx.cac_userattrs = 0;
1576 ctx.cac_opattrs = 0;
1577 ctx.cac_acl_state = acl_state;
1578 ctx.cac_private = (void *)vals;
1580 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1582 rc = LDAP_INSUFFICIENT_ACCESS;
1588 #endif /* LDAP_SLAPI */
1589 freeit: if ( e != target ) {
1590 be_entry_release_r( op, e );
1602 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1604 struct berval *nval = (struct berval *)c->cac_private;
1605 Operation *op = NULL;
1607 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1612 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1614 #endif /* LDAP_SLAPI */
1621 AttributeDescription *entry_at,
1622 struct berval *nval,
1623 slap_access_t access,
1627 int rc = LDAP_INSUFFICIENT_ACCESS;
1628 Backend *be = op->o_bd;
1632 assert( op->o_conn );
1634 assert( access > ACL_NONE );
1636 op->o_bd = select_backend( edn, 0, 0 );
1638 if ( target && dn_match( &target->e_nname, edn ) ) {
1642 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1646 Attribute *a = NULL;
1649 if ( entry_at == NULL ) {
1650 entry_at = slap_schema.si_ad_entry;
1653 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1655 if ( access_allowed_mask( op, e, entry_at,
1656 NULL, access, NULL, mask ) == 0 )
1658 rc = LDAP_INSUFFICIENT_ACCESS;
1665 a = attr_find( e->e_attrs, entry_at );
1667 SlapReply rs = { 0 };
1668 AttributeName anlist[ 2 ];
1670 anlist[ 0 ].an_name = entry_at->ad_cname;
1671 anlist[ 0 ].an_desc = entry_at;
1672 BER_BVZERO( &anlist[ 1 ].an_name );
1673 rs.sr_attrs = anlist;
1675 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1677 /* NOTE: backend_operational() is also called
1678 * when returning results, so it's supposed
1679 * to do no harm to entries */
1681 rc = backend_operational( op, &rs );
1684 if ( rc == LDAP_SUCCESS ) {
1685 if ( rs.sr_operational_attrs ) {
1687 a = rs.sr_operational_attrs;
1690 rc = LDAP_NO_SUCH_OBJECT;
1696 if ( access_allowed_mask( op, e, entry_at,
1697 nval, access, NULL, mask ) == 0 )
1699 rc = LDAP_INSUFFICIENT_ACCESS;
1705 else if ( op->o_pb ) {
1706 /* try any computed attributes */
1707 computed_attr_context ctx;
1709 slapi_int_pblock_set_operation( op->o_pb, op );
1711 ctx.cac_pb = op->o_pb;
1712 ctx.cac_attrs = NULL;
1713 ctx.cac_userattrs = 0;
1714 ctx.cac_opattrs = 0;
1715 ctx.cac_private = (void *)nval;
1717 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1719 rc = LDAP_INSUFFICIENT_ACCESS;
1725 #endif /* LDAP_SLAPI */
1727 freeit: if ( e != target ) {
1728 be_entry_release_r( op, e );
1739 int backend_operational(
1747 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1748 /* just count them */ ;
1751 * If operational attributes (allegedly) are required,
1752 * and the backend supports specific operational attributes,
1753 * add them to the attribute list
1755 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1756 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1758 *ap = slap_operational_entryDN( rs->sr_entry );
1759 ap = &(*ap)->a_next;
1762 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1763 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1765 *ap = slap_operational_subschemaSubentry( op->o_bd );
1766 ap = &(*ap)->a_next;
1769 /* Let the overlays have a chance at this */
1771 if ( SLAP_ISOVERLAY( be_orig ) )
1772 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1774 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1775 op->o_bd && op->o_bd->be_operational != NULL )
1777 rc = op->o_bd->be_operational( op, rs );
1785 static void init_group_pblock( Operation *op, Entry *target,
1786 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1788 slapi_int_pblock_set_operation( op->o_pb, op );
1790 slapi_pblock_set( op->o_pb,
1791 SLAPI_X_GROUP_ENTRY, (void *)e );
1792 slapi_pblock_set( op->o_pb,
1793 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1794 slapi_pblock_set( op->o_pb,
1795 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1796 slapi_pblock_set( op->o_pb,
1797 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1800 static int call_group_preop_plugins( Operation *op )
1804 rc = slapi_int_call_plugins( op->o_bd,
1805 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1807 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1808 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1810 rc = LDAP_NO_SUCH_ATTRIBUTE;
1819 static void call_group_postop_plugins( Operation *op )
1821 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1823 #endif /* LDAP_SLAPI */