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 )
768 return be_rootdn_bind( op, NULL ) == LDAP_SUCCESS;
772 * checks if binding as rootdn
775 * SLAP_CB_CONTINUE if not the rootdn
776 * LDAP_SUCCESS if rootdn & rootpw
777 * LDAP_INVALID_CREDENTIALS if rootdn & !rootpw
780 * if LDAP_SUCCESS, op->orb_edn is set
781 * if LDAP_INVALID_CREDENTIALS, response is sent to client
784 be_rootdn_bind( Operation *op, SlapReply *rs )
788 assert( op->o_tag == LDAP_REQ_BIND );
789 assert( op->orb_method == LDAP_AUTH_SIMPLE );
791 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
792 return SLAP_CB_CONTINUE;
795 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
796 rc = LDAP_INVALID_CREDENTIALS;
805 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
806 op->o_conn->c_sasl_authctx, NULL );
809 rc = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
812 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
815 rc = ( rc == 0 ? LDAP_SUCCESS : LDAP_INVALID_CREDENTIALS );
820 Debug( LDAP_DEBUG_TRACE, "%s: rootdn=\"%s\" bind%s\n",
821 op->o_log_prefix, op->o_bd->be_rootdn.bv_val,
822 rc == LDAP_SUCCESS ? " succeeded" : " failed" );
824 if ( rc == LDAP_SUCCESS ) {
825 /* Set to the pretty rootdn */
826 ber_dupbv( &op->orb_edn, &op->o_bd->be_rootdn );
829 send_ldap_result( op, rs );
842 if ( op->o_bd->be_release ) {
843 /* free and release entry from backend */
844 return op->o_bd->be_release( op, e, rw );
853 backend_unbind( Operation *op, SlapReply *rs )
857 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
858 if ( be->be_unbind ) {
860 be->be_unbind( op, rs );
868 backend_connection_init(
873 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
874 if ( be->be_connection_init ) {
875 be->be_connection_init( be, conn );
883 backend_connection_destroy(
888 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
889 if ( be->be_connection_destroy ) {
890 be->be_connection_destroy( be, conn);
898 backend_check_controls(
902 LDAPControl **ctrls = op->o_ctrls;
903 rs->sr_err = LDAP_SUCCESS;
906 for( ; *ctrls != NULL ; ctrls++ ) {
909 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
910 case LDAP_CONTROL_NOT_FOUND:
911 /* unrecognized control */
912 if ( (*ctrls)->ldctl_iscritical ) {
913 /* should not be reachable */
914 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
915 "unrecognized critical control: %s\n",
916 (*ctrls)->ldctl_oid, 0, 0 );
919 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
920 "unrecognized non-critical control: %s\n",
921 (*ctrls)->ldctl_oid, 0, 0 );
925 case LDAP_COMPARE_FALSE:
926 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
927 /* RFC 4511 allows unavailableCriticalExtension to be
928 * returned when the server is unwilling to perform
929 * an operation extended by a recognized critical
932 rs->sr_text = "critical control unavailable in context";
933 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
938 case LDAP_COMPARE_TRUE:
943 Debug( LDAP_DEBUG_ANY,
944 "backend_check_controls: unable to check control: %s\n",
945 (*ctrls)->ldctl_oid, 0, 0 );
948 rs->sr_text = "unable to check control";
949 rs->sr_err = LDAP_OTHER;
955 #if 0 /* temporarily removed */
956 /* check should be generalized */
957 if( get_relax(op) && !be_isroot(op)) {
958 rs->sr_text = "requires manager authorization";
959 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
968 backend_check_restrictions(
971 struct berval *opdata )
973 slap_mask_t restrictops;
974 slap_mask_t requires;
976 slap_mask_t exopflag = 0;
983 int rc = SLAP_CB_CONTINUE;
985 if ( op->o_bd->be_chk_controls ) {
986 rc = ( *op->o_bd->be_chk_controls )( op, rs );
989 if ( rc == SLAP_CB_CONTINUE ) {
990 rc = backend_check_controls( op, rs );
993 if ( rc != LDAP_SUCCESS ) {
997 restrictops = op->o_bd->be_restrictops;
998 requires = op->o_bd->be_requires;
999 ssf = &op->o_bd->be_ssf_set;
1002 restrictops = frontendDB->be_restrictops;
1003 requires = frontendDB->be_requires;
1004 ssf = &frontendDB->be_ssf_set;
1007 switch( op->o_tag ) {
1009 opflag = SLAP_RESTRICT_OP_ADD;
1013 opflag = SLAP_RESTRICT_OP_BIND;
1016 case LDAP_REQ_COMPARE:
1017 opflag = SLAP_RESTRICT_OP_COMPARE;
1019 case LDAP_REQ_DELETE:
1021 opflag = SLAP_RESTRICT_OP_DELETE;
1023 case LDAP_REQ_EXTENDED:
1024 opflag = SLAP_RESTRICT_OP_EXTENDED;
1027 /* treat unspecified as a modify */
1028 opflag = SLAP_RESTRICT_OP_MODIFY;
1033 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1036 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1040 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1041 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1045 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1046 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1050 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1051 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1056 /* treat everything else as a modify */
1057 opflag = SLAP_RESTRICT_OP_MODIFY;
1061 case LDAP_REQ_MODIFY:
1063 opflag = SLAP_RESTRICT_OP_MODIFY;
1065 case LDAP_REQ_RENAME:
1067 opflag = SLAP_RESTRICT_OP_RENAME;
1069 case LDAP_REQ_SEARCH:
1070 opflag = SLAP_RESTRICT_OP_SEARCH;
1072 case LDAP_REQ_UNBIND:
1077 rs->sr_text = "restrict operations internal error";
1078 rs->sr_err = LDAP_OTHER;
1083 /* these checks don't apply to StartTLS */
1085 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1086 if( op->o_transport_ssf < ssf->sss_transport ) {
1087 rs->sr_text = op->o_transport_ssf
1088 ? "stronger transport confidentiality required"
1089 : "transport confidentiality required";
1093 if( op->o_tls_ssf < ssf->sss_tls ) {
1094 rs->sr_text = op->o_tls_ssf
1095 ? "stronger TLS confidentiality required"
1096 : "TLS confidentiality required";
1101 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1102 /* simple bind specific check */
1103 if( op->o_ssf < ssf->sss_simple_bind ) {
1104 rs->sr_text = op->o_ssf
1105 ? "stronger confidentiality required"
1106 : "confidentiality required";
1111 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1112 /* these checks don't apply to SASL bind */
1114 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1115 rs->sr_text = op->o_sasl_ssf
1116 ? "stronger SASL confidentiality required"
1117 : "SASL confidentiality required";
1121 if( op->o_ssf < ssf->sss_ssf ) {
1122 rs->sr_text = op->o_ssf
1123 ? "stronger confidentiality required"
1124 : "confidentiality required";
1130 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1131 rs->sr_text = op->o_transport_ssf
1132 ? "stronger transport confidentiality required for update"
1133 : "transport confidentiality required for update";
1137 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1138 rs->sr_text = op->o_tls_ssf
1139 ? "stronger TLS confidentiality required for update"
1140 : "TLS confidentiality required for update";
1144 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1145 rs->sr_text = op->o_sasl_ssf
1146 ? "stronger SASL confidentiality required for update"
1147 : "SASL confidentiality required for update";
1151 if( op->o_ssf < ssf->sss_update_ssf ) {
1152 rs->sr_text = op->o_ssf
1153 ? "stronger confidentiality required for update"
1154 : "confidentiality required for update";
1158 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1159 BER_BVISEMPTY( &op->o_ndn ) )
1161 rs->sr_text = "modifications require authentication";
1162 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1166 #ifdef SLAP_X_LISTENER_MOD
1167 if ( op->o_conn->c_listener &&
1168 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1169 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1171 /* no "w" mode means readonly */
1172 rs->sr_text = "modifications not allowed on this listener";
1173 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1176 #endif /* SLAP_X_LISTENER_MOD */
1181 /* these checks don't apply to Bind, StartTLS, or Unbind */
1183 if( requires & SLAP_REQUIRE_STRONG ) {
1184 /* should check mechanism */
1185 if( ( op->o_transport_ssf < ssf->sss_transport
1186 && op->o_authtype == LDAP_AUTH_SIMPLE )
1187 || BER_BVISEMPTY( &op->o_dn ) )
1189 rs->sr_text = "strong(er) authentication required";
1190 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1195 if( requires & SLAP_REQUIRE_SASL ) {
1196 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1197 rs->sr_text = "SASL authentication required";
1198 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1203 if( requires & SLAP_REQUIRE_AUTHC ) {
1204 if( BER_BVISEMPTY( &op->o_dn ) ) {
1205 rs->sr_text = "authentication required";
1206 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1211 if( requires & SLAP_REQUIRE_BIND ) {
1213 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1214 version = op->o_conn->c_protocol;
1215 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1218 /* no bind has occurred */
1219 rs->sr_text = "BIND required";
1220 rs->sr_err = LDAP_OPERATIONS_ERROR;
1225 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1226 if( op->o_protocol < LDAP_VERSION3 ) {
1227 /* no bind has occurred */
1228 rs->sr_text = "operation restricted to LDAPv3 clients";
1229 rs->sr_err = LDAP_OPERATIONS_ERROR;
1234 #ifdef SLAP_X_LISTENER_MOD
1235 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1236 if ( op->o_conn->c_listener &&
1237 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1239 /* no "x" mode means bind required */
1240 rs->sr_text = "bind required on this listener";
1241 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1246 if ( !starttls && !updateop ) {
1247 if ( op->o_conn->c_listener &&
1248 !( op->o_conn->c_listener->sl_perms &
1249 ( !BER_BVISEMPTY( &op->o_dn )
1250 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1252 /* no "r" mode means no read */
1253 rs->sr_text = "read not allowed on this listener";
1254 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1258 #endif /* SLAP_X_LISTENER_MOD */
1262 if( ( restrictops & opflag )
1263 || ( exopflag && ( restrictops & exopflag ) ) ) {
1264 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1265 rs->sr_text = "read operations restricted";
1266 } else if ( restrictops & exopflag ) {
1267 rs->sr_text = "extended operation restricted";
1269 rs->sr_text = "operation restricted";
1271 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1275 rs->sr_err = LDAP_SUCCESS;
1279 int backend_check_referrals( Operation *op, SlapReply *rs )
1281 rs->sr_err = LDAP_SUCCESS;
1283 if( op->o_bd->be_chk_referrals ) {
1284 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1286 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1287 send_ldap_result( op, rs );
1299 AttributeDescription *at,
1305 if ( op->o_bd == NULL ) {
1306 return LDAP_NO_SUCH_OBJECT;
1309 if ( op->o_bd->be_fetch ) {
1310 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1313 return LDAP_UNWILLING_TO_PERFORM;
1320 struct berval *gr_ndn,
1321 struct berval *op_ndn,
1322 ObjectClass *group_oc,
1323 AttributeDescription *group_at )
1326 void *o_priv = op->o_private, *e_priv = NULL;
1330 Backend *be = op->o_bd;
1332 op->o_bd = select_backend( gr_ndn, 0 );
1334 for ( g = op->o_groups; g; g = g->ga_next ) {
1335 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1336 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1340 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1350 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1355 op->o_private = NULL;
1356 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1357 e_priv = op->o_private;
1358 op->o_private = o_priv;
1362 a = attr_find( e->e_attrs, group_at );
1364 /* If the attribute is a subtype of labeledURI,
1365 * treat this as a dynamic group ala groupOfURLs
1367 if ( is_at_subtype( group_at->ad_type,
1368 slap_schema.si_ad_labeledURI->ad_type ) )
1372 struct berval bv, nbase;
1375 void *user_priv = NULL;
1376 Backend *b2 = op->o_bd;
1378 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1382 rc = LDAP_COMPARE_FALSE;
1383 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1384 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1390 BER_BVZERO( &nbase );
1392 /* host, attrs and extensions parts must be empty */
1393 if ( ( ludp->lud_host && *ludp->lud_host )
1400 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1401 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1402 op->o_tmpmemctx ) != LDAP_SUCCESS )
1407 switch ( ludp->lud_scope ) {
1408 case LDAP_SCOPE_BASE:
1409 if ( !dn_match( &nbase, op_ndn ) ) {
1413 case LDAP_SCOPE_ONELEVEL:
1414 dnParent( op_ndn, &bv );
1415 if ( !dn_match( &nbase, &bv ) ) {
1419 case LDAP_SCOPE_SUBTREE:
1420 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1424 case LDAP_SCOPE_SUBORDINATE:
1425 if ( dn_match( &nbase, op_ndn ) ||
1426 !dnIsSuffix( op_ndn, &nbase ) )
1432 /* NOTE: this could be NULL
1433 * if no filter is provided,
1434 * or if filter parsing fails.
1435 * In the latter case,
1436 * we should give up. */
1437 if ( ludp->lud_filter != NULL && ludp->lud_filter != '\0') {
1438 filter = str2filter_x( op, ludp->lud_filter );
1439 if ( filter == NULL ) {
1445 /* only get user if required
1446 * and not available yet */
1447 if ( user == NULL ) {
1450 op->o_bd = select_backend( op_ndn, 0 );
1451 op->o_private = NULL;
1452 rc2 = be_entry_get_rw( op, op_ndn, NULL, NULL, 0, &user );
1453 user_priv = op->o_private;
1454 op->o_private = o_priv;
1462 if ( test_filter( NULL, user, filter ) ==
1467 filter_free_x( op, filter );
1470 ldap_free_urldesc( ludp );
1471 if ( !BER_BVISNULL( &nbase ) ) {
1472 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1474 if ( rc != LDAP_COMPARE_FALSE ) {
1479 if ( user != NULL && user != target ) {
1480 op->o_private = user_priv;
1481 be_entry_release_r( op, user );
1482 op->o_private = o_priv;
1487 rc = value_find_ex( group_at,
1488 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1489 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1490 a->a_nvals, op_ndn, op->o_tmpmemctx );
1491 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
1492 rc = LDAP_COMPARE_FALSE;
1497 rc = LDAP_NO_SUCH_ATTRIBUTE;
1500 if ( e != target ) {
1501 op->o_private = e_priv;
1502 be_entry_release_r( op, e );
1503 op->o_private = o_priv;
1507 rc = LDAP_NO_SUCH_OBJECT;
1510 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1511 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1513 g->ga_be = op->o_bd;
1514 g->ga_oc = group_oc;
1515 g->ga_at = group_at;
1517 g->ga_len = gr_ndn->bv_len;
1518 strcpy( g->ga_ndn, gr_ndn->bv_val );
1519 g->ga_next = op->o_groups;
1532 struct berval *gr_ndn,
1533 struct berval *op_ndn,
1534 ObjectClass *group_oc,
1535 AttributeDescription *group_at )
1540 if ( op->o_abandon ) {
1541 return SLAPD_ABANDON;
1545 op->o_bd = frontendDB;
1546 rc = frontendDB->be_group( op, target, gr_ndn,
1547 op_ndn, group_oc, group_at );
1558 AttributeDescription *entry_at,
1560 slap_access_t access )
1563 void *o_priv = op->o_private, *e_priv = NULL;
1564 Attribute *a = NULL;
1565 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1566 AccessControlState acl_state = ACL_STATE_INIT;
1567 Backend *be = op->o_bd;
1569 op->o_bd = select_backend( edn, 0 );
1571 if ( target && dn_match( &target->e_nname, edn ) ) {
1575 op->o_private = NULL;
1576 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1577 e_priv = op->o_private;
1578 op->o_private = o_priv;
1582 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1583 assert( vals == NULL );
1586 if ( op->o_conn && access > ACL_NONE &&
1587 access_allowed( op, e, entry_at, NULL,
1588 access, &acl_state ) == 0 )
1590 rc = LDAP_INSUFFICIENT_ACCESS;
1595 a = attr_find( e->e_attrs, entry_at );
1597 SlapReply rs = { 0 };
1598 AttributeName anlist[ 2 ];
1600 anlist[ 0 ].an_name = entry_at->ad_cname;
1601 anlist[ 0 ].an_desc = entry_at;
1602 BER_BVZERO( &anlist[ 1 ].an_name );
1603 rs.sr_attrs = anlist;
1605 /* NOTE: backend_operational() is also called
1606 * when returning results, so it's supposed
1607 * to do no harm to entries */
1609 rc = backend_operational( op, &rs );
1612 if ( rc == LDAP_SUCCESS ) {
1613 if ( rs.sr_operational_attrs ) {
1615 a = rs.sr_operational_attrs;
1618 rc = LDAP_NO_SUCH_ATTRIBUTE;
1626 if ( op->o_conn && access > ACL_NONE &&
1627 access_allowed( op, e, entry_at, NULL,
1628 access, &acl_state ) == 0 )
1630 rc = LDAP_INSUFFICIENT_ACCESS;
1634 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1637 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1639 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1641 if ( op->o_conn && access > ACL_NONE &&
1642 access_allowed( op, e, entry_at,
1649 ber_dupbv_x( &v[j], &a->a_nvals[i],
1651 if ( !BER_BVISNULL( &v[j] ) ) {
1656 op->o_tmpfree( v, op->o_tmpmemctx );
1658 rc = LDAP_INSUFFICIENT_ACCESS;
1661 BER_BVZERO( &v[j] );
1666 freeit: if ( e != target ) {
1667 op->o_private = e_priv;
1668 be_entry_release_r( op, e );
1669 op->o_private = o_priv;
1685 AttributeDescription *entry_at,
1687 slap_access_t access )
1693 op->o_bd = frontendDB;
1694 rc = frontendDB->be_attribute( op, target, edn,
1695 entry_at, vals, access );
1706 AttributeDescription *entry_at,
1707 struct berval *nval,
1708 slap_access_t access,
1712 void *o_priv = op->o_private, *e_priv = NULL;
1713 int rc = LDAP_INSUFFICIENT_ACCESS;
1714 Backend *be = op->o_bd;
1717 assert( op != NULL );
1718 assert( op->o_conn != NULL );
1719 assert( edn != NULL );
1720 assert( access > ACL_NONE );
1722 op->o_bd = select_backend( edn, 0 );
1724 if ( target && dn_match( &target->e_nname, edn ) ) {
1728 op->o_private = NULL;
1729 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1730 e_priv = op->o_private;
1731 op->o_private = o_priv;
1735 Attribute *a = NULL;
1738 if ( entry_at == NULL ) {
1739 entry_at = slap_schema.si_ad_entry;
1742 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1744 if ( access_allowed_mask( op, e, entry_at,
1745 NULL, access, NULL, mask ) == 0 )
1747 rc = LDAP_INSUFFICIENT_ACCESS;
1754 a = attr_find( e->e_attrs, entry_at );
1756 SlapReply rs = { 0 };
1757 AttributeName anlist[ 2 ];
1759 anlist[ 0 ].an_name = entry_at->ad_cname;
1760 anlist[ 0 ].an_desc = entry_at;
1761 BER_BVZERO( &anlist[ 1 ].an_name );
1762 rs.sr_attrs = anlist;
1764 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1766 /* NOTE: backend_operational() is also called
1767 * when returning results, so it's supposed
1768 * to do no harm to entries */
1770 rc = backend_operational( op, &rs );
1773 if ( rc == LDAP_SUCCESS ) {
1774 if ( rs.sr_operational_attrs ) {
1776 a = rs.sr_operational_attrs;
1779 rc = LDAP_NO_SUCH_OBJECT;
1785 if ( access_allowed_mask( op, e, entry_at,
1786 nval, access, NULL, mask ) == 0 )
1788 rc = LDAP_INSUFFICIENT_ACCESS;
1794 freeit: if ( e != target ) {
1795 op->o_private = e_priv;
1796 be_entry_release_r( op, e );
1797 op->o_private = o_priv;
1816 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1817 /* just count them */ ;
1820 * If operational attributes (allegedly) are required,
1821 * and the backend supports specific operational attributes,
1822 * add them to the attribute list
1824 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1825 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1826 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1828 *ap = slap_operational_entryDN( rs->sr_entry );
1829 ap = &(*ap)->a_next;
1832 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1833 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1834 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1836 *ap = slap_operational_subschemaSubentry( op->o_bd );
1837 ap = &(*ap)->a_next;
1840 if ( op->o_bd != NULL ) {
1841 BackendDB *be_orig = op->o_bd;
1843 /* Let the overlays have a chance at this */
1844 op->o_bd = select_backend( &op->o_req_ndn, 0 );
1845 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1846 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1847 op->o_bd->be_operational != NULL )
1849 rc = op->o_bd->be_operational( op, rs );
1857 int backend_operational( Operation *op, SlapReply *rs )
1862 /* Moved this into the frontend so global overlays are called */
1865 op->o_bd = frontendDB;
1866 rc = frontendDB->be_operational( op, rs );