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-2013 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 );
213 if ( !BER_BVISEMPTY( &be->be_rootndn )
214 && select_backend( &be->be_rootndn, 0 ) == be
215 && BER_BVISNULL( &be->be_rootpw ) )
217 /* warning: if rootdn entry is created,
218 * it can take rootdn privileges;
219 * set empty rootpw to prevent */
223 if ( be->bd_info->bi_db_open ) {
224 rc = be->bd_info->bi_db_open( be, cr );
226 (void)backend_set_controls( be );
229 char *type = be->bd_info->bi_type;
230 char *suffix = "(null)";
232 if ( overlay_is_over( be ) ) {
233 slap_overinfo *oi = (slap_overinfo *)be->bd_info->bi_private;
234 type = oi->oi_orig->bi_type;
237 if ( be->be_suffix != NULL && !BER_BVISNULL( &be->be_suffix[0] ) ) {
238 suffix = be->be_suffix[0].bv_val;
241 Debug( LDAP_DEBUG_ANY,
242 "backend_startup_one (type=%s, suffix=\"%s\"): "
243 "bi_db_open failed! (%d)\n",
251 int backend_startup(Backend *be)
256 ConfigReply cr={0, ""};
258 if( ! ( nBackendDB > 0 ) ) {
260 Debug( LDAP_DEBUG_ANY,
261 "backend_startup: %d databases to startup.\n",
267 /* silent noop if disabled */
268 if ( SLAP_DBDISABLED( be ))
270 if ( be->bd_info->bi_open ) {
271 rc = be->bd_info->bi_open( be->bd_info );
273 Debug( LDAP_DEBUG_ANY,
274 "backend_startup: bi_open failed!\n",
281 return backend_startup_one( be, &cr );
284 /* open frontend, if required */
285 if ( frontendDB->bd_info->bi_db_open ) {
286 rc = frontendDB->bd_info->bi_db_open( frontendDB, &cr );
288 Debug( LDAP_DEBUG_ANY,
289 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
295 /* open each backend type */
297 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
299 if( bi->bi_nDB == 0) {
300 /* no database of this type, don't open */
305 rc = bi->bi_open( bi );
307 Debug( LDAP_DEBUG_ANY,
308 "backend_startup: bi_open %d (%s) failed!\n",
314 (void)backend_init_controls( bi );
317 /* open each backend database */
319 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
321 if ( SLAP_DBDISABLED( be ))
323 if ( be->be_suffix == NULL ) {
324 Debug( LDAP_DEBUG_ANY,
325 "backend_startup: warning, database %d (%s) "
327 i, be->bd_info->bi_type, 0 );
330 rc = backend_startup_one( be, &cr );
338 int backend_num( Backend *be )
343 if( be == NULL ) return -1;
345 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
346 if( be == b2 ) return i;
352 int backend_shutdown( Backend *be )
358 /* shutdown a specific backend database */
360 if ( be->bd_info->bi_nDB == 0 ) {
361 /* no database of this type, we never opened it */
365 if ( be->bd_info->bi_db_close ) {
366 rc = be->bd_info->bi_db_close( be, NULL );
370 if( be->bd_info->bi_close ) {
371 rc = be->bd_info->bi_close( be->bd_info );
378 /* close each backend database */
379 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
380 if ( SLAP_DBDISABLED( be ))
382 if ( be->bd_info->bi_db_close ) {
383 be->bd_info->bi_db_close( be, NULL );
387 Debug( LDAP_DEBUG_ANY,
388 "backend_close: bi_db_close %s failed!\n",
393 /* close each backend type */
394 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
395 if( bi->bi_nDB == 0 ) {
396 /* no database of this type */
405 /* close frontend, if required */
406 if ( frontendDB->bd_info->bi_db_close ) {
407 rc = frontendDB->bd_info->bi_db_close ( frontendDB, NULL );
409 Debug( LDAP_DEBUG_ANY,
410 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
419 * This function is supposed to be the exact counterpart
420 * of backend_startup_one(), although this one calls bi_db_destroy()
421 * while backend_startup_one() calls bi_db_open().
423 * Make sure backend_stopdown_one() destroys resources allocated
424 * by backend_startup_one(); only call backend_destroy_one() when
425 * all stuff in a BackendDB needs to be destroyed
428 backend_stopdown_one( BackendDB *bd )
430 if ( bd->be_pending_csn_list ) {
431 struct slap_csn_entry *csne;
432 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
434 struct slap_csn_entry *tmp_csne = csne;
436 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
437 ch_free( csne->ce_csn.bv_val );
438 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
441 ch_free( bd->be_pending_csn_list );
444 if ( bd->bd_info->bi_db_destroy ) {
445 bd->bd_info->bi_db_destroy( bd, NULL );
449 void backend_destroy_one( BackendDB *bd, int dynamic )
452 LDAP_STAILQ_REMOVE(&backendDB, bd, BackendDB, be_next );
455 if ( bd->be_syncinfo ) {
456 syncinfo_free( bd->be_syncinfo, 1 );
459 backend_stopdown_one( bd );
461 ber_bvarray_free( bd->be_suffix );
462 ber_bvarray_free( bd->be_nsuffix );
463 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
464 free( bd->be_rootdn.bv_val );
466 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
467 free( bd->be_rootndn.bv_val );
469 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
470 free( bd->be_rootpw.bv_val );
472 acl_destroy( bd->be_acl );
473 limits_destroy( bd->be_limits );
474 if ( bd->be_extra_anlist ) {
475 anlist_free( bd->be_extra_anlist, 1, NULL );
477 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
478 ch_free( bd->be_update_ndn.bv_val );
480 if ( bd->be_update_refs ) {
481 ber_bvarray_free( bd->be_update_refs );
484 ldap_pvt_thread_mutex_destroy( &bd->be_pcl_mutex );
491 int backend_destroy(void)
496 /* destroy each backend database */
497 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
498 backend_destroy_one( bd, 1 );
501 /* destroy each backend type */
502 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
503 if( bi->bi_destroy ) {
504 bi->bi_destroy( bi );
509 LDAP_STAILQ_INIT(&backendInfo);
511 /* destroy frontend database */
514 if ( bd->bd_info->bi_db_destroy ) {
515 bd->bd_info->bi_db_destroy( bd, NULL );
517 ber_bvarray_free( bd->be_suffix );
518 ber_bvarray_free( bd->be_nsuffix );
519 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
520 free( bd->be_rootdn.bv_val );
522 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
523 free( bd->be_rootndn.bv_val );
525 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
526 free( bd->be_rootpw.bv_val );
528 acl_destroy( bd->be_acl );
535 BackendInfo* backend_info(const char *type)
539 /* search for the backend type */
540 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
541 if( strcasecmp(bi->bi_type, type) == 0 ) {
555 /* If idx < 0, just add to end of list */
557 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
558 } else if ( idx == 0 ) {
559 LDAP_STAILQ_INSERT_HEAD(&backendDB, be, be_next);
564 b2 = LDAP_STAILQ_FIRST(&backendDB);
566 for (i=0; i<idx; i++) {
567 b2 = LDAP_STAILQ_NEXT(b2, be_next);
569 LDAP_STAILQ_INSERT_AFTER(&backendDB, b2, be, be_next);
579 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
580 backend_db_insert(be, idx);
590 BackendInfo *bi = backend_info(type);
595 fprintf( stderr, "Unrecognized database type (%s)\n", type );
599 /* If be is provided, treat it as private. Otherwise allocate
600 * one and add it to the global list.
603 be = ch_calloc( 1, sizeof(Backend) );
605 if ( idx >= nbackends )
608 backend_db_insert( be, idx );
614 be->be_def_limit = frontendDB->be_def_limit;
615 be->be_dfltaccess = frontendDB->be_dfltaccess;
617 be->be_restrictops = frontendDB->be_restrictops;
618 be->be_requires = frontendDB->be_requires;
619 be->be_ssf_set = frontendDB->be_ssf_set;
621 ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
623 /* assign a default depth limit for alias deref */
624 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
626 if ( bi->bi_db_init ) {
627 rc = bi->bi_db_init( be, cr );
631 fprintf( stderr, "database init failed (%s)\n", type );
632 /* If we created and linked this be, remove it and free it */
634 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
635 ldap_pvt_thread_mutex_destroy( &be->be_pcl_mutex );
642 backend_init_controls( bi );
654 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
655 if ( be->bd_info->bi_db_close ) {
656 be->bd_info->bi_db_close( be, NULL );
660 if ( frontendDB->bd_info->bi_db_close ) {
661 frontendDB->bd_info->bi_db_close( frontendDB, NULL );
672 ber_len_t len, dnlen = dn->bv_len;
675 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
676 if ( be->be_nsuffix == NULL || SLAP_DBHIDDEN( be ) || SLAP_DBDISABLED( be )) {
680 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
682 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
687 len = be->be_nsuffix[j].bv_len;
690 /* suffix is longer than DN */
695 * input DN is normalized, so the separator check
696 * need not look at escaping
698 if ( len && len < dnlen &&
699 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
704 if ( strcmp( be->be_nsuffix[j].bv_val,
705 &dn->bv_val[dnlen-len] ) == 0 )
718 struct berval *bvsuffix )
722 if ( be->be_nsuffix == NULL ) {
726 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
727 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
738 struct berval *bvsubordinate )
742 if ( be->be_nsuffix == NULL ) {
746 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
747 if ( dnIsSuffix( bvsubordinate, &be->be_nsuffix[i] ) ) {
756 be_isroot_dn( Backend *be, struct berval *ndn )
758 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
762 return dn_match( &be->be_rootndn, ndn );
766 be_slurp_update( Operation *op )
768 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
769 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
773 be_shadow_update( Operation *op )
775 /* This assumes that all internal ops (connid <= -1000) on a syncrepl
776 * database are syncrepl operations.
778 return ( ( SLAP_SYNC_SHADOW( op->o_bd ) && SLAPD_SYNC_IS_SYNCCONN( op->o_connid ) ) ||
779 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
783 be_isupdate_dn( Backend *be, struct berval *ndn )
785 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
789 return dn_match( &be->be_update_ndn, ndn );
793 be_root_dn( Backend *be )
795 return &be->be_rootdn;
799 be_isroot( Operation *op )
801 return be_isroot_dn( op->o_bd, &op->o_ndn );
805 be_isroot_pw( Operation *op )
807 return be_rootdn_bind( op, NULL ) == LDAP_SUCCESS;
811 * checks if binding as rootdn
814 * SLAP_CB_CONTINUE if not the rootdn, or if rootpw is null
815 * LDAP_SUCCESS if rootdn & rootpw
816 * LDAP_INVALID_CREDENTIALS if rootdn & !rootpw
819 * if LDAP_SUCCESS, op->orb_edn is set
820 * if LDAP_INVALID_CREDENTIALS, response is sent to client
823 be_rootdn_bind( Operation *op, SlapReply *rs )
827 void *old_authctx = NULL;
830 assert( op->o_tag == LDAP_REQ_BIND );
831 assert( op->orb_method == LDAP_AUTH_SIMPLE );
833 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
834 return SLAP_CB_CONTINUE;
837 if ( BER_BVISNULL( &op->o_bd->be_rootpw ) ) {
838 /* give the database a chance */
839 return SLAP_CB_CONTINUE;
842 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
843 /* rootdn bind explicitly disallowed */
844 rc = LDAP_INVALID_CREDENTIALS;
853 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
854 op->o_conn->c_sasl_authctx, 0, &old_authctx, NULL );
857 rc = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
860 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
861 old_authctx, 0, NULL, NULL );
864 rc = ( rc == 0 ? LDAP_SUCCESS : LDAP_INVALID_CREDENTIALS );
869 Debug( LDAP_DEBUG_TRACE, "%s: rootdn=\"%s\" bind%s\n",
870 op->o_log_prefix, op->o_bd->be_rootdn.bv_val,
871 rc == LDAP_SUCCESS ? " succeeded" : " failed" );
873 if ( rc == LDAP_SUCCESS ) {
874 /* Set to the pretty rootdn */
875 ber_dupbv( &op->orb_edn, &op->o_bd->be_rootdn );
878 send_ldap_result( op, rs );
891 if ( op->o_bd->be_release ) {
892 /* free and release entry from backend */
893 return op->o_bd->be_release( op, e, rw );
902 backend_unbind( Operation *op, SlapReply *rs )
906 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
907 if ( be->be_unbind ) {
909 be->be_unbind( op, rs );
917 backend_connection_init(
922 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
923 if ( be->be_connection_init ) {
924 be->be_connection_init( be, conn );
932 backend_connection_destroy(
937 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
938 if ( be->be_connection_destroy ) {
939 be->be_connection_destroy( be, conn);
947 backend_check_controls(
951 LDAPControl **ctrls = op->o_ctrls;
952 rs->sr_err = LDAP_SUCCESS;
955 for( ; *ctrls != NULL ; ctrls++ ) {
958 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
959 case LDAP_CONTROL_NOT_FOUND:
960 /* unrecognized control */
961 if ( (*ctrls)->ldctl_iscritical ) {
962 /* should not be reachable */
963 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
964 "unrecognized critical control: %s\n",
965 (*ctrls)->ldctl_oid, 0, 0 );
968 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
969 "unrecognized non-critical control: %s\n",
970 (*ctrls)->ldctl_oid, 0, 0 );
974 case LDAP_COMPARE_FALSE:
975 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
976 #ifdef SLAP_CONTROL_X_WHATFAILED
977 if ( get_whatFailed( op ) ) {
979 oids[ 0 ] = (*ctrls)->ldctl_oid;
981 slap_ctrl_whatFailed_add( op, rs, oids );
984 /* RFC 4511 allows unavailableCriticalExtension to be
985 * returned when the server is unwilling to perform
986 * an operation extended by a recognized critical
989 rs->sr_text = "critical control unavailable in context";
990 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
995 case LDAP_COMPARE_TRUE:
1000 Debug( LDAP_DEBUG_ANY,
1001 "backend_check_controls: unable to check control: %s\n",
1002 (*ctrls)->ldctl_oid, 0, 0 );
1005 rs->sr_text = "unable to check control";
1006 rs->sr_err = LDAP_OTHER;
1012 #if 0 /* temporarily removed */
1013 /* check should be generalized */
1014 if( get_relax(op) && !be_isroot(op)) {
1015 rs->sr_text = "requires manager authorization";
1016 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1025 backend_check_restrictions(
1028 struct berval *opdata )
1030 slap_mask_t restrictops;
1031 slap_mask_t requires;
1033 slap_mask_t exopflag = 0;
1034 slap_ssf_set_t ssfs, *ssf;
1039 restrictops = frontendDB->be_restrictops;
1040 requires = frontendDB->be_requires;
1041 ssfs = frontendDB->be_ssf_set;
1045 slap_ssf_t *fssf, *bssf;
1046 int rc = SLAP_CB_CONTINUE, i;
1048 if ( op->o_bd->be_chk_controls ) {
1049 rc = ( *op->o_bd->be_chk_controls )( op, rs );
1052 if ( rc == SLAP_CB_CONTINUE ) {
1053 rc = backend_check_controls( op, rs );
1056 if ( rc != LDAP_SUCCESS ) {
1060 restrictops |= op->o_bd->be_restrictops;
1061 requires |= op->o_bd->be_requires;
1062 bssf = &op->o_bd->be_ssf_set.sss_ssf;
1063 fssf = &ssfs.sss_ssf;
1064 for ( i=0; i < (int)(sizeof(ssfs)/sizeof(slap_ssf_t)); i++ ) {
1065 if ( bssf[i] ) fssf[i] = bssf[i];
1069 switch( op->o_tag ) {
1071 opflag = SLAP_RESTRICT_OP_ADD;
1075 opflag = SLAP_RESTRICT_OP_BIND;
1078 case LDAP_REQ_COMPARE:
1079 opflag = SLAP_RESTRICT_OP_COMPARE;
1081 case LDAP_REQ_DELETE:
1083 opflag = SLAP_RESTRICT_OP_DELETE;
1085 case LDAP_REQ_EXTENDED:
1086 opflag = SLAP_RESTRICT_OP_EXTENDED;
1089 /* treat unspecified as a modify */
1090 opflag = SLAP_RESTRICT_OP_MODIFY;
1095 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1098 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1102 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1103 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1107 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1108 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1112 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1113 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1118 /* treat everything else as a modify */
1119 opflag = SLAP_RESTRICT_OP_MODIFY;
1123 case LDAP_REQ_MODIFY:
1125 opflag = SLAP_RESTRICT_OP_MODIFY;
1127 case LDAP_REQ_RENAME:
1129 opflag = SLAP_RESTRICT_OP_RENAME;
1131 case LDAP_REQ_SEARCH:
1132 opflag = SLAP_RESTRICT_OP_SEARCH;
1134 case LDAP_REQ_UNBIND:
1139 rs->sr_text = "restrict operations internal error";
1140 rs->sr_err = LDAP_OTHER;
1145 /* these checks don't apply to StartTLS */
1147 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1148 if( op->o_transport_ssf < ssf->sss_transport ) {
1149 rs->sr_text = op->o_transport_ssf
1150 ? "stronger transport confidentiality required"
1151 : "transport confidentiality required";
1155 if( op->o_tls_ssf < ssf->sss_tls ) {
1156 rs->sr_text = op->o_tls_ssf
1157 ? "stronger TLS confidentiality required"
1158 : "TLS confidentiality required";
1163 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1164 /* simple bind specific check */
1165 if( op->o_ssf < ssf->sss_simple_bind ) {
1166 rs->sr_text = op->o_ssf
1167 ? "stronger confidentiality required"
1168 : "confidentiality required";
1173 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1174 /* these checks don't apply to SASL bind */
1176 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1177 rs->sr_text = op->o_sasl_ssf
1178 ? "stronger SASL confidentiality required"
1179 : "SASL confidentiality required";
1183 if( op->o_ssf < ssf->sss_ssf ) {
1184 rs->sr_text = op->o_ssf
1185 ? "stronger confidentiality required"
1186 : "confidentiality required";
1192 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1193 rs->sr_text = op->o_transport_ssf
1194 ? "stronger transport confidentiality required for update"
1195 : "transport confidentiality required for update";
1199 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1200 rs->sr_text = op->o_tls_ssf
1201 ? "stronger TLS confidentiality required for update"
1202 : "TLS confidentiality required for update";
1206 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1207 rs->sr_text = op->o_sasl_ssf
1208 ? "stronger SASL confidentiality required for update"
1209 : "SASL confidentiality required for update";
1213 if( op->o_ssf < ssf->sss_update_ssf ) {
1214 rs->sr_text = op->o_ssf
1215 ? "stronger confidentiality required for update"
1216 : "confidentiality required for update";
1220 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1221 BER_BVISEMPTY( &op->o_ndn ) )
1223 rs->sr_text = "modifications require authentication";
1224 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1228 #ifdef SLAP_X_LISTENER_MOD
1229 if ( op->o_conn->c_listener &&
1230 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1231 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1233 /* no "w" mode means readonly */
1234 rs->sr_text = "modifications not allowed on this listener";
1235 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1238 #endif /* SLAP_X_LISTENER_MOD */
1243 /* these checks don't apply to Bind, StartTLS, or Unbind */
1245 if( requires & SLAP_REQUIRE_STRONG ) {
1246 /* should check mechanism */
1247 if( ( op->o_transport_ssf < ssf->sss_transport
1248 && op->o_authtype == LDAP_AUTH_SIMPLE )
1249 || BER_BVISEMPTY( &op->o_dn ) )
1251 rs->sr_text = "strong(er) authentication required";
1252 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1257 if( requires & SLAP_REQUIRE_SASL ) {
1258 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1259 rs->sr_text = "SASL authentication required";
1260 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1265 if( requires & SLAP_REQUIRE_AUTHC ) {
1266 if( BER_BVISEMPTY( &op->o_dn ) ) {
1267 rs->sr_text = "authentication required";
1268 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1273 if( requires & SLAP_REQUIRE_BIND ) {
1275 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1276 version = op->o_conn->c_protocol;
1277 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1280 /* no bind has occurred */
1281 rs->sr_text = "BIND required";
1282 rs->sr_err = LDAP_OPERATIONS_ERROR;
1287 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1288 if( op->o_protocol < LDAP_VERSION3 ) {
1289 /* no bind has occurred */
1290 rs->sr_text = "operation restricted to LDAPv3 clients";
1291 rs->sr_err = LDAP_OPERATIONS_ERROR;
1296 #ifdef SLAP_X_LISTENER_MOD
1297 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1298 if ( op->o_conn->c_listener &&
1299 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1301 /* no "x" mode means bind required */
1302 rs->sr_text = "bind required on this listener";
1303 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1308 if ( !starttls && !updateop ) {
1309 if ( op->o_conn->c_listener &&
1310 !( op->o_conn->c_listener->sl_perms &
1311 ( !BER_BVISEMPTY( &op->o_dn )
1312 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1314 /* no "r" mode means no read */
1315 rs->sr_text = "read not allowed on this listener";
1316 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1320 #endif /* SLAP_X_LISTENER_MOD */
1324 if( ( restrictops & opflag )
1325 || ( exopflag && ( restrictops & exopflag ) )
1326 || (( restrictops & SLAP_RESTRICT_READONLY ) && updateop )) {
1327 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1328 rs->sr_text = "read operations restricted";
1329 } else if ( restrictops & exopflag ) {
1330 rs->sr_text = "extended operation restricted";
1332 rs->sr_text = "operation restricted";
1334 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1338 rs->sr_err = LDAP_SUCCESS;
1342 int backend_check_referrals( Operation *op, SlapReply *rs )
1344 rs->sr_err = LDAP_SUCCESS;
1346 if( op->o_bd->be_chk_referrals ) {
1347 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1349 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1350 send_ldap_result( op, rs );
1362 AttributeDescription *at,
1368 if ( op->o_bd == NULL ) {
1369 return LDAP_NO_SUCH_OBJECT;
1372 if ( op->o_bd->be_fetch ) {
1373 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1376 return LDAP_UNWILLING_TO_PERFORM;
1383 struct berval *gr_ndn,
1384 struct berval *op_ndn,
1385 ObjectClass *group_oc,
1386 AttributeDescription *group_at )
1389 void *o_priv = op->o_private, *e_priv = NULL;
1393 Backend *be = op->o_bd;
1396 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1397 if ( oex->oe_key == (void *)backend_group )
1401 if ( oex && ((OpExtraDB *)oex)->oe_db )
1402 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1404 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1405 op->o_bd = select_backend( gr_ndn, 0 );
1407 for ( g = op->o_groups; g; g = g->ga_next ) {
1408 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1409 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1413 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1423 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1428 op->o_private = NULL;
1429 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1430 e_priv = op->o_private;
1431 op->o_private = o_priv;
1435 a = attr_find( e->e_attrs, group_at );
1437 /* If the attribute is a subtype of labeledURI,
1438 * treat this as a dynamic group ala groupOfURLs
1440 if ( is_at_subtype( group_at->ad_type,
1441 slap_schema.si_ad_labeledURI->ad_type ) )
1445 struct berval bv, nbase;
1448 void *user_priv = NULL;
1449 Backend *b2 = op->o_bd;
1451 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1455 rc = LDAP_COMPARE_FALSE;
1456 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1457 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1463 BER_BVZERO( &nbase );
1465 /* host, attrs and extensions parts must be empty */
1466 if ( ( ludp->lud_host && *ludp->lud_host )
1473 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1474 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1475 op->o_tmpmemctx ) != LDAP_SUCCESS )
1480 switch ( ludp->lud_scope ) {
1481 case LDAP_SCOPE_BASE:
1482 if ( !dn_match( &nbase, op_ndn ) ) {
1486 case LDAP_SCOPE_ONELEVEL:
1487 dnParent( op_ndn, &bv );
1488 if ( !dn_match( &nbase, &bv ) ) {
1492 case LDAP_SCOPE_SUBTREE:
1493 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1497 case LDAP_SCOPE_SUBORDINATE:
1498 if ( dn_match( &nbase, op_ndn ) ||
1499 !dnIsSuffix( op_ndn, &nbase ) )
1505 /* NOTE: this could be NULL
1506 * if no filter is provided,
1507 * or if filter parsing fails.
1508 * In the latter case,
1509 * we should give up. */
1510 if ( ludp->lud_filter != NULL && ludp->lud_filter != '\0') {
1511 filter = str2filter_x( op, ludp->lud_filter );
1512 if ( filter == NULL ) {
1518 /* only get user if required
1519 * and not available yet */
1520 if ( user == NULL ) {
1523 op->o_bd = select_backend( op_ndn, 0 );
1524 op->o_private = NULL;
1525 rc2 = be_entry_get_rw( op, op_ndn, NULL, NULL, 0, &user );
1526 user_priv = op->o_private;
1527 op->o_private = o_priv;
1535 if ( test_filter( NULL, user, filter ) ==
1540 filter_free_x( op, filter, 1 );
1543 ldap_free_urldesc( ludp );
1544 if ( !BER_BVISNULL( &nbase ) ) {
1545 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1547 if ( rc != LDAP_COMPARE_FALSE ) {
1552 if ( user != NULL && user != target ) {
1553 op->o_private = user_priv;
1554 be_entry_release_r( op, user );
1555 op->o_private = o_priv;
1560 rc = attr_valfind( a,
1561 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1562 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1563 op_ndn, NULL, op->o_tmpmemctx );
1564 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
1565 rc = LDAP_COMPARE_FALSE;
1570 rc = LDAP_NO_SUCH_ATTRIBUTE;
1573 if ( e != target ) {
1574 op->o_private = e_priv;
1575 be_entry_release_r( op, e );
1576 op->o_private = o_priv;
1580 rc = LDAP_NO_SUCH_OBJECT;
1583 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1584 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1586 g->ga_be = op->o_bd;
1587 g->ga_oc = group_oc;
1588 g->ga_at = group_at;
1590 g->ga_len = gr_ndn->bv_len;
1591 strcpy( g->ga_ndn, gr_ndn->bv_val );
1592 g->ga_next = op->o_groups;
1605 struct berval *gr_ndn,
1606 struct berval *op_ndn,
1607 ObjectClass *group_oc,
1608 AttributeDescription *group_at )
1614 if ( op->o_abandon ) {
1615 return SLAPD_ABANDON;
1618 oex.oe_db = op->o_bd;
1619 oex.oe.oe_key = (void *)backend_group;
1620 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1623 op->o_bd = frontendDB;
1624 rc = frontendDB->be_group( op, target, gr_ndn,
1625 op_ndn, group_oc, group_at );
1627 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
1637 AttributeDescription *entry_at,
1639 slap_access_t access )
1642 void *o_priv = op->o_private, *e_priv = NULL;
1643 Attribute *a = NULL;
1644 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1645 AccessControlState acl_state = ACL_STATE_INIT;
1646 Backend *be = op->o_bd;
1649 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1650 if ( oex->oe_key == (void *)backend_attribute )
1654 if ( oex && ((OpExtraDB *)oex)->oe_db )
1655 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1657 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1658 op->o_bd = select_backend( edn, 0 );
1660 if ( target && dn_match( &target->e_nname, edn ) ) {
1664 op->o_private = NULL;
1665 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1666 e_priv = op->o_private;
1667 op->o_private = o_priv;
1671 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1672 assert( vals == NULL );
1675 if ( op->o_conn && access > ACL_NONE &&
1676 access_allowed( op, e, entry_at, NULL,
1677 access, &acl_state ) == 0 )
1679 rc = LDAP_INSUFFICIENT_ACCESS;
1684 a = attr_find( e->e_attrs, entry_at );
1686 SlapReply rs = { REP_SEARCH };
1687 AttributeName anlist[ 2 ];
1689 anlist[ 0 ].an_name = entry_at->ad_cname;
1690 anlist[ 0 ].an_desc = entry_at;
1691 BER_BVZERO( &anlist[ 1 ].an_name );
1692 rs.sr_attrs = anlist;
1694 /* NOTE: backend_operational() is also called
1695 * when returning results, so it's supposed
1696 * to do no harm to entries */
1698 rc = backend_operational( op, &rs );
1700 if ( rc == LDAP_SUCCESS ) {
1701 if ( rs.sr_operational_attrs ) {
1703 a = rs.sr_operational_attrs;
1706 rc = LDAP_NO_SUCH_ATTRIBUTE;
1714 if ( op->o_conn && access > ACL_NONE &&
1715 access_allowed( op, e, entry_at, NULL,
1716 access, &acl_state ) == 0 )
1718 rc = LDAP_INSUFFICIENT_ACCESS;
1723 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1725 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1727 if ( op->o_conn && access > ACL_NONE &&
1728 access_allowed( op, e, entry_at,
1735 ber_dupbv_x( &v[j], &a->a_nvals[i],
1737 if ( !BER_BVISNULL( &v[j] ) ) {
1742 op->o_tmpfree( v, op->o_tmpmemctx );
1744 rc = LDAP_INSUFFICIENT_ACCESS;
1747 BER_BVZERO( &v[j] );
1752 freeit: if ( e != target ) {
1753 op->o_private = e_priv;
1754 be_entry_release_r( op, e );
1755 op->o_private = o_priv;
1771 AttributeDescription *entry_at,
1773 slap_access_t access )
1779 oex.oe_db = op->o_bd;
1780 oex.oe.oe_key = (void *)backend_attribute;
1781 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1784 op->o_bd = frontendDB;
1785 rc = frontendDB->be_attribute( op, target, edn,
1786 entry_at, vals, access );
1788 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
1798 AttributeDescription *entry_at,
1799 struct berval *nval,
1800 slap_access_t access,
1804 void *o_priv, *e_priv = NULL;
1805 int rc = LDAP_INSUFFICIENT_ACCESS;
1809 assert( op != NULL );
1810 assert( op->o_conn != NULL );
1811 assert( edn != NULL );
1812 assert( access > ACL_NONE );
1815 o_priv = op->o_private;
1818 op->o_bd = select_backend( edn, 0 );
1821 if ( target && dn_match( &target->e_nname, edn ) ) {
1825 op->o_private = NULL;
1826 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1827 e_priv = op->o_private;
1828 op->o_private = o_priv;
1832 Attribute *a = NULL;
1835 if ( entry_at == NULL ) {
1836 entry_at = slap_schema.si_ad_entry;
1839 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1841 if ( access_allowed_mask( op, e, entry_at,
1842 NULL, access, NULL, mask ) == 0 )
1844 rc = LDAP_INSUFFICIENT_ACCESS;
1851 a = attr_find( e->e_attrs, entry_at );
1853 SlapReply rs = { REP_SEARCH };
1854 AttributeName anlist[ 2 ];
1856 anlist[ 0 ].an_name = entry_at->ad_cname;
1857 anlist[ 0 ].an_desc = entry_at;
1858 BER_BVZERO( &anlist[ 1 ].an_name );
1859 rs.sr_attrs = anlist;
1861 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1863 /* NOTE: backend_operational() is also called
1864 * when returning results, so it's supposed
1865 * to do no harm to entries */
1867 rc = backend_operational( op, &rs );
1869 if ( rc == LDAP_SUCCESS ) {
1870 if ( rs.sr_operational_attrs ) {
1872 a = rs.sr_operational_attrs;
1875 rc = LDAP_NO_SUCH_OBJECT;
1881 if ( access_allowed_mask( op, e, entry_at,
1882 nval, access, NULL, mask ) == 0 )
1884 rc = LDAP_INSUFFICIENT_ACCESS;
1890 freeit: if ( e != target ) {
1891 op->o_private = e_priv;
1892 be_entry_release_r( op, e );
1893 op->o_private = o_priv;
1910 int rc = LDAP_SUCCESS;
1911 BackendDB *be_orig = op->o_bd;
1914 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1915 if ( oex->oe_key == (void *)backend_operational )
1919 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1920 /* just count them */ ;
1923 * If operational attributes (allegedly) are required,
1924 * and the backend supports specific operational attributes,
1925 * add them to the attribute list
1927 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1928 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1929 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1931 *ap = slap_operational_entryDN( rs->sr_entry );
1932 ap = &(*ap)->a_next;
1935 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1936 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1937 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1939 *ap = slap_operational_subschemaSubentry( op->o_bd );
1940 ap = &(*ap)->a_next;
1943 /* Let the overlays have a chance at this */
1944 if ( oex && ((OpExtraDB *)oex)->oe_db )
1945 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1947 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1948 op->o_bd = select_backend( &op->o_req_ndn, 0 );
1950 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1951 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1952 op->o_bd->be_operational != NULL )
1954 rc = op->o_bd->be_operational( op, rs );
1961 int backend_operational( Operation *op, SlapReply *rs )
1967 oex.oe_db = op->o_bd;
1968 oex.oe.oe_key = (void *)backend_operational;
1969 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1971 /* Moved this into the frontend so global overlays are called */
1974 op->o_bd = frontendDB;
1975 rc = frontendDB->be_operational( op, rs );
1977 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
1982 /* helper that calls the bi_tool_entry_first_x() variant with default args;
1983 * use to initialize a backend's bi_tool_entry_first() when appropriate
1986 backend_tool_entry_first( BackendDB *be )
1988 return be->bd_info->bi_tool_entry_first_x( be,
1989 NULL, LDAP_SCOPE_DEFAULT, NULL );