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>
42 * If a module is configured as dynamic, its header should not
43 * get included into slapd. While this is a general rule and does
44 * not have much of an effect in UNIX, this rule should be adhered
45 * to for Windows, where dynamic object code should not be implicitly
46 * imported into slapd without appropriate __declspec(dllimport) directives.
50 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
53 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
56 backend_init_controls( BackendInfo *bi )
58 if ( bi->bi_controls ) {
61 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
64 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
65 == LDAP_CONTROL_NOT_FOUND )
67 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
74 bi->bi_ctrls[ cid ] = 1;
81 int backend_init(void)
86 if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
87 /* already initialized */
88 Debug( LDAP_DEBUG_ANY,
89 "backend_init: already initialized\n", 0, 0, 0 );
93 for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ ) {
94 assert( bi->bi_init != 0 );
96 rc = bi->bi_init( bi );
99 Debug( LDAP_DEBUG_ANY,
100 "backend_init: initialized for type \"%s\"\n",
102 /* destroy those we've already inited */
107 if ( slap_binfo[nBackendInfo].bi_destroy ) {
108 slap_binfo[nBackendInfo].bi_destroy(
109 &slap_binfo[nBackendInfo] );
115 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
118 if ( nBackendInfo > 0) {
126 Debug( LDAP_DEBUG_ANY,
127 "backend_init: failed\n",
131 #endif /* SLAPD_MODULES */
134 int backend_add(BackendInfo *aBackendInfo)
138 if ( aBackendInfo->bi_init == NULL ) {
139 Debug( LDAP_DEBUG_ANY, "backend_add: "
140 "backend type \"%s\" does not have the (mandatory)init function\n",
141 aBackendInfo->bi_type, 0, 0 );
145 rc = aBackendInfo->bi_init(aBackendInfo);
147 Debug( LDAP_DEBUG_ANY,
148 "backend_add: initialization for type \"%s\" failed\n",
149 aBackendInfo->bi_type, 0, 0 );
153 (void)backend_init_controls( aBackendInfo );
155 /* now add the backend type to the Backend Info List */
156 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
162 backend_set_controls( BackendDB *be )
164 BackendInfo *bi = be->bd_info;
166 /* back-relay takes care of itself; so may do other */
167 if ( overlay_is_over( be ) ) {
168 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
171 if ( bi->bi_controls ) {
172 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
173 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
174 sizeof( be->be_ctrls ) );
175 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
180 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
181 if ( bi->bi_ctrls[ i ] ) {
182 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
192 /* startup a specific backend database */
193 int backend_startup_one(Backend *be, ConfigReply *cr)
197 assert( be != NULL );
199 be->be_pending_csn_list = (struct be_pcl *)
200 ch_calloc( 1, sizeof( struct be_pcl ) );
202 LDAP_TAILQ_INIT( be->be_pending_csn_list );
204 Debug( LDAP_DEBUG_TRACE,
205 "backend_startup_one: starting \"%s\"\n",
206 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
209 /* set database controls */
210 (void)backend_set_controls( be );
212 if ( be->bd_info->bi_db_open ) {
213 rc = be->bd_info->bi_db_open( be, cr );
215 (void)backend_set_controls( be );
218 Debug( LDAP_DEBUG_ANY,
219 "backend_startup_one: bi_db_open failed! (%d)\n",
227 int backend_startup(Backend *be)
232 ConfigReply cr={0, ""};
234 if( ! ( nBackendDB > 0 ) ) {
236 Debug( LDAP_DEBUG_ANY,
237 "backend_startup: %d databases to startup.\n",
243 if ( be->bd_info->bi_open ) {
244 rc = be->bd_info->bi_open( be->bd_info );
246 Debug( LDAP_DEBUG_ANY,
247 "backend_startup: bi_open failed!\n",
253 /* append global access controls */
254 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
256 return backend_startup_one( be, &cr );
259 /* open frontend, if required */
260 if ( frontendDB->bd_info->bi_db_open ) {
261 rc = frontendDB->bd_info->bi_db_open( frontendDB, NULL );
263 Debug( LDAP_DEBUG_ANY,
264 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
270 /* open each backend type */
272 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
274 if( bi->bi_nDB == 0) {
275 /* no database of this type, don't open */
280 rc = bi->bi_open( bi );
282 Debug( LDAP_DEBUG_ANY,
283 "backend_startup: bi_open %d (%s) failed!\n",
289 (void)backend_init_controls( bi );
292 /* open each backend database */
294 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
296 if ( be->be_suffix == NULL ) {
297 Debug( LDAP_DEBUG_ANY,
298 "backend_startup: warning, database %d (%s) "
300 i, be->bd_info->bi_type, 0 );
302 /* append global access controls */
303 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
305 rc = backend_startup_one( be, &cr );
313 int backend_num( Backend *be )
318 if( be == NULL ) return -1;
320 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
321 if( be == b2 ) return i;
327 int backend_shutdown( Backend *be )
333 /* shutdown a specific backend database */
335 if ( be->bd_info->bi_nDB == 0 ) {
336 /* no database of this type, we never opened it */
340 if ( be->bd_info->bi_db_close ) {
341 be->bd_info->bi_db_close( be, NULL );
344 if( be->bd_info->bi_close ) {
345 be->bd_info->bi_close( be->bd_info );
351 /* close each backend database */
352 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
353 if ( be->bd_info->bi_db_close ) {
354 be->bd_info->bi_db_close( be, NULL );
358 Debug( LDAP_DEBUG_ANY,
359 "backend_close: bi_db_close %s failed!\n",
364 /* close each backend type */
365 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
366 if( bi->bi_nDB == 0 ) {
367 /* no database of this type */
376 /* close frontend, if required */
377 if ( frontendDB->bd_info->bi_db_close ) {
378 rc = frontendDB->bd_info->bi_db_close ( frontendDB, NULL );
380 Debug( LDAP_DEBUG_ANY,
381 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
390 * This function is supposed to be the exact counterpart
391 * of backend_startup_one(), although this one calls bi_db_destroy()
392 * while backend_startup_one() calls bi_db_open().
394 * Make sure backend_stopdown_one() destroys resources allocated
395 * by backend_startup_one(); only call backend_destroy_one() when
396 * all stuff in a BackendDB needs to be destroyed
399 backend_stopdown_one( BackendDB *bd )
401 if ( bd->be_pending_csn_list ) {
402 struct slap_csn_entry *csne;
403 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
405 struct slap_csn_entry *tmp_csne = csne;
407 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
408 ch_free( csne->ce_csn.bv_val );
409 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
412 ch_free( bd->be_pending_csn_list );
415 if ( bd->bd_info->bi_db_destroy ) {
416 bd->bd_info->bi_db_destroy( bd, NULL );
420 void backend_destroy_one( BackendDB *bd, int dynamic )
423 LDAP_STAILQ_REMOVE(&backendDB, bd, BackendDB, be_next );
426 if ( bd->be_syncinfo ) {
427 syncinfo_free( bd->be_syncinfo, 1 );
430 backend_stopdown_one( bd );
432 ber_bvarray_free( bd->be_suffix );
433 ber_bvarray_free( bd->be_nsuffix );
434 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
435 free( bd->be_rootdn.bv_val );
437 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
438 free( bd->be_rootndn.bv_val );
440 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
441 free( bd->be_rootpw.bv_val );
443 acl_destroy( bd->be_acl, frontendDB->be_acl );
444 limits_destroy( bd->be_limits );
445 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
446 ch_free( bd->be_update_ndn.bv_val );
448 if ( bd->be_update_refs ) {
449 ber_bvarray_free( bd->be_update_refs );
457 int backend_destroy(void)
462 /* destroy each backend database */
463 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
464 backend_destroy_one( bd, 1 );
467 /* destroy each backend type */
468 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
469 if( bi->bi_destroy ) {
470 bi->bi_destroy( bi );
475 LDAP_STAILQ_INIT(&backendInfo);
477 /* destroy frontend database */
480 if ( bd->bd_info->bi_db_destroy ) {
481 bd->bd_info->bi_db_destroy( bd, NULL );
483 ber_bvarray_free( bd->be_suffix );
484 ber_bvarray_free( bd->be_nsuffix );
485 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
486 free( bd->be_rootdn.bv_val );
488 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
489 free( bd->be_rootndn.bv_val );
491 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
492 free( bd->be_rootpw.bv_val );
494 acl_destroy( bd->be_acl, frontendDB->be_acl );
500 BackendInfo* backend_info(const char *type)
504 /* search for the backend type */
505 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
506 if( strcasecmp(bi->bi_type, type) == 0 ) {
520 /* If idx < 0, just add to end of list */
522 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
523 } else if ( idx == 0 ) {
524 LDAP_STAILQ_INSERT_HEAD(&backendDB, be, be_next);
529 b2 = LDAP_STAILQ_FIRST(&backendDB);
531 for (i=0; i<idx; i++) {
532 b2 = LDAP_STAILQ_NEXT(b2, be_next);
534 LDAP_STAILQ_INSERT_AFTER(&backendDB, b2, be, be_next);
544 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
545 backend_db_insert(be, idx);
555 BackendInfo *bi = backend_info(type);
560 fprintf( stderr, "Unrecognized database type (%s)\n", type );
564 /* If be is provided, treat it as private. Otherwise allocate
565 * one and add it to the global list.
568 be = ch_calloc( 1, sizeof(Backend) );
570 if ( idx >= nbackends )
573 backend_db_insert( be, idx );
578 be->be_def_limit = frontendDB->be_def_limit;
579 be->be_dfltaccess = frontendDB->be_dfltaccess;
581 be->be_restrictops = frontendDB->be_restrictops;
582 be->be_requires = frontendDB->be_requires;
583 be->be_ssf_set = frontendDB->be_ssf_set;
585 be->be_pcl_mutexp = &be->be_pcl_mutex;
586 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
588 /* assign a default depth limit for alias deref */
589 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
591 if ( bi->bi_db_init ) {
592 rc = bi->bi_db_init( be, cr );
596 fprintf( stderr, "database init failed (%s)\n", type );
597 /* If we created and linked this be, remove it and free it */
599 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
615 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
616 if ( be->bd_info->bi_db_close ) {
617 be->bd_info->bi_db_close( be, NULL );
621 if ( frontendDB->bd_info->bi_db_close ) {
622 frontendDB->bd_info->bi_db_close( frontendDB, NULL );
633 ber_len_t len, dnlen = dn->bv_len;
636 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
637 if ( be->be_nsuffix == NULL || SLAP_DBHIDDEN( be )) {
641 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
643 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
648 len = be->be_nsuffix[j].bv_len;
651 /* suffix is longer than DN */
656 * input DN is normalized, so the separator check
657 * need not look at escaping
659 if ( len && len < dnlen &&
660 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
665 if ( strcmp( be->be_nsuffix[j].bv_val,
666 &dn->bv_val[dnlen-len] ) == 0 )
679 struct berval *bvsuffix )
683 if ( be->be_nsuffix == NULL ) {
687 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
688 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
699 struct berval *bvsubordinate )
703 if ( be->be_nsuffix == NULL ) {
707 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
708 if ( dnIsSuffix( bvsubordinate, &be->be_nsuffix[i] ) ) {
717 be_isroot_dn( Backend *be, struct berval *ndn )
719 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
723 return dn_match( &be->be_rootndn, ndn );
727 be_slurp_update( Operation *op )
729 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
730 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
734 be_shadow_update( Operation *op )
736 /* This assumes that all internal ops (connid == -1) on a syncrepl
737 * database are syncrepl operations.
739 return (( SLAP_SYNC_SHADOW( op->o_bd ) && op->o_connid == -1 ) ||
740 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
744 be_isupdate_dn( Backend *be, struct berval *ndn )
746 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
750 return dn_match( &be->be_update_ndn, ndn );
754 be_root_dn( Backend *be )
756 return &be->be_rootdn;
760 be_isroot( Operation *op )
762 return be_isroot_dn( op->o_bd, &op->o_ndn );
766 be_isroot_pw( Operation *op )
770 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
774 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
779 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
780 op->o_conn->c_sasl_authctx, NULL );
783 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
786 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
798 if ( op->o_bd->be_release ) {
799 /* free and release entry from backend */
800 return op->o_bd->be_release( op, e, rw );
809 backend_unbind( Operation *op, SlapReply *rs )
813 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
814 if ( be->be_unbind ) {
816 be->be_unbind( op, rs );
824 backend_connection_init(
829 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
830 if ( be->be_connection_init ) {
831 be->be_connection_init( be, conn );
839 backend_connection_destroy(
844 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
845 if ( be->be_connection_destroy ) {
846 be->be_connection_destroy( be, conn);
854 backend_check_controls(
858 LDAPControl **ctrls = op->o_ctrls;
859 rs->sr_err = LDAP_SUCCESS;
862 for( ; *ctrls != NULL ; ctrls++ ) {
865 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
866 case LDAP_CONTROL_NOT_FOUND:
867 /* unrecognized control */
868 if ( (*ctrls)->ldctl_iscritical ) {
869 /* should not be reachable */
870 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
871 "unrecognized critical control: %s\n",
872 (*ctrls)->ldctl_oid, 0, 0 );
875 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
876 "unrecognized non-critical control: %s\n",
877 (*ctrls)->ldctl_oid, 0, 0 );
881 case LDAP_COMPARE_FALSE:
882 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
883 /* RFC 4511 allows unavailableCriticalExtension to be
884 * returned when the server is unwilling to perform
885 * an operation extended by a recognized critical
888 rs->sr_text = "critical control unavailable in context";
889 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
894 case LDAP_COMPARE_TRUE:
899 Debug( LDAP_DEBUG_ANY,
900 "backend_check_controls: unable to check control: %s\n",
901 (*ctrls)->ldctl_oid, 0, 0 );
904 rs->sr_text = "unable to check control";
905 rs->sr_err = LDAP_OTHER;
911 #if 0 /* temporarily removed */
912 /* check should be generalized */
913 if( get_relax(op) && !be_isroot(op)) {
914 rs->sr_text = "requires manager authorization";
915 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
924 backend_check_restrictions(
927 struct berval *opdata )
929 slap_mask_t restrictops;
930 slap_mask_t requires;
932 slap_mask_t exopflag = 0;
939 int rc = SLAP_CB_CONTINUE;
941 if ( op->o_bd->be_chk_controls ) {
942 rc = ( *op->o_bd->be_chk_controls )( op, rs );
945 if ( rc == SLAP_CB_CONTINUE ) {
946 rc = backend_check_controls( op, rs );
949 if ( rc != LDAP_SUCCESS ) {
953 restrictops = op->o_bd->be_restrictops;
954 requires = op->o_bd->be_requires;
955 ssf = &op->o_bd->be_ssf_set;
958 restrictops = frontendDB->be_restrictops;
959 requires = frontendDB->be_requires;
960 ssf = &frontendDB->be_ssf_set;
963 switch( op->o_tag ) {
965 opflag = SLAP_RESTRICT_OP_ADD;
969 opflag = SLAP_RESTRICT_OP_BIND;
972 case LDAP_REQ_COMPARE:
973 opflag = SLAP_RESTRICT_OP_COMPARE;
975 case LDAP_REQ_DELETE:
977 opflag = SLAP_RESTRICT_OP_DELETE;
979 case LDAP_REQ_EXTENDED:
980 opflag = SLAP_RESTRICT_OP_EXTENDED;
983 /* treat unspecified as a modify */
984 opflag = SLAP_RESTRICT_OP_MODIFY;
989 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
992 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
996 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
997 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1001 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1002 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1006 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1007 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1012 /* treat everything else as a modify */
1013 opflag = SLAP_RESTRICT_OP_MODIFY;
1017 case LDAP_REQ_MODIFY:
1019 opflag = SLAP_RESTRICT_OP_MODIFY;
1021 case LDAP_REQ_RENAME:
1023 opflag = SLAP_RESTRICT_OP_RENAME;
1025 case LDAP_REQ_SEARCH:
1026 opflag = SLAP_RESTRICT_OP_SEARCH;
1028 case LDAP_REQ_UNBIND:
1033 rs->sr_text = "restrict operations internal error";
1034 rs->sr_err = LDAP_OTHER;
1039 /* these checks don't apply to StartTLS */
1041 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1042 if( op->o_transport_ssf < ssf->sss_transport ) {
1043 rs->sr_text = op->o_transport_ssf
1044 ? "stronger transport confidentiality required"
1045 : "transport confidentiality required";
1049 if( op->o_tls_ssf < ssf->sss_tls ) {
1050 rs->sr_text = op->o_tls_ssf
1051 ? "stronger TLS confidentiality required"
1052 : "TLS confidentiality required";
1057 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1058 /* simple bind specific check */
1059 if( op->o_ssf < ssf->sss_simple_bind ) {
1060 rs->sr_text = op->o_ssf
1061 ? "stronger confidentiality required"
1062 : "confidentiality required";
1067 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1068 /* these checks don't apply to SASL bind */
1070 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1071 rs->sr_text = op->o_sasl_ssf
1072 ? "stronger SASL confidentiality required"
1073 : "SASL confidentiality required";
1077 if( op->o_ssf < ssf->sss_ssf ) {
1078 rs->sr_text = op->o_ssf
1079 ? "stronger confidentiality required"
1080 : "confidentiality required";
1086 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1087 rs->sr_text = op->o_transport_ssf
1088 ? "stronger transport confidentiality required for update"
1089 : "transport confidentiality required for update";
1093 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1094 rs->sr_text = op->o_tls_ssf
1095 ? "stronger TLS confidentiality required for update"
1096 : "TLS confidentiality required for update";
1100 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1101 rs->sr_text = op->o_sasl_ssf
1102 ? "stronger SASL confidentiality required for update"
1103 : "SASL confidentiality required for update";
1107 if( op->o_ssf < ssf->sss_update_ssf ) {
1108 rs->sr_text = op->o_ssf
1109 ? "stronger confidentiality required for update"
1110 : "confidentiality required for update";
1114 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1115 BER_BVISEMPTY( &op->o_ndn ) )
1117 rs->sr_text = "modifications require authentication";
1118 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1122 #ifdef SLAP_X_LISTENER_MOD
1123 if ( op->o_conn->c_listener &&
1124 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1125 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1127 /* no "w" mode means readonly */
1128 rs->sr_text = "modifications not allowed on this listener";
1129 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1132 #endif /* SLAP_X_LISTENER_MOD */
1137 /* these checks don't apply to Bind, StartTLS, or Unbind */
1139 if( requires & SLAP_REQUIRE_STRONG ) {
1140 /* should check mechanism */
1141 if( ( op->o_transport_ssf < ssf->sss_transport
1142 && op->o_authtype == LDAP_AUTH_SIMPLE )
1143 || BER_BVISEMPTY( &op->o_dn ) )
1145 rs->sr_text = "strong(er) authentication required";
1146 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1151 if( requires & SLAP_REQUIRE_SASL ) {
1152 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1153 rs->sr_text = "SASL authentication required";
1154 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1159 if( requires & SLAP_REQUIRE_AUTHC ) {
1160 if( BER_BVISEMPTY( &op->o_dn ) ) {
1161 rs->sr_text = "authentication required";
1162 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1167 if( requires & SLAP_REQUIRE_BIND ) {
1169 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1170 version = op->o_conn->c_protocol;
1171 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1174 /* no bind has occurred */
1175 rs->sr_text = "BIND required";
1176 rs->sr_err = LDAP_OPERATIONS_ERROR;
1181 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1182 if( op->o_protocol < LDAP_VERSION3 ) {
1183 /* no bind has occurred */
1184 rs->sr_text = "operation restricted to LDAPv3 clients";
1185 rs->sr_err = LDAP_OPERATIONS_ERROR;
1190 #ifdef SLAP_X_LISTENER_MOD
1191 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1192 if ( op->o_conn->c_listener &&
1193 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1195 /* no "x" mode means bind required */
1196 rs->sr_text = "bind required on this listener";
1197 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1202 if ( !starttls && !updateop ) {
1203 if ( op->o_conn->c_listener &&
1204 !( op->o_conn->c_listener->sl_perms &
1205 ( !BER_BVISEMPTY( &op->o_dn )
1206 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1208 /* no "r" mode means no read */
1209 rs->sr_text = "read not allowed on this listener";
1210 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1214 #endif /* SLAP_X_LISTENER_MOD */
1218 if( ( restrictops & opflag )
1219 || ( exopflag && ( restrictops & exopflag ) ) ) {
1220 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1221 rs->sr_text = "read operations restricted";
1222 } else if ( restrictops & exopflag ) {
1223 rs->sr_text = "extended operation restricted";
1225 rs->sr_text = "operation restricted";
1227 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1231 rs->sr_err = LDAP_SUCCESS;
1235 int backend_check_referrals( Operation *op, SlapReply *rs )
1237 rs->sr_err = LDAP_SUCCESS;
1239 if( op->o_bd->be_chk_referrals ) {
1240 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1242 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1243 send_ldap_result( op, rs );
1255 AttributeDescription *at,
1261 if ( op->o_bd == NULL ) {
1262 return LDAP_NO_SUCH_OBJECT;
1265 if ( op->o_bd->be_fetch ) {
1266 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1269 return LDAP_UNWILLING_TO_PERFORM;
1276 struct berval *gr_ndn,
1277 struct berval *op_ndn,
1278 ObjectClass *group_oc,
1279 AttributeDescription *group_at )
1282 void *o_priv = op->o_private, *e_priv = NULL;
1286 Backend *be = op->o_bd;
1288 op->o_bd = select_backend( gr_ndn, 0 );
1290 for ( g = op->o_groups; g; g = g->ga_next ) {
1291 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1292 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1296 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1306 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1311 op->o_private = NULL;
1312 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1313 e_priv = op->o_private;
1314 op->o_private = o_priv;
1318 a = attr_find( e->e_attrs, group_at );
1320 /* If the attribute is a subtype of labeledURI,
1321 * treat this as a dynamic group ala groupOfURLs
1323 if ( is_at_subtype( group_at->ad_type,
1324 slap_schema.si_ad_labeledURI->ad_type ) )
1328 struct berval bv, nbase;
1331 void *user_priv = NULL;
1332 Backend *b2 = op->o_bd;
1334 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1338 rc = LDAP_COMPARE_FALSE;
1339 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1340 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1346 BER_BVZERO( &nbase );
1348 /* host, attrs and extensions parts must be empty */
1349 if ( ( ludp->lud_host && *ludp->lud_host )
1356 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1357 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1358 op->o_tmpmemctx ) != LDAP_SUCCESS )
1363 switch ( ludp->lud_scope ) {
1364 case LDAP_SCOPE_BASE:
1365 if ( !dn_match( &nbase, op_ndn ) ) {
1369 case LDAP_SCOPE_ONELEVEL:
1370 dnParent( op_ndn, &bv );
1371 if ( !dn_match( &nbase, &bv ) ) {
1375 case LDAP_SCOPE_SUBTREE:
1376 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1380 case LDAP_SCOPE_SUBORDINATE:
1381 if ( dn_match( &nbase, op_ndn ) ||
1382 !dnIsSuffix( op_ndn, &nbase ) )
1388 /* NOTE: this could be NULL
1389 * if no filter is provided,
1390 * or if filter parsing fails.
1391 * In the latter case,
1392 * we should give up. */
1393 if ( ludp->lud_filter != NULL && ludp->lud_filter != '\0') {
1394 filter = str2filter_x( op, ludp->lud_filter );
1395 if ( filter == NULL ) {
1401 /* only get user if required
1402 * and not available yet */
1403 if ( user == NULL ) {
1406 op->o_bd = select_backend( op_ndn, 0 );
1407 op->o_private = NULL;
1408 rc2 = be_entry_get_rw( op, op_ndn, NULL, NULL, 0, &user );
1409 user_priv = op->o_private;
1410 op->o_private = o_priv;
1418 if ( test_filter( NULL, user, filter ) ==
1423 filter_free_x( op, filter );
1426 ldap_free_urldesc( ludp );
1427 if ( !BER_BVISNULL( &nbase ) ) {
1428 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1430 if ( rc != LDAP_COMPARE_FALSE ) {
1435 if ( user != NULL && user != target ) {
1436 op->o_private = user_priv;
1437 be_entry_release_r( op, user );
1438 op->o_private = o_priv;
1443 rc = value_find_ex( group_at,
1444 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1445 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1446 a->a_nvals, op_ndn, op->o_tmpmemctx );
1447 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
1448 rc = LDAP_COMPARE_FALSE;
1453 rc = LDAP_NO_SUCH_ATTRIBUTE;
1456 if ( e != target ) {
1457 op->o_private = e_priv;
1458 be_entry_release_r( op, e );
1459 op->o_private = o_priv;
1463 rc = LDAP_NO_SUCH_OBJECT;
1466 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1467 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1469 g->ga_be = op->o_bd;
1470 g->ga_oc = group_oc;
1471 g->ga_at = group_at;
1473 g->ga_len = gr_ndn->bv_len;
1474 strcpy( g->ga_ndn, gr_ndn->bv_val );
1475 g->ga_next = op->o_groups;
1488 struct berval *gr_ndn,
1489 struct berval *op_ndn,
1490 ObjectClass *group_oc,
1491 AttributeDescription *group_at )
1496 if ( op->o_abandon ) {
1497 return SLAPD_ABANDON;
1501 op->o_bd = frontendDB;
1502 rc = frontendDB->be_group( op, target, gr_ndn,
1503 op_ndn, group_oc, group_at );
1514 AttributeDescription *entry_at,
1516 slap_access_t access )
1519 void *o_priv = op->o_private, *e_priv = NULL;
1520 Attribute *a = NULL;
1521 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1522 AccessControlState acl_state = ACL_STATE_INIT;
1523 Backend *be = op->o_bd;
1525 op->o_bd = select_backend( edn, 0 );
1527 if ( target && dn_match( &target->e_nname, edn ) ) {
1531 op->o_private = NULL;
1532 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1533 e_priv = op->o_private;
1534 op->o_private = o_priv;
1538 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1539 assert( vals == NULL );
1542 if ( op->o_conn && access > ACL_NONE &&
1543 access_allowed( op, e, entry_at, NULL,
1544 access, &acl_state ) == 0 )
1546 rc = LDAP_INSUFFICIENT_ACCESS;
1551 a = attr_find( e->e_attrs, entry_at );
1553 SlapReply rs = { 0 };
1554 AttributeName anlist[ 2 ];
1556 anlist[ 0 ].an_name = entry_at->ad_cname;
1557 anlist[ 0 ].an_desc = entry_at;
1558 BER_BVZERO( &anlist[ 1 ].an_name );
1559 rs.sr_attrs = anlist;
1561 /* NOTE: backend_operational() is also called
1562 * when returning results, so it's supposed
1563 * to do no harm to entries */
1565 rc = backend_operational( op, &rs );
1568 if ( rc == LDAP_SUCCESS ) {
1569 if ( rs.sr_operational_attrs ) {
1571 a = rs.sr_operational_attrs;
1574 rc = LDAP_NO_SUCH_ATTRIBUTE;
1582 if ( op->o_conn && access > ACL_NONE &&
1583 access_allowed( op, e, entry_at, NULL,
1584 access, &acl_state ) == 0 )
1586 rc = LDAP_INSUFFICIENT_ACCESS;
1590 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1593 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1595 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1597 if ( op->o_conn && access > ACL_NONE &&
1598 access_allowed( op, e, entry_at,
1605 ber_dupbv_x( &v[j], &a->a_nvals[i],
1607 if ( !BER_BVISNULL( &v[j] ) ) {
1612 op->o_tmpfree( v, op->o_tmpmemctx );
1614 rc = LDAP_INSUFFICIENT_ACCESS;
1617 BER_BVZERO( &v[j] );
1622 freeit: if ( e != target ) {
1623 op->o_private = e_priv;
1624 be_entry_release_r( op, e );
1625 op->o_private = o_priv;
1641 AttributeDescription *entry_at,
1643 slap_access_t access )
1649 op->o_bd = frontendDB;
1650 rc = frontendDB->be_attribute( op, target, edn,
1651 entry_at, vals, access );
1662 AttributeDescription *entry_at,
1663 struct berval *nval,
1664 slap_access_t access,
1668 void *o_priv = op->o_private, *e_priv = NULL;
1669 int rc = LDAP_INSUFFICIENT_ACCESS;
1670 Backend *be = op->o_bd;
1673 assert( op != NULL );
1674 assert( op->o_conn != NULL );
1675 assert( edn != NULL );
1676 assert( access > ACL_NONE );
1678 op->o_bd = select_backend( edn, 0 );
1680 if ( target && dn_match( &target->e_nname, edn ) ) {
1684 op->o_private = NULL;
1685 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1686 e_priv = op->o_private;
1687 op->o_private = o_priv;
1691 Attribute *a = NULL;
1694 if ( entry_at == NULL ) {
1695 entry_at = slap_schema.si_ad_entry;
1698 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1700 if ( access_allowed_mask( op, e, entry_at,
1701 NULL, access, NULL, mask ) == 0 )
1703 rc = LDAP_INSUFFICIENT_ACCESS;
1710 a = attr_find( e->e_attrs, entry_at );
1712 SlapReply rs = { 0 };
1713 AttributeName anlist[ 2 ];
1715 anlist[ 0 ].an_name = entry_at->ad_cname;
1716 anlist[ 0 ].an_desc = entry_at;
1717 BER_BVZERO( &anlist[ 1 ].an_name );
1718 rs.sr_attrs = anlist;
1720 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1722 /* NOTE: backend_operational() is also called
1723 * when returning results, so it's supposed
1724 * to do no harm to entries */
1726 rc = backend_operational( op, &rs );
1729 if ( rc == LDAP_SUCCESS ) {
1730 if ( rs.sr_operational_attrs ) {
1732 a = rs.sr_operational_attrs;
1735 rc = LDAP_NO_SUCH_OBJECT;
1741 if ( access_allowed_mask( op, e, entry_at,
1742 nval, access, NULL, mask ) == 0 )
1744 rc = LDAP_INSUFFICIENT_ACCESS;
1750 freeit: if ( e != target ) {
1751 op->o_private = e_priv;
1752 be_entry_release_r( op, e );
1753 op->o_private = o_priv;
1772 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1773 /* just count them */ ;
1776 * If operational attributes (allegedly) are required,
1777 * and the backend supports specific operational attributes,
1778 * add them to the attribute list
1780 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1781 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1782 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1784 *ap = slap_operational_entryDN( rs->sr_entry );
1785 ap = &(*ap)->a_next;
1788 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1789 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1790 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1792 *ap = slap_operational_subschemaSubentry( op->o_bd );
1793 ap = &(*ap)->a_next;
1796 if ( op->o_bd != NULL ) {
1797 BackendDB *be_orig = op->o_bd;
1799 /* Let the overlays have a chance at this */
1800 op->o_bd = select_backend( &op->o_req_ndn, 0 );
1801 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1802 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1803 op->o_bd->be_operational != NULL )
1805 rc = op->o_bd->be_operational( op, rs );
1813 int backend_operational( Operation *op, SlapReply *rs )
1818 /* Moved this into the frontend so global overlays are called */
1821 op->o_bd = frontendDB;
1822 rc = frontendDB->be_operational( op, rs );