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>
41 * If a module is configured as dynamic, its header should not
42 * get included into slapd. While this is a general rule and does
43 * not have much of an effect in UNIX, this rule should be adhered
44 * to for Windows, where dynamic object code should not be implicitly
45 * imported into slapd without appropriate __declspec(dllimport) directives.
49 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
52 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
55 backend_init_controls( BackendInfo *bi )
57 if ( bi->bi_controls ) {
60 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
63 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
64 == LDAP_CONTROL_NOT_FOUND )
66 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
73 bi->bi_ctrls[ cid ] = 1;
80 int backend_init(void)
85 if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
86 /* already initialized */
87 Debug( LDAP_DEBUG_ANY,
88 "backend_init: already initialized\n", 0, 0, 0 );
92 for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ ) {
93 assert( bi->bi_init != 0 );
95 rc = bi->bi_init( bi );
98 Debug( LDAP_DEBUG_ANY,
99 "backend_init: initialized for type \"%s\"\n",
101 /* destroy those we've already inited */
106 if ( slap_binfo[nBackendInfo].bi_destroy ) {
107 slap_binfo[nBackendInfo].bi_destroy(
108 &slap_binfo[nBackendInfo] );
114 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
117 if ( nBackendInfo > 0) {
125 Debug( LDAP_DEBUG_ANY,
126 "backend_init: failed\n",
130 #endif /* SLAPD_MODULES */
133 int backend_add(BackendInfo *aBackendInfo)
137 if ( aBackendInfo->bi_init == NULL ) {
138 Debug( LDAP_DEBUG_ANY, "backend_add: "
139 "backend type \"%s\" does not have the (mandatory)init function\n",
140 aBackendInfo->bi_type, 0, 0 );
144 rc = aBackendInfo->bi_init(aBackendInfo);
146 Debug( LDAP_DEBUG_ANY,
147 "backend_add: initialization for type \"%s\" failed\n",
148 aBackendInfo->bi_type, 0, 0 );
152 (void)backend_init_controls( aBackendInfo );
154 /* now add the backend type to the Backend Info List */
155 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
161 backend_set_controls( BackendDB *be )
163 BackendInfo *bi = be->bd_info;
165 /* back-relay takes care of itself; so may do other */
166 if ( overlay_is_over( be ) ) {
167 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
170 if ( bi->bi_controls ) {
171 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
172 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
173 sizeof( be->be_ctrls ) );
174 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
179 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
180 if ( bi->bi_ctrls[ i ] ) {
181 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
191 /* startup a specific backend database */
192 int backend_startup_one(Backend *be)
196 assert( be != NULL );
198 be->be_pending_csn_list = (struct be_pcl *)
199 ch_calloc( 1, sizeof( struct be_pcl ) );
201 LDAP_TAILQ_INIT( be->be_pending_csn_list );
203 Debug( LDAP_DEBUG_TRACE,
204 "backend_startup_one: starting \"%s\"\n",
205 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
208 /* set database controls */
209 (void)backend_set_controls( be );
211 if ( be->bd_info->bi_db_open ) {
212 rc = be->bd_info->bi_db_open( be );
214 (void)backend_set_controls( be );
217 Debug( LDAP_DEBUG_ANY,
218 "backend_startup_one: bi_db_open failed! (%d)\n",
226 int backend_startup(Backend *be)
232 if( ! ( nBackendDB > 0 ) ) {
234 Debug( LDAP_DEBUG_ANY,
235 "backend_startup: %d databases to startup.\n",
241 if ( be->bd_info->bi_open ) {
242 rc = be->bd_info->bi_open( be->bd_info );
244 Debug( LDAP_DEBUG_ANY,
245 "backend_startup: bi_open failed!\n",
251 /* append global access controls */
252 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
254 return backend_startup_one( be );
257 /* open frontend, if required */
258 if ( frontendDB->bd_info->bi_db_open ) {
259 rc = frontendDB->bd_info->bi_db_open( frontendDB );
261 Debug( LDAP_DEBUG_ANY,
262 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
268 /* open each backend type */
270 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
272 if( bi->bi_nDB == 0) {
273 /* no database of this type, don't open */
278 rc = bi->bi_open( bi );
280 Debug( LDAP_DEBUG_ANY,
281 "backend_startup: bi_open %d (%s) failed!\n",
287 (void)backend_init_controls( bi );
290 /* open each backend database */
292 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
294 if ( be->be_suffix == NULL ) {
295 Debug( LDAP_DEBUG_ANY,
296 "backend_startup: warning, database %d (%s) "
298 i, be->bd_info->bi_type, 0 );
300 /* append global access controls */
301 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
303 rc = backend_startup_one( be );
311 int backend_num( Backend *be )
316 if( be == NULL ) return -1;
318 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
319 if( be == b2 ) return i;
325 int backend_shutdown( Backend *be )
331 /* shutdown a specific backend database */
333 if ( be->bd_info->bi_nDB == 0 ) {
334 /* no database of this type, we never opened it */
338 if ( be->bd_info->bi_db_close ) {
339 be->bd_info->bi_db_close( be );
342 if( be->bd_info->bi_close ) {
343 be->bd_info->bi_close( be->bd_info );
349 /* close each backend database */
350 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
351 if ( be->bd_info->bi_db_close ) {
352 be->bd_info->bi_db_close( be );
356 Debug( LDAP_DEBUG_ANY,
357 "backend_close: bi_db_close %s failed!\n",
362 /* close each backend type */
363 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
364 if( bi->bi_nDB == 0 ) {
365 /* no database of this type */
374 /* close frontend, if required */
375 if ( frontendDB->bd_info->bi_db_close ) {
376 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
378 Debug( LDAP_DEBUG_ANY,
379 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
388 * This function is supposed to be the exact counterpart
389 * of backend_startup_one(), although this one calls bi_db_destroy()
390 * while backend_startup_one() calls bi_db_open().
392 * Make sure backend_stopdown_one() destroys resources allocated
393 * by backend_startup_one(); only call backend_destroy_one() when
394 * all stuff in a BackendDB needs to be destroyed
397 backend_stopdown_one( BackendDB *bd )
399 if ( bd->be_pending_csn_list ) {
400 struct slap_csn_entry *csne;
401 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
403 struct slap_csn_entry *tmp_csne = csne;
405 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
406 ch_free( csne->ce_csn.bv_val );
407 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
410 ch_free( bd->be_pending_csn_list );
413 if ( bd->bd_info->bi_db_destroy ) {
414 bd->bd_info->bi_db_destroy( bd );
418 void backend_destroy_one( BackendDB *bd, int dynamic )
421 LDAP_STAILQ_REMOVE(&backendDB, bd, BackendDB, be_next );
424 if ( bd->be_syncinfo ) {
425 syncinfo_free( bd->be_syncinfo, 1 );
428 backend_stopdown_one( bd );
430 ber_bvarray_free( bd->be_suffix );
431 ber_bvarray_free( bd->be_nsuffix );
432 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
433 free( bd->be_rootdn.bv_val );
435 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
436 free( bd->be_rootndn.bv_val );
438 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
439 free( bd->be_rootpw.bv_val );
441 acl_destroy( bd->be_acl, frontendDB->be_acl );
442 limits_destroy( bd->be_limits );
443 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
444 ch_free( bd->be_update_ndn.bv_val );
446 if ( bd->be_update_refs ) {
447 ber_bvarray_free( bd->be_update_refs );
455 int backend_destroy(void)
460 /* destroy each backend database */
461 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
462 backend_destroy_one( bd, 1 );
465 /* destroy each backend type */
466 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
467 if( bi->bi_destroy ) {
468 bi->bi_destroy( bi );
473 LDAP_STAILQ_INIT(&backendInfo);
475 /* destroy frontend database */
478 if ( bd->bd_info->bi_db_destroy ) {
479 bd->bd_info->bi_db_destroy( bd );
481 ber_bvarray_free( bd->be_suffix );
482 ber_bvarray_free( bd->be_nsuffix );
483 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
484 free( bd->be_rootdn.bv_val );
486 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
487 free( bd->be_rootndn.bv_val );
489 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
490 free( bd->be_rootpw.bv_val );
492 acl_destroy( bd->be_acl, frontendDB->be_acl );
498 BackendInfo* backend_info(const char *type)
502 /* search for the backend type */
503 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
504 if( strcasecmp(bi->bi_type, type) == 0 ) {
518 /* If idx < 0, just add to end of list */
520 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
521 } else if ( idx == 0 ) {
522 LDAP_STAILQ_INSERT_HEAD(&backendDB, be, be_next);
527 b2 = LDAP_STAILQ_FIRST(&backendDB);
529 for (i=0; i<idx; i++) {
530 b2 = LDAP_STAILQ_NEXT(b2, be_next);
532 LDAP_STAILQ_INSERT_AFTER(&backendDB, b2, be, be_next);
542 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
543 backend_db_insert(be, idx);
552 BackendInfo *bi = backend_info(type);
557 fprintf( stderr, "Unrecognized database type (%s)\n", type );
561 /* If be is provided, treat it as private. Otherwise allocate
562 * one and add it to the global list.
565 be = ch_calloc( 1, sizeof(Backend) );
567 if ( idx >= nbackends )
570 backend_db_insert( be, idx );
575 be->be_def_limit = frontendDB->be_def_limit;
576 be->be_dfltaccess = frontendDB->be_dfltaccess;
578 be->be_restrictops = frontendDB->be_restrictops;
579 be->be_requires = frontendDB->be_requires;
580 be->be_ssf_set = frontendDB->be_ssf_set;
582 be->be_pcl_mutexp = &be->be_pcl_mutex;
583 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
585 /* assign a default depth limit for alias deref */
586 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
588 if ( bi->bi_db_init ) {
589 rc = bi->bi_db_init( be );
593 fprintf( stderr, "database init failed (%s)\n", type );
594 /* If we created and linked this be, remove it and free it */
596 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
612 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
613 if ( be->bd_info->bi_db_close ) {
614 be->bd_info->bi_db_close( be );
618 if ( frontendDB->bd_info->bi_db_close ) {
619 frontendDB->bd_info->bi_db_close( frontendDB );
631 ber_len_t len, dnlen = dn->bv_len;
632 Backend *be, *b2 = NULL;
634 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
635 if ( be->be_nsuffix == NULL || SLAP_DBHIDDEN( be )) {
639 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
641 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
646 len = be->be_nsuffix[j].bv_len;
649 /* suffix is longer than DN */
654 * input DN is normalized, so the separator check
655 * need not look at escaping
657 if ( len && len < dnlen &&
658 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
663 if ( strcmp( be->be_nsuffix[j].bv_val,
664 &dn->bv_val[dnlen-len] ) == 0 )
669 if( manageDSAit && len == dnlen &&
670 !SLAP_GLUE_SUBORDINATE( be ) ) {
674 /* If any parts of the tree are glued, use the first
675 * match regardless of manageDSAit. Otherwise use the
678 if( !( SLAP_DBFLAGS( be ) & ( SLAP_DBFLAG_GLUE_INSTANCE |
679 SLAP_DBFLAG_GLUE_SUBORDINATE )))
693 struct berval *bvsuffix )
697 if ( be->be_nsuffix == NULL ) {
701 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
702 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
711 be_isroot_dn( Backend *be, struct berval *ndn )
713 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
717 return dn_match( &be->be_rootndn, ndn );
721 be_slurp_update( Operation *op )
723 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
724 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
728 be_shadow_update( Operation *op )
730 /* This assumes that all internal ops (connid == -1) on a syncrepl
731 * database are syncrepl operations.
733 return (( SLAP_SYNC_SHADOW( op->o_bd ) && op->o_connid == -1 ) ||
734 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
738 be_isupdate_dn( Backend *be, struct berval *ndn )
740 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
744 return dn_match( &be->be_update_ndn, ndn );
748 be_root_dn( Backend *be )
750 return &be->be_rootdn;
754 be_isroot( Operation *op )
756 return be_isroot_dn( op->o_bd, &op->o_ndn );
760 be_isroot_pw( Operation *op )
764 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
768 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
773 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
774 op->o_conn->c_sasl_authctx, NULL );
777 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
780 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
792 if ( op->o_bd->be_release ) {
793 /* free and release entry from backend */
794 return op->o_bd->be_release( op, e, rw );
803 backend_unbind( Operation *op, SlapReply *rs )
807 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
808 if ( be->be_unbind ) {
810 be->be_unbind( op, rs );
818 backend_connection_init(
823 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
824 if ( be->be_connection_init ) {
825 be->be_connection_init( be, conn );
833 backend_connection_destroy(
838 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
839 if ( be->be_connection_destroy ) {
840 be->be_connection_destroy( be, conn);
848 backend_check_controls(
852 LDAPControl **ctrls = op->o_ctrls;
853 rs->sr_err = LDAP_SUCCESS;
856 for( ; *ctrls != NULL ; ctrls++ ) {
859 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
860 case LDAP_CONTROL_NOT_FOUND:
861 /* unrecognized control */
862 if ( (*ctrls)->ldctl_iscritical ) {
863 /* should not be reachable */
864 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
865 "unrecognized critical control: %s\n",
866 (*ctrls)->ldctl_oid, 0, 0 );
869 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
870 "unrecognized non-critical control: %s\n",
871 (*ctrls)->ldctl_oid, 0, 0 );
875 case LDAP_COMPARE_FALSE:
876 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
877 /* RFC 4511 allows unavailableCriticalExtension to be
878 * returned when the server is unwilling to perform
879 * an operation extended by a recognized critical
882 rs->sr_text = "critical control unavailable in context";
883 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
888 case LDAP_COMPARE_TRUE:
893 Debug( LDAP_DEBUG_ANY,
894 "backend_check_controls: unable to check control: %s\n",
895 (*ctrls)->ldctl_oid, 0, 0 );
898 rs->sr_text = "unable to check control";
899 rs->sr_err = LDAP_OTHER;
905 #if 0 /* temporarily removed */
906 /* check should be generalized */
907 if( get_relax(op) && !be_isroot(op)) {
908 rs->sr_text = "requires manager authorization";
909 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
918 backend_check_restrictions(
921 struct berval *opdata )
923 slap_mask_t restrictops;
924 slap_mask_t requires;
926 slap_mask_t exopflag = 0;
933 int rc = SLAP_CB_CONTINUE;
935 if ( op->o_bd->be_chk_controls ) {
936 rc = ( *op->o_bd->be_chk_controls )( op, rs );
939 if ( rc == SLAP_CB_CONTINUE ) {
940 rc = backend_check_controls( op, rs );
943 if ( rc != LDAP_SUCCESS ) {
947 restrictops = op->o_bd->be_restrictops;
948 requires = op->o_bd->be_requires;
949 ssf = &op->o_bd->be_ssf_set;
952 restrictops = frontendDB->be_restrictops;
953 requires = frontendDB->be_requires;
954 ssf = &frontendDB->be_ssf_set;
957 switch( op->o_tag ) {
959 opflag = SLAP_RESTRICT_OP_ADD;
963 opflag = SLAP_RESTRICT_OP_BIND;
966 case LDAP_REQ_COMPARE:
967 opflag = SLAP_RESTRICT_OP_COMPARE;
969 case LDAP_REQ_DELETE:
971 opflag = SLAP_RESTRICT_OP_DELETE;
973 case LDAP_REQ_EXTENDED:
974 opflag = SLAP_RESTRICT_OP_EXTENDED;
977 /* treat unspecified as a modify */
978 opflag = SLAP_RESTRICT_OP_MODIFY;
983 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
986 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
990 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
991 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
995 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
996 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1000 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1001 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1006 /* treat everything else as a modify */
1007 opflag = SLAP_RESTRICT_OP_MODIFY;
1011 case LDAP_REQ_MODIFY:
1013 opflag = SLAP_RESTRICT_OP_MODIFY;
1015 case LDAP_REQ_RENAME:
1017 opflag = SLAP_RESTRICT_OP_RENAME;
1019 case LDAP_REQ_SEARCH:
1020 opflag = SLAP_RESTRICT_OP_SEARCH;
1022 case LDAP_REQ_UNBIND:
1027 rs->sr_text = "restrict operations internal error";
1028 rs->sr_err = LDAP_OTHER;
1033 /* these checks don't apply to StartTLS */
1035 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1036 if( op->o_transport_ssf < ssf->sss_transport ) {
1037 rs->sr_text = op->o_transport_ssf
1038 ? "stronger transport confidentiality required"
1039 : "transport confidentiality required";
1043 if( op->o_tls_ssf < ssf->sss_tls ) {
1044 rs->sr_text = op->o_tls_ssf
1045 ? "stronger TLS confidentiality required"
1046 : "TLS confidentiality required";
1051 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1052 /* simple bind specific check */
1053 if( op->o_ssf < ssf->sss_simple_bind ) {
1054 rs->sr_text = op->o_ssf
1055 ? "stronger confidentiality required"
1056 : "confidentiality required";
1061 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1062 /* these checks don't apply to SASL bind */
1064 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1065 rs->sr_text = op->o_sasl_ssf
1066 ? "stronger SASL confidentiality required"
1067 : "SASL confidentiality required";
1071 if( op->o_ssf < ssf->sss_ssf ) {
1072 rs->sr_text = op->o_ssf
1073 ? "stronger confidentiality required"
1074 : "confidentiality required";
1080 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1081 rs->sr_text = op->o_transport_ssf
1082 ? "stronger transport confidentiality required for update"
1083 : "transport confidentiality required for update";
1087 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1088 rs->sr_text = op->o_tls_ssf
1089 ? "stronger TLS confidentiality required for update"
1090 : "TLS confidentiality required for update";
1094 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1095 rs->sr_text = op->o_sasl_ssf
1096 ? "stronger SASL confidentiality required for update"
1097 : "SASL confidentiality required for update";
1101 if( op->o_ssf < ssf->sss_update_ssf ) {
1102 rs->sr_text = op->o_ssf
1103 ? "stronger confidentiality required for update"
1104 : "confidentiality required for update";
1108 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1109 BER_BVISEMPTY( &op->o_ndn ) )
1111 rs->sr_text = "modifications require authentication";
1112 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1116 #ifdef SLAP_X_LISTENER_MOD
1117 if ( op->o_conn->c_listener &&
1118 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1119 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1121 /* no "w" mode means readonly */
1122 rs->sr_text = "modifications not allowed on this listener";
1123 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1126 #endif /* SLAP_X_LISTENER_MOD */
1131 /* these checks don't apply to Bind, StartTLS, or Unbind */
1133 if( requires & SLAP_REQUIRE_STRONG ) {
1134 /* should check mechanism */
1135 if( ( op->o_transport_ssf < ssf->sss_transport
1136 && op->o_authtype == LDAP_AUTH_SIMPLE )
1137 || BER_BVISEMPTY( &op->o_dn ) )
1139 rs->sr_text = "strong(er) authentication required";
1140 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1145 if( requires & SLAP_REQUIRE_SASL ) {
1146 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1147 rs->sr_text = "SASL authentication required";
1148 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1153 if( requires & SLAP_REQUIRE_AUTHC ) {
1154 if( BER_BVISEMPTY( &op->o_dn ) ) {
1155 rs->sr_text = "authentication required";
1156 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1161 if( requires & SLAP_REQUIRE_BIND ) {
1163 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1164 version = op->o_conn->c_protocol;
1165 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1168 /* no bind has occurred */
1169 rs->sr_text = "BIND required";
1170 rs->sr_err = LDAP_OPERATIONS_ERROR;
1175 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1176 if( op->o_protocol < LDAP_VERSION3 ) {
1177 /* no bind has occurred */
1178 rs->sr_text = "operation restricted to LDAPv3 clients";
1179 rs->sr_err = LDAP_OPERATIONS_ERROR;
1184 #ifdef SLAP_X_LISTENER_MOD
1185 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1186 if ( op->o_conn->c_listener &&
1187 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1189 /* no "x" mode means bind required */
1190 rs->sr_text = "bind required on this listener";
1191 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1196 if ( !starttls && !updateop ) {
1197 if ( op->o_conn->c_listener &&
1198 !( op->o_conn->c_listener->sl_perms &
1199 ( !BER_BVISEMPTY( &op->o_dn )
1200 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1202 /* no "r" mode means no read */
1203 rs->sr_text = "read not allowed on this listener";
1204 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1208 #endif /* SLAP_X_LISTENER_MOD */
1212 if( ( restrictops & opflag )
1213 || ( exopflag && ( restrictops & exopflag ) ) ) {
1214 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1215 rs->sr_text = "read operations restricted";
1216 } else if ( restrictops & exopflag ) {
1217 rs->sr_text = "extended operation restricted";
1219 rs->sr_text = "operation restricted";
1221 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1225 rs->sr_err = LDAP_SUCCESS;
1229 int backend_check_referrals( Operation *op, SlapReply *rs )
1231 rs->sr_err = LDAP_SUCCESS;
1233 if( op->o_bd->be_chk_referrals ) {
1234 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1236 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1237 send_ldap_result( op, rs );
1249 AttributeDescription *at,
1255 if ( op->o_bd == NULL ) {
1256 return LDAP_NO_SUCH_OBJECT;
1259 if ( op->o_bd->be_fetch ) {
1260 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1263 return LDAP_UNWILLING_TO_PERFORM;
1270 struct berval *gr_ndn,
1271 struct berval *op_ndn,
1272 ObjectClass *group_oc,
1273 AttributeDescription *group_at )
1276 void *o_priv = op->o_private, *e_priv = NULL;
1280 Backend *be = op->o_bd;
1282 op->o_bd = select_backend( gr_ndn, 0, 0 );
1284 for ( g = op->o_groups; g; g = g->ga_next ) {
1285 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1286 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1290 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1300 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1305 op->o_private = NULL;
1306 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1307 e_priv = op->o_private;
1308 op->o_private = o_priv;
1312 a = attr_find( e->e_attrs, group_at );
1314 /* If the attribute is a subtype of labeledURI,
1315 * treat this as a dynamic group ala groupOfURLs
1317 if ( is_at_subtype( group_at->ad_type,
1318 slap_schema.si_ad_labeledURI->ad_type ) )
1322 struct berval bv, nbase;
1325 void *user_priv = NULL;
1326 Backend *b2 = op->o_bd;
1328 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1332 rc = LDAP_COMPARE_FALSE;
1333 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1334 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1340 BER_BVZERO( &nbase );
1342 /* host, attrs and extensions parts must be empty */
1343 if ( ( ludp->lud_host && *ludp->lud_host )
1350 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1351 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1352 op->o_tmpmemctx ) != LDAP_SUCCESS )
1357 switch ( ludp->lud_scope ) {
1358 case LDAP_SCOPE_BASE:
1359 if ( !dn_match( &nbase, op_ndn ) ) {
1363 case LDAP_SCOPE_ONELEVEL:
1364 dnParent( op_ndn, &bv );
1365 if ( !dn_match( &nbase, &bv ) ) {
1369 case LDAP_SCOPE_SUBTREE:
1370 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1374 case LDAP_SCOPE_SUBORDINATE:
1375 if ( dn_match( &nbase, op_ndn ) ||
1376 !dnIsSuffix( op_ndn, &nbase ) )
1382 /* NOTE: this could be NULL
1383 * if no filter is provided,
1384 * or if filter parsing fails.
1385 * In the latter case,
1386 * we should give up. */
1387 if ( ludp->lud_filter != NULL && ludp->lud_filter != '\0') {
1388 filter = str2filter_x( op, ludp->lud_filter );
1389 if ( filter == NULL ) {
1395 /* only get user if required
1396 * and not available yet */
1397 if ( user == NULL ) {
1400 op->o_bd = select_backend( op_ndn, 0, 0 );
1401 op->o_private = NULL;
1402 rc2 = be_entry_get_rw( op, op_ndn, NULL, NULL, 0, &user );
1403 user_priv = op->o_private;
1404 op->o_private = o_priv;
1412 if ( test_filter( NULL, user, filter ) ==
1417 filter_free_x( op, filter );
1420 ldap_free_urldesc( ludp );
1421 if ( !BER_BVISNULL( &nbase ) ) {
1422 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1424 if ( rc != LDAP_COMPARE_FALSE ) {
1429 if ( user != NULL && user != target ) {
1430 op->o_private = user_priv;
1431 be_entry_release_r( op, user );
1432 op->o_private = o_priv;
1437 rc = value_find_ex( group_at,
1438 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1439 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1440 a->a_nvals, op_ndn, op->o_tmpmemctx );
1441 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
1442 rc = LDAP_COMPARE_FALSE;
1447 rc = LDAP_NO_SUCH_ATTRIBUTE;
1450 if ( e != target ) {
1451 op->o_private = e_priv;
1452 be_entry_release_r( op, e );
1453 op->o_private = o_priv;
1457 rc = LDAP_NO_SUCH_OBJECT;
1460 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1461 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1463 g->ga_be = op->o_bd;
1464 g->ga_oc = group_oc;
1465 g->ga_at = group_at;
1467 g->ga_len = gr_ndn->bv_len;
1468 strcpy( g->ga_ndn, gr_ndn->bv_val );
1469 g->ga_next = op->o_groups;
1482 struct berval *gr_ndn,
1483 struct berval *op_ndn,
1484 ObjectClass *group_oc,
1485 AttributeDescription *group_at )
1490 if ( op->o_abandon ) {
1491 return SLAPD_ABANDON;
1495 op->o_bd = frontendDB;
1496 rc = frontendDB->be_group( op, target, gr_ndn,
1497 op_ndn, group_oc, group_at );
1508 AttributeDescription *entry_at,
1510 slap_access_t access )
1513 void *o_priv = op->o_private, *e_priv = NULL;
1514 Attribute *a = NULL;
1515 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1516 AccessControlState acl_state = ACL_STATE_INIT;
1517 Backend *be = op->o_bd;
1519 op->o_bd = select_backend( edn, 0, 0 );
1521 if ( target && dn_match( &target->e_nname, edn ) ) {
1525 op->o_private = NULL;
1526 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1527 e_priv = op->o_private;
1528 op->o_private = o_priv;
1532 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1533 assert( vals == NULL );
1536 if ( op->o_conn && access > ACL_NONE &&
1537 access_allowed( op, e, entry_at, NULL,
1538 access, &acl_state ) == 0 )
1540 rc = LDAP_INSUFFICIENT_ACCESS;
1545 a = attr_find( e->e_attrs, entry_at );
1547 SlapReply rs = { 0 };
1548 AttributeName anlist[ 2 ];
1550 anlist[ 0 ].an_name = entry_at->ad_cname;
1551 anlist[ 0 ].an_desc = entry_at;
1552 BER_BVZERO( &anlist[ 1 ].an_name );
1553 rs.sr_attrs = anlist;
1555 /* NOTE: backend_operational() is also called
1556 * when returning results, so it's supposed
1557 * to do no harm to entries */
1559 rc = backend_operational( op, &rs );
1562 if ( rc == LDAP_SUCCESS ) {
1563 if ( rs.sr_operational_attrs ) {
1565 a = rs.sr_operational_attrs;
1568 rc = LDAP_NO_SUCH_ATTRIBUTE;
1576 if ( op->o_conn && access > ACL_NONE &&
1577 access_allowed( op, e, entry_at, NULL,
1578 access, &acl_state ) == 0 )
1580 rc = LDAP_INSUFFICIENT_ACCESS;
1584 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1587 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1589 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1591 if ( op->o_conn && access > ACL_NONE &&
1592 access_allowed( op, e, entry_at,
1599 ber_dupbv_x( &v[j], &a->a_nvals[i],
1601 if ( !BER_BVISNULL( &v[j] ) ) {
1606 op->o_tmpfree( v, op->o_tmpmemctx );
1608 rc = LDAP_INSUFFICIENT_ACCESS;
1611 BER_BVZERO( &v[j] );
1616 freeit: if ( e != target ) {
1617 op->o_private = e_priv;
1618 be_entry_release_r( op, e );
1619 op->o_private = o_priv;
1635 AttributeDescription *entry_at,
1637 slap_access_t access )
1643 op->o_bd = frontendDB;
1644 rc = frontendDB->be_attribute( op, target, edn,
1645 entry_at, vals, access );
1656 AttributeDescription *entry_at,
1657 struct berval *nval,
1658 slap_access_t access,
1662 void *o_priv = op->o_private, *e_priv = NULL;
1663 int rc = LDAP_INSUFFICIENT_ACCESS;
1664 Backend *be = op->o_bd;
1667 assert( op != NULL );
1668 assert( op->o_conn != NULL );
1669 assert( edn != NULL );
1670 assert( access > ACL_NONE );
1672 op->o_bd = select_backend( edn, 0, 0 );
1674 if ( target && dn_match( &target->e_nname, edn ) ) {
1678 op->o_private = NULL;
1679 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1680 e_priv = op->o_private;
1681 op->o_private = o_priv;
1685 Attribute *a = NULL;
1688 if ( entry_at == NULL ) {
1689 entry_at = slap_schema.si_ad_entry;
1692 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1694 if ( access_allowed_mask( op, e, entry_at,
1695 NULL, access, NULL, mask ) == 0 )
1697 rc = LDAP_INSUFFICIENT_ACCESS;
1704 a = attr_find( e->e_attrs, entry_at );
1706 SlapReply rs = { 0 };
1707 AttributeName anlist[ 2 ];
1709 anlist[ 0 ].an_name = entry_at->ad_cname;
1710 anlist[ 0 ].an_desc = entry_at;
1711 BER_BVZERO( &anlist[ 1 ].an_name );
1712 rs.sr_attrs = anlist;
1714 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1716 /* NOTE: backend_operational() is also called
1717 * when returning results, so it's supposed
1718 * to do no harm to entries */
1720 rc = backend_operational( op, &rs );
1723 if ( rc == LDAP_SUCCESS ) {
1724 if ( rs.sr_operational_attrs ) {
1726 a = rs.sr_operational_attrs;
1729 rc = LDAP_NO_SUCH_OBJECT;
1735 if ( access_allowed_mask( op, e, entry_at,
1736 nval, access, NULL, mask ) == 0 )
1738 rc = LDAP_INSUFFICIENT_ACCESS;
1744 freeit: if ( e != target ) {
1745 op->o_private = e_priv;
1746 be_entry_release_r( op, e );
1747 op->o_private = o_priv;
1766 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1767 /* just count them */ ;
1770 * If operational attributes (allegedly) are required,
1771 * and the backend supports specific operational attributes,
1772 * add them to the attribute list
1774 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1775 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1776 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1778 *ap = slap_operational_entryDN( rs->sr_entry );
1779 ap = &(*ap)->a_next;
1782 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1783 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1784 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1786 *ap = slap_operational_subschemaSubentry( op->o_bd );
1787 ap = &(*ap)->a_next;
1790 if ( op->o_bd != NULL ) {
1791 BackendDB *be_orig = op->o_bd;
1793 /* Let the overlays have a chance at this */
1794 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
1795 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1796 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1797 op->o_bd->be_operational != NULL )
1799 rc = op->o_bd->be_operational( op, rs );
1807 int backend_operational( Operation *op, SlapReply *rs )
1812 /* Moved this into the frontend so global overlays are called */
1815 op->o_bd = frontendDB;
1816 rc = frontendDB->be_operational( op, rs );