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-2006 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",
387 void backend_destroy_one( BackendDB *bd, int dynamic )
390 LDAP_STAILQ_REMOVE(&backendDB, bd, slap_backend_db, be_next );
393 if ( bd->be_syncinfo ) {
394 syncinfo_free( bd->be_syncinfo );
397 if ( bd->be_pending_csn_list ) {
398 struct slap_csn_entry *csne;
399 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
401 struct slap_csn_entry *tmp_csne = csne;
403 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
404 ch_free( csne->ce_csn.bv_val );
405 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
408 ch_free( bd->be_pending_csn_list );
411 if ( bd->bd_info->bi_db_destroy ) {
412 bd->bd_info->bi_db_destroy( bd );
414 ber_bvarray_free( bd->be_suffix );
415 ber_bvarray_free( bd->be_nsuffix );
416 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
417 free( bd->be_rootdn.bv_val );
419 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
420 free( bd->be_rootndn.bv_val );
422 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
423 free( bd->be_rootpw.bv_val );
425 acl_destroy( bd->be_acl, frontendDB->be_acl );
426 limits_destroy( bd->be_limits );
427 if ( bd->be_replogfile ) {
428 ch_free( bd->be_replogfile );
430 if ( bd->be_replica_argsfile ) {
431 ch_free( bd->be_replica_argsfile );
433 if ( bd->be_replica_pidfile ) {
434 ch_free( bd->be_replica_pidfile );
436 destroy_replica_info( bd );
437 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
438 ch_free( bd->be_update_ndn.bv_val );
440 if ( bd->be_update_refs ) {
441 ber_bvarray_free( bd->be_update_refs );
449 int backend_destroy(void)
454 /* destroy each backend database */
455 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
456 backend_destroy_one( bd, 1 );
459 /* destroy each backend type */
460 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
461 if( bi->bi_destroy ) {
462 bi->bi_destroy( bi );
467 LDAP_STAILQ_INIT(&backendInfo);
469 /* destroy frontend database */
472 if ( bd->bd_info->bi_db_destroy ) {
473 bd->bd_info->bi_db_destroy( bd );
475 ber_bvarray_free( bd->be_suffix );
476 ber_bvarray_free( bd->be_nsuffix );
477 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
478 free( bd->be_rootdn.bv_val );
480 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
481 free( bd->be_rootndn.bv_val );
483 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
484 free( bd->be_rootpw.bv_val );
486 acl_destroy( bd->be_acl, frontendDB->be_acl );
488 if ( bd->be_replogfile != NULL ) {
489 free( bd->be_replogfile );
491 if ( bd->be_replica_argsfile ) {
492 ch_free( bd->be_replica_argsfile );
494 if ( bd->be_replica_pidfile ) {
495 ch_free( bd->be_replica_pidfile );
497 assert( bd->be_replica == NULL );
503 BackendInfo* backend_info(const char *type)
507 /* search for the backend type */
508 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
509 if( strcasecmp(bi->bi_type, type) == 0 ) {
523 BackendInfo *bi = backend_info(type);
527 fprintf( stderr, "Unrecognized database type (%s)\n", type );
531 /* If be is provided, treat it as private. Otherwise allocate
532 * one and add it to the global list.
535 be = ch_calloc( 1, sizeof(Backend) );
537 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
542 be->be_def_limit = frontendDB->be_def_limit;
543 be->be_dfltaccess = frontendDB->be_dfltaccess;
545 be->be_restrictops = frontendDB->be_restrictops;
546 be->be_requires = frontendDB->be_requires;
547 be->be_ssf_set = frontendDB->be_ssf_set;
549 be->be_pcl_mutexp = &be->be_pcl_mutex;
550 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
552 /* assign a default depth limit for alias deref */
553 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
555 if ( bi->bi_db_init ) {
556 rc = bi->bi_db_init( be );
560 fprintf( stderr, "database init failed (%s)\n", type );
574 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
575 if ( be->bd_info->bi_db_close ) {
576 be->bd_info->bi_db_close( be );
580 if ( frontendDB->bd_info->bi_db_close ) {
581 (*frontendDB->bd_info->bi_db_close)( frontendDB );
593 ber_len_t len, dnlen = dn->bv_len;
594 Backend *be, *b2 = NULL;
596 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
597 if ( be->be_nsuffix == NULL || SLAP_DBHIDDEN( be )) {
601 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
603 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
608 len = be->be_nsuffix[j].bv_len;
611 /* suffix is longer than DN */
616 * input DN is normalized, so the separator check
617 * need not look at escaping
619 if ( len && len < dnlen &&
620 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
625 if ( strcmp( be->be_nsuffix[j].bv_val,
626 &dn->bv_val[dnlen-len] ) == 0 )
631 if( manageDSAit && len == dnlen &&
632 !SLAP_GLUE_SUBORDINATE( be ) ) {
636 /* If any parts of the tree are glued, use the first
637 * match regardless of manageDSAit. Otherwise use the
640 if( !( SLAP_DBFLAGS( be ) & ( SLAP_DBFLAG_GLUE_INSTANCE |
641 SLAP_DBFLAG_GLUE_SUBORDINATE )))
655 struct berval *bvsuffix )
659 if ( be->be_nsuffix == NULL ) {
663 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
664 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
673 be_isroot_dn( Backend *be, struct berval *ndn )
675 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
679 return dn_match( &be->be_rootndn, ndn );
683 be_slurp_update( Operation *op )
685 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
686 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
690 be_shadow_update( Operation *op )
692 /* This assumes that all internal ops (connid == -1) on a syncrepl
693 * database are syncrepl operations.
695 return (( SLAP_SYNC_SHADOW( op->o_bd ) && op->o_connid == -1 ) ||
696 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
700 be_isupdate_dn( Backend *be, struct berval *ndn )
702 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
706 return dn_match( &be->be_update_ndn, ndn );
710 be_root_dn( Backend *be )
712 return &be->be_rootdn;
716 be_isroot( Operation *op )
718 return be_isroot_dn( op->o_bd, &op->o_ndn );
722 be_isroot_pw( Operation *op )
726 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
730 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
735 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
736 op->o_conn->c_sasl_authctx, NULL );
739 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
742 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
754 if ( op->o_bd->be_release ) {
755 /* free and release entry from backend */
756 return op->o_bd->be_release( op, e, rw );
765 backend_unbind( Operation *op, SlapReply *rs )
769 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
770 if ( be->be_unbind ) {
772 be->be_unbind( op, rs );
780 backend_connection_init(
785 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
786 if ( be->be_connection_init ) {
787 be->be_connection_init( be, conn );
795 backend_connection_destroy(
800 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
801 if ( be->be_connection_destroy ) {
802 be->be_connection_destroy( be, conn);
810 backend_check_controls(
814 LDAPControl **ctrls = op->o_ctrls;
815 rs->sr_err = LDAP_SUCCESS;
818 for( ; *ctrls != NULL ; ctrls++ ) {
821 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
822 case LDAP_CONTROL_NOT_FOUND:
823 /* unrecognized control */
824 if ( (*ctrls)->ldctl_iscritical ) {
825 /* should not be reachable */
826 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
827 "unrecognized critical control: %s\n",
828 (*ctrls)->ldctl_oid, 0, 0 );
831 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
832 "unrecognized non-critical control: %s\n",
833 (*ctrls)->ldctl_oid, 0, 0 );
837 case LDAP_COMPARE_FALSE:
838 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
839 /* Per RFC 2251 (and LDAPBIS discussions), if the control
840 * is recognized and appropriate for the operation (which
841 * we've already verified), then the server should make
842 * use of the control when performing the operation.
844 * Here we find that operation extended by the control
845 * is unavailable in a particular context, and the control
846 * is marked Critical, hence the return of
847 * unwillingToPerform.
849 rs->sr_text = "critical control unavailable in context";
850 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
855 case LDAP_COMPARE_TRUE:
860 Debug( LDAP_DEBUG_ANY,
861 "backend_check_controls: unable to check control: %s\n",
862 (*ctrls)->ldctl_oid, 0, 0 );
865 rs->sr_text = "unable to check control";
866 rs->sr_err = LDAP_OTHER;
872 #if 0 /* temporarily removed */
873 /* check should be generalized */
874 if( get_relax(op) && !be_isroot(op)) {
875 rs->sr_text = "requires manager authorization";
876 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
885 backend_check_restrictions(
888 struct berval *opdata )
890 slap_mask_t restrictops;
891 slap_mask_t requires;
893 slap_mask_t exopflag = 0;
900 int rc = SLAP_CB_CONTINUE;
902 if ( op->o_bd->be_chk_controls ) {
903 rc = ( *op->o_bd->be_chk_controls )( op, rs );
906 if ( rc == SLAP_CB_CONTINUE ) {
907 rc = backend_check_controls( op, rs );
910 if ( rc != LDAP_SUCCESS ) {
914 restrictops = op->o_bd->be_restrictops;
915 requires = op->o_bd->be_requires;
916 ssf = &op->o_bd->be_ssf_set;
919 restrictops = frontendDB->be_restrictops;
920 requires = frontendDB->be_requires;
921 ssf = &frontendDB->be_ssf_set;
924 switch( op->o_tag ) {
926 opflag = SLAP_RESTRICT_OP_ADD;
930 opflag = SLAP_RESTRICT_OP_BIND;
933 case LDAP_REQ_COMPARE:
934 opflag = SLAP_RESTRICT_OP_COMPARE;
936 case LDAP_REQ_DELETE:
938 opflag = SLAP_RESTRICT_OP_DELETE;
940 case LDAP_REQ_EXTENDED:
941 opflag = SLAP_RESTRICT_OP_EXTENDED;
944 /* treat unspecified as a modify */
945 opflag = SLAP_RESTRICT_OP_MODIFY;
950 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
953 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
957 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
958 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
962 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
963 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
967 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
968 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
973 /* treat everything else as a modify */
974 opflag = SLAP_RESTRICT_OP_MODIFY;
978 case LDAP_REQ_MODIFY:
980 opflag = SLAP_RESTRICT_OP_MODIFY;
982 case LDAP_REQ_RENAME:
984 opflag = SLAP_RESTRICT_OP_RENAME;
986 case LDAP_REQ_SEARCH:
987 opflag = SLAP_RESTRICT_OP_SEARCH;
989 case LDAP_REQ_UNBIND:
994 rs->sr_text = "restrict operations internal error";
995 rs->sr_err = LDAP_OTHER;
1000 /* these checks don't apply to StartTLS */
1002 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1003 if( op->o_transport_ssf < ssf->sss_transport ) {
1004 rs->sr_text = op->o_transport_ssf
1005 ? "stronger transport confidentiality required"
1006 : "transport confidentiality required";
1010 if( op->o_tls_ssf < ssf->sss_tls ) {
1011 rs->sr_text = op->o_tls_ssf
1012 ? "stronger TLS confidentiality required"
1013 : "TLS confidentiality required";
1018 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1019 /* simple bind specific check */
1020 if( op->o_ssf < ssf->sss_simple_bind ) {
1021 rs->sr_text = op->o_ssf
1022 ? "stronger confidentiality required"
1023 : "confidentiality required";
1028 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1029 /* these checks don't apply to SASL bind */
1031 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1032 rs->sr_text = op->o_sasl_ssf
1033 ? "stronger SASL confidentiality required"
1034 : "SASL confidentiality required";
1038 if( op->o_ssf < ssf->sss_ssf ) {
1039 rs->sr_text = op->o_ssf
1040 ? "stronger confidentiality required"
1041 : "confidentiality required";
1047 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1048 rs->sr_text = op->o_transport_ssf
1049 ? "stronger transport confidentiality required for update"
1050 : "transport confidentiality required for update";
1054 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1055 rs->sr_text = op->o_tls_ssf
1056 ? "stronger TLS confidentiality required for update"
1057 : "TLS confidentiality required for update";
1061 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1062 rs->sr_text = op->o_sasl_ssf
1063 ? "stronger SASL confidentiality required for update"
1064 : "SASL confidentiality required for update";
1068 if( op->o_ssf < ssf->sss_update_ssf ) {
1069 rs->sr_text = op->o_ssf
1070 ? "stronger confidentiality required for update"
1071 : "confidentiality required for update";
1075 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1076 BER_BVISEMPTY( &op->o_ndn ) )
1078 rs->sr_text = "modifications require authentication";
1079 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1083 #ifdef SLAP_X_LISTENER_MOD
1084 if ( op->o_conn->c_listener &&
1085 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1086 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1088 /* no "w" mode means readonly */
1089 rs->sr_text = "modifications not allowed on this listener";
1090 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1093 #endif /* SLAP_X_LISTENER_MOD */
1098 /* these checks don't apply to Bind, StartTLS, or Unbind */
1100 if( requires & SLAP_REQUIRE_STRONG ) {
1101 /* should check mechanism */
1102 if( ( op->o_transport_ssf < ssf->sss_transport
1103 && op->o_authtype == LDAP_AUTH_SIMPLE )
1104 || BER_BVISEMPTY( &op->o_dn ) )
1106 rs->sr_text = "strong(er) authentication required";
1107 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1112 if( requires & SLAP_REQUIRE_SASL ) {
1113 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1114 rs->sr_text = "SASL authentication required";
1115 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1120 if( requires & SLAP_REQUIRE_AUTHC ) {
1121 if( BER_BVISEMPTY( &op->o_dn ) ) {
1122 rs->sr_text = "authentication required";
1123 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1128 if( requires & SLAP_REQUIRE_BIND ) {
1130 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1131 version = op->o_conn->c_protocol;
1132 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1135 /* no bind has occurred */
1136 rs->sr_text = "BIND required";
1137 rs->sr_err = LDAP_OPERATIONS_ERROR;
1142 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1143 if( op->o_protocol < LDAP_VERSION3 ) {
1144 /* no bind has occurred */
1145 rs->sr_text = "operation restricted to LDAPv3 clients";
1146 rs->sr_err = LDAP_OPERATIONS_ERROR;
1151 #ifdef SLAP_X_LISTENER_MOD
1152 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1153 if ( op->o_conn->c_listener &&
1154 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1156 /* no "x" mode means bind required */
1157 rs->sr_text = "bind required on this listener";
1158 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1163 if ( !starttls && !updateop ) {
1164 if ( op->o_conn->c_listener &&
1165 !( op->o_conn->c_listener->sl_perms &
1166 ( !BER_BVISEMPTY( &op->o_dn )
1167 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1169 /* no "r" mode means no read */
1170 rs->sr_text = "read not allowed on this listener";
1171 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1175 #endif /* SLAP_X_LISTENER_MOD */
1179 if( ( restrictops & opflag )
1180 || ( exopflag && ( restrictops & exopflag ) ) ) {
1181 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1182 rs->sr_text = "read operations restricted";
1183 } else if ( restrictops & exopflag ) {
1184 rs->sr_text = "extended operation restricted";
1186 rs->sr_text = "operation restricted";
1188 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1192 rs->sr_err = LDAP_SUCCESS;
1196 int backend_check_referrals( Operation *op, SlapReply *rs )
1198 rs->sr_err = LDAP_SUCCESS;
1200 if( op->o_bd->be_chk_referrals ) {
1201 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1203 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1204 send_ldap_result( op, rs );
1216 AttributeDescription *at,
1222 if ( op->o_bd == NULL ) {
1223 return LDAP_NO_SUCH_OBJECT;
1226 if ( op->o_bd->be_fetch ) {
1227 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1230 return LDAP_UNWILLING_TO_PERFORM;
1237 struct berval *gr_ndn,
1238 struct berval *op_ndn,
1239 ObjectClass *group_oc,
1240 AttributeDescription *group_at )
1243 void *o_priv = op->o_private, *e_priv = NULL;
1247 Backend *be = op->o_bd;
1249 op->o_bd = select_backend( gr_ndn, 0, 0 );
1251 for ( g = op->o_groups; g; g = g->ga_next ) {
1252 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1253 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1257 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1267 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1271 op->o_private = NULL;
1272 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1273 e_priv = op->o_private;
1274 op->o_private = o_priv;
1277 a = attr_find( e->e_attrs, group_at );
1279 /* If the attribute is a subtype of labeledURI, treat this as
1280 * a dynamic group ala groupOfURLs
1282 if (is_at_subtype( group_at->ad_type,
1283 slap_schema.si_ad_labeledURI->ad_type ) )
1287 struct berval bv, nbase;
1290 void *user_priv = NULL;
1291 Backend *b2 = op->o_bd;
1293 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1296 op->o_bd = select_backend( op_ndn, 0, 0 );
1297 op->o_private = NULL;
1298 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1299 user_priv = op->o_private;
1300 op->o_private = o_priv;
1304 rc = LDAP_COMPARE_FALSE;
1305 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1306 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1311 BER_BVZERO( &nbase );
1312 /* host part must be empty */
1313 /* attrs and extensions parts must be empty */
1314 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1315 ludp->lud_attrs || ludp->lud_exts )
1319 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1320 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1321 op->o_tmpmemctx ) != LDAP_SUCCESS )
1325 switch ( ludp->lud_scope ) {
1326 case LDAP_SCOPE_BASE:
1327 if ( !dn_match( &nbase, op_ndn ) ) {
1331 case LDAP_SCOPE_ONELEVEL:
1332 dnParent( op_ndn, &bv );
1333 if ( !dn_match( &nbase, &bv ) ) {
1337 case LDAP_SCOPE_SUBTREE:
1338 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1342 case LDAP_SCOPE_SUBORDINATE:
1343 if ( dn_match( &nbase, op_ndn ) ||
1344 !dnIsSuffix( op_ndn, &nbase ) )
1349 filter = str2filter_x( op, ludp->lud_filter );
1351 if ( test_filter( NULL, user, filter ) ==
1356 filter_free_x( op, filter );
1359 ldap_free_urldesc( ludp );
1360 if ( !BER_BVISNULL( &nbase ) ) {
1361 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1363 if ( rc == 0 ) break;
1365 if ( user != target ) {
1366 op->o_private = user_priv;
1367 be_entry_release_r( op, user );
1368 op->o_private = o_priv;
1373 rc = value_find_ex( group_at,
1374 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1375 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1376 a->a_nvals, op_ndn, op->o_tmpmemctx );
1377 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1378 rc = LDAP_COMPARE_FALSE;
1381 rc = LDAP_NO_SUCH_ATTRIBUTE;
1383 if ( e != target ) {
1384 op->o_private = e_priv;
1385 be_entry_release_r( op, e );
1386 op->o_private = o_priv;
1389 rc = LDAP_NO_SUCH_OBJECT;
1392 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1393 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1395 g->ga_be = op->o_bd;
1396 g->ga_oc = group_oc;
1397 g->ga_at = group_at;
1399 g->ga_len = gr_ndn->bv_len;
1400 strcpy( g->ga_ndn, gr_ndn->bv_val );
1401 g->ga_next = op->o_groups;
1413 struct berval *gr_ndn,
1414 struct berval *op_ndn,
1415 ObjectClass *group_oc,
1416 AttributeDescription *group_at )
1421 if ( op->o_abandon ) {
1422 return SLAPD_ABANDON;
1426 op->o_bd = frontendDB;
1427 rc = frontendDB->be_group( op, target, gr_ndn,
1428 op_ndn, group_oc, group_at );
1439 AttributeDescription *entry_at,
1441 slap_access_t access )
1444 void *o_priv = op->o_private, *e_priv = NULL;
1445 Attribute *a = NULL;
1446 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1447 AccessControlState acl_state = ACL_STATE_INIT;
1448 Backend *be = op->o_bd;
1450 op->o_bd = select_backend( edn, 0, 0 );
1452 if ( target && dn_match( &target->e_nname, edn ) ) {
1456 op->o_private = NULL;
1457 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1458 e_priv = op->o_private;
1459 op->o_private = o_priv;
1463 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1464 assert( vals == NULL );
1467 if ( op->o_conn && access > ACL_NONE &&
1468 access_allowed( op, e, entry_at, NULL,
1469 access, &acl_state ) == 0 )
1471 rc = LDAP_INSUFFICIENT_ACCESS;
1476 a = attr_find( e->e_attrs, entry_at );
1478 SlapReply rs = { 0 };
1479 AttributeName anlist[ 2 ];
1481 anlist[ 0 ].an_name = entry_at->ad_cname;
1482 anlist[ 0 ].an_desc = entry_at;
1483 BER_BVZERO( &anlist[ 1 ].an_name );
1484 rs.sr_attrs = anlist;
1486 /* NOTE: backend_operational() is also called
1487 * when returning results, so it's supposed
1488 * to do no harm to entries */
1490 rc = backend_operational( op, &rs );
1493 if ( rc == LDAP_SUCCESS ) {
1494 if ( rs.sr_operational_attrs ) {
1496 a = rs.sr_operational_attrs;
1499 rc = LDAP_NO_SUCH_ATTRIBUTE;
1507 if ( op->o_conn && access > ACL_NONE &&
1508 access_allowed( op, e, entry_at, NULL,
1509 access, &acl_state ) == 0 )
1511 rc = LDAP_INSUFFICIENT_ACCESS;
1515 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1518 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1520 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1522 if ( op->o_conn && access > ACL_NONE &&
1523 access_allowed( op, e, entry_at,
1530 ber_dupbv_x( &v[j], &a->a_nvals[i],
1532 if ( !BER_BVISNULL( &v[j] ) ) {
1537 op->o_tmpfree( v, op->o_tmpmemctx );
1539 rc = LDAP_INSUFFICIENT_ACCESS;
1542 BER_BVZERO( &v[j] );
1547 freeit: if ( e != target ) {
1548 op->o_private = e_priv;
1549 be_entry_release_r( op, e );
1550 op->o_private = o_priv;
1566 AttributeDescription *entry_at,
1568 slap_access_t access )
1574 op->o_bd = frontendDB;
1575 rc = frontendDB->be_attribute( op, target, edn,
1576 entry_at, vals, access );
1587 AttributeDescription *entry_at,
1588 struct berval *nval,
1589 slap_access_t access,
1593 void *o_priv = op->o_private, *e_priv = NULL;
1594 int rc = LDAP_INSUFFICIENT_ACCESS;
1595 Backend *be = op->o_bd;
1598 assert( op != NULL );
1599 assert( op->o_conn != NULL );
1600 assert( edn != NULL );
1601 assert( access > ACL_NONE );
1603 op->o_bd = select_backend( edn, 0, 0 );
1605 if ( target && dn_match( &target->e_nname, edn ) ) {
1609 op->o_private = NULL;
1610 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1611 e_priv = op->o_private;
1612 op->o_private = o_priv;
1616 Attribute *a = NULL;
1619 if ( entry_at == NULL ) {
1620 entry_at = slap_schema.si_ad_entry;
1623 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1625 if ( access_allowed_mask( op, e, entry_at,
1626 NULL, access, NULL, mask ) == 0 )
1628 rc = LDAP_INSUFFICIENT_ACCESS;
1635 a = attr_find( e->e_attrs, entry_at );
1637 SlapReply rs = { 0 };
1638 AttributeName anlist[ 2 ];
1640 anlist[ 0 ].an_name = entry_at->ad_cname;
1641 anlist[ 0 ].an_desc = entry_at;
1642 BER_BVZERO( &anlist[ 1 ].an_name );
1643 rs.sr_attrs = anlist;
1645 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1647 /* NOTE: backend_operational() is also called
1648 * when returning results, so it's supposed
1649 * to do no harm to entries */
1651 rc = backend_operational( op, &rs );
1654 if ( rc == LDAP_SUCCESS ) {
1655 if ( rs.sr_operational_attrs ) {
1657 a = rs.sr_operational_attrs;
1660 rc = LDAP_NO_SUCH_OBJECT;
1666 if ( access_allowed_mask( op, e, entry_at,
1667 nval, access, NULL, mask ) == 0 )
1669 rc = LDAP_INSUFFICIENT_ACCESS;
1675 freeit: if ( e != target ) {
1676 op->o_private = e_priv;
1677 be_entry_release_r( op, e );
1678 op->o_private = o_priv;
1698 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1699 /* just count them */ ;
1702 * If operational attributes (allegedly) are required,
1703 * and the backend supports specific operational attributes,
1704 * add them to the attribute list
1706 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1707 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1708 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1710 *ap = slap_operational_entryDN( rs->sr_entry );
1711 ap = &(*ap)->a_next;
1714 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1715 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1716 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1718 *ap = slap_operational_subschemaSubentry( op->o_bd );
1719 ap = &(*ap)->a_next;
1722 if ( op->o_bd != NULL )
1724 /* Let the overlays have a chance at this */
1726 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
1727 if ( !be_match( op->o_bd, frontendDB ) &&
1728 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1729 op->o_bd != NULL && op->o_bd->be_operational != NULL )
1731 rc = op->o_bd->be_operational( op, rs );
1739 int backend_operational( Operation *op, SlapReply *rs )
1744 /* Moved this into the frontend so global overlays are called */
1747 op->o_bd = frontendDB;
1748 rc = frontendDB->be_operational( op, rs );