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, slap_backend_db, 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 ( bd->be_replogfile ) {
444 ch_free( bd->be_replogfile );
446 if ( bd->be_replica_argsfile ) {
447 ch_free( bd->be_replica_argsfile );
449 if ( bd->be_replica_pidfile ) {
450 ch_free( bd->be_replica_pidfile );
452 destroy_replica_info( bd );
453 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
454 ch_free( bd->be_update_ndn.bv_val );
456 if ( bd->be_update_refs ) {
457 ber_bvarray_free( bd->be_update_refs );
465 int backend_destroy(void)
470 /* destroy each backend database */
471 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
472 backend_destroy_one( bd, 1 );
475 /* destroy each backend type */
476 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
477 if( bi->bi_destroy ) {
478 bi->bi_destroy( bi );
483 LDAP_STAILQ_INIT(&backendInfo);
485 /* destroy frontend database */
488 if ( bd->bd_info->bi_db_destroy ) {
489 bd->bd_info->bi_db_destroy( bd );
491 ber_bvarray_free( bd->be_suffix );
492 ber_bvarray_free( bd->be_nsuffix );
493 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
494 free( bd->be_rootdn.bv_val );
496 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
497 free( bd->be_rootndn.bv_val );
499 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
500 free( bd->be_rootpw.bv_val );
502 acl_destroy( bd->be_acl, frontendDB->be_acl );
504 if ( bd->be_replogfile != NULL ) {
505 free( bd->be_replogfile );
507 if ( bd->be_replica_argsfile ) {
508 ch_free( bd->be_replica_argsfile );
510 if ( bd->be_replica_pidfile ) {
511 ch_free( bd->be_replica_pidfile );
513 assert( bd->be_replica == NULL );
519 BackendInfo* backend_info(const char *type)
523 /* search for the backend type */
524 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
525 if( strcasecmp(bi->bi_type, type) == 0 ) {
539 /* If idx < 0, just add to end of list */
541 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
542 } else if ( idx == 0 ) {
543 LDAP_STAILQ_INSERT_HEAD(&backendDB, be, be_next);
548 b2 = LDAP_STAILQ_FIRST(&backendDB);
550 for (i=0; i<idx; i++) {
551 b2 = LDAP_STAILQ_NEXT(b2, be_next);
553 LDAP_STAILQ_INSERT_AFTER(&backendDB, b2, be, be_next);
563 LDAP_STAILQ_REMOVE(&backendDB, be, slap_backend_db, be_next);
564 backend_db_insert(be, idx);
573 BackendInfo *bi = backend_info(type);
578 fprintf( stderr, "Unrecognized database type (%s)\n", type );
582 /* If be is provided, treat it as private. Otherwise allocate
583 * one and add it to the global list.
586 be = ch_calloc( 1, sizeof(Backend) );
588 if ( idx >= nbackends )
591 backend_db_insert( be, idx );
596 be->be_def_limit = frontendDB->be_def_limit;
597 be->be_dfltaccess = frontendDB->be_dfltaccess;
599 be->be_restrictops = frontendDB->be_restrictops;
600 be->be_requires = frontendDB->be_requires;
601 be->be_ssf_set = frontendDB->be_ssf_set;
603 be->be_pcl_mutexp = &be->be_pcl_mutex;
604 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
606 /* assign a default depth limit for alias deref */
607 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
609 if ( bi->bi_db_init ) {
610 rc = bi->bi_db_init( be );
614 fprintf( stderr, "database init failed (%s)\n", type );
615 /* If we created and linked this be, remove it and free it */
617 LDAP_STAILQ_REMOVE(&backendDB, be, slap_backend_db, be_next);
633 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
634 if ( be->bd_info->bi_db_close ) {
635 be->bd_info->bi_db_close( be );
639 if ( frontendDB->bd_info->bi_db_close ) {
640 frontendDB->bd_info->bi_db_close( frontendDB );
652 ber_len_t len, dnlen = dn->bv_len;
653 Backend *be, *b2 = NULL;
655 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
656 if ( be->be_nsuffix == NULL || SLAP_DBHIDDEN( be )) {
660 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
662 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
667 len = be->be_nsuffix[j].bv_len;
670 /* suffix is longer than DN */
675 * input DN is normalized, so the separator check
676 * need not look at escaping
678 if ( len && len < dnlen &&
679 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
684 if ( strcmp( be->be_nsuffix[j].bv_val,
685 &dn->bv_val[dnlen-len] ) == 0 )
690 if( manageDSAit && len == dnlen &&
691 !SLAP_GLUE_SUBORDINATE( be ) ) {
695 /* If any parts of the tree are glued, use the first
696 * match regardless of manageDSAit. Otherwise use the
699 if( !( SLAP_DBFLAGS( be ) & ( SLAP_DBFLAG_GLUE_INSTANCE |
700 SLAP_DBFLAG_GLUE_SUBORDINATE )))
714 struct berval *bvsuffix )
718 if ( be->be_nsuffix == NULL ) {
722 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
723 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
732 be_isroot_dn( Backend *be, struct berval *ndn )
734 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
738 return dn_match( &be->be_rootndn, ndn );
742 be_slurp_update( Operation *op )
744 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
745 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
749 be_shadow_update( Operation *op )
751 /* This assumes that all internal ops (connid == -1) on a syncrepl
752 * database are syncrepl operations.
754 return (( SLAP_SYNC_SHADOW( op->o_bd ) && op->o_connid == -1 ) ||
755 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
759 be_isupdate_dn( Backend *be, struct berval *ndn )
761 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
765 return dn_match( &be->be_update_ndn, ndn );
769 be_root_dn( Backend *be )
771 return &be->be_rootdn;
775 be_isroot( Operation *op )
777 return be_isroot_dn( op->o_bd, &op->o_ndn );
781 be_isroot_pw( Operation *op )
785 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
789 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
794 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
795 op->o_conn->c_sasl_authctx, NULL );
798 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
801 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
813 if ( op->o_bd->be_release ) {
814 /* free and release entry from backend */
815 return op->o_bd->be_release( op, e, rw );
824 backend_unbind( Operation *op, SlapReply *rs )
828 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
829 if ( be->be_unbind ) {
831 be->be_unbind( op, rs );
839 backend_connection_init(
844 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
845 if ( be->be_connection_init ) {
846 be->be_connection_init( be, conn );
854 backend_connection_destroy(
859 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
860 if ( be->be_connection_destroy ) {
861 be->be_connection_destroy( be, conn);
869 backend_check_controls(
873 LDAPControl **ctrls = op->o_ctrls;
874 rs->sr_err = LDAP_SUCCESS;
877 for( ; *ctrls != NULL ; ctrls++ ) {
880 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
881 case LDAP_CONTROL_NOT_FOUND:
882 /* unrecognized control */
883 if ( (*ctrls)->ldctl_iscritical ) {
884 /* should not be reachable */
885 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
886 "unrecognized critical control: %s\n",
887 (*ctrls)->ldctl_oid, 0, 0 );
890 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
891 "unrecognized non-critical control: %s\n",
892 (*ctrls)->ldctl_oid, 0, 0 );
896 case LDAP_COMPARE_FALSE:
897 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
898 /* RFC 4511 allows unavailableCriticalExtension to be
899 * returned when the server is unwilling to perform
900 * an operation extended by a recognized critical
903 rs->sr_text = "critical control unavailable in context";
904 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
909 case LDAP_COMPARE_TRUE:
914 Debug( LDAP_DEBUG_ANY,
915 "backend_check_controls: unable to check control: %s\n",
916 (*ctrls)->ldctl_oid, 0, 0 );
919 rs->sr_text = "unable to check control";
920 rs->sr_err = LDAP_OTHER;
926 #if 0 /* temporarily removed */
927 /* check should be generalized */
928 if( get_relax(op) && !be_isroot(op)) {
929 rs->sr_text = "requires manager authorization";
930 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
939 backend_check_restrictions(
942 struct berval *opdata )
944 slap_mask_t restrictops;
945 slap_mask_t requires;
947 slap_mask_t exopflag = 0;
954 int rc = SLAP_CB_CONTINUE;
956 if ( op->o_bd->be_chk_controls ) {
957 rc = ( *op->o_bd->be_chk_controls )( op, rs );
960 if ( rc == SLAP_CB_CONTINUE ) {
961 rc = backend_check_controls( op, rs );
964 if ( rc != LDAP_SUCCESS ) {
968 restrictops = op->o_bd->be_restrictops;
969 requires = op->o_bd->be_requires;
970 ssf = &op->o_bd->be_ssf_set;
973 restrictops = frontendDB->be_restrictops;
974 requires = frontendDB->be_requires;
975 ssf = &frontendDB->be_ssf_set;
978 switch( op->o_tag ) {
980 opflag = SLAP_RESTRICT_OP_ADD;
984 opflag = SLAP_RESTRICT_OP_BIND;
987 case LDAP_REQ_COMPARE:
988 opflag = SLAP_RESTRICT_OP_COMPARE;
990 case LDAP_REQ_DELETE:
992 opflag = SLAP_RESTRICT_OP_DELETE;
994 case LDAP_REQ_EXTENDED:
995 opflag = SLAP_RESTRICT_OP_EXTENDED;
998 /* treat unspecified as a modify */
999 opflag = SLAP_RESTRICT_OP_MODIFY;
1004 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1007 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1011 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1012 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1016 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1017 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1021 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1022 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1027 /* treat everything else as a modify */
1028 opflag = SLAP_RESTRICT_OP_MODIFY;
1032 case LDAP_REQ_MODIFY:
1034 opflag = SLAP_RESTRICT_OP_MODIFY;
1036 case LDAP_REQ_RENAME:
1038 opflag = SLAP_RESTRICT_OP_RENAME;
1040 case LDAP_REQ_SEARCH:
1041 opflag = SLAP_RESTRICT_OP_SEARCH;
1043 case LDAP_REQ_UNBIND:
1048 rs->sr_text = "restrict operations internal error";
1049 rs->sr_err = LDAP_OTHER;
1054 /* these checks don't apply to StartTLS */
1056 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1057 if( op->o_transport_ssf < ssf->sss_transport ) {
1058 rs->sr_text = op->o_transport_ssf
1059 ? "stronger transport confidentiality required"
1060 : "transport confidentiality required";
1064 if( op->o_tls_ssf < ssf->sss_tls ) {
1065 rs->sr_text = op->o_tls_ssf
1066 ? "stronger TLS confidentiality required"
1067 : "TLS confidentiality required";
1072 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1073 /* simple bind specific check */
1074 if( op->o_ssf < ssf->sss_simple_bind ) {
1075 rs->sr_text = op->o_ssf
1076 ? "stronger confidentiality required"
1077 : "confidentiality required";
1082 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1083 /* these checks don't apply to SASL bind */
1085 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1086 rs->sr_text = op->o_sasl_ssf
1087 ? "stronger SASL confidentiality required"
1088 : "SASL confidentiality required";
1092 if( op->o_ssf < ssf->sss_ssf ) {
1093 rs->sr_text = op->o_ssf
1094 ? "stronger confidentiality required"
1095 : "confidentiality required";
1101 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1102 rs->sr_text = op->o_transport_ssf
1103 ? "stronger transport confidentiality required for update"
1104 : "transport confidentiality required for update";
1108 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1109 rs->sr_text = op->o_tls_ssf
1110 ? "stronger TLS confidentiality required for update"
1111 : "TLS confidentiality required for update";
1115 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1116 rs->sr_text = op->o_sasl_ssf
1117 ? "stronger SASL confidentiality required for update"
1118 : "SASL confidentiality required for update";
1122 if( op->o_ssf < ssf->sss_update_ssf ) {
1123 rs->sr_text = op->o_ssf
1124 ? "stronger confidentiality required for update"
1125 : "confidentiality required for update";
1129 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1130 BER_BVISEMPTY( &op->o_ndn ) )
1132 rs->sr_text = "modifications require authentication";
1133 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1137 #ifdef SLAP_X_LISTENER_MOD
1138 if ( op->o_conn->c_listener &&
1139 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1140 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1142 /* no "w" mode means readonly */
1143 rs->sr_text = "modifications not allowed on this listener";
1144 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1147 #endif /* SLAP_X_LISTENER_MOD */
1152 /* these checks don't apply to Bind, StartTLS, or Unbind */
1154 if( requires & SLAP_REQUIRE_STRONG ) {
1155 /* should check mechanism */
1156 if( ( op->o_transport_ssf < ssf->sss_transport
1157 && op->o_authtype == LDAP_AUTH_SIMPLE )
1158 || BER_BVISEMPTY( &op->o_dn ) )
1160 rs->sr_text = "strong(er) authentication required";
1161 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1166 if( requires & SLAP_REQUIRE_SASL ) {
1167 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1168 rs->sr_text = "SASL authentication required";
1169 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1174 if( requires & SLAP_REQUIRE_AUTHC ) {
1175 if( BER_BVISEMPTY( &op->o_dn ) ) {
1176 rs->sr_text = "authentication required";
1177 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1182 if( requires & SLAP_REQUIRE_BIND ) {
1184 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1185 version = op->o_conn->c_protocol;
1186 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1189 /* no bind has occurred */
1190 rs->sr_text = "BIND required";
1191 rs->sr_err = LDAP_OPERATIONS_ERROR;
1196 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1197 if( op->o_protocol < LDAP_VERSION3 ) {
1198 /* no bind has occurred */
1199 rs->sr_text = "operation restricted to LDAPv3 clients";
1200 rs->sr_err = LDAP_OPERATIONS_ERROR;
1205 #ifdef SLAP_X_LISTENER_MOD
1206 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1207 if ( op->o_conn->c_listener &&
1208 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1210 /* no "x" mode means bind required */
1211 rs->sr_text = "bind required on this listener";
1212 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1217 if ( !starttls && !updateop ) {
1218 if ( op->o_conn->c_listener &&
1219 !( op->o_conn->c_listener->sl_perms &
1220 ( !BER_BVISEMPTY( &op->o_dn )
1221 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1223 /* no "r" mode means no read */
1224 rs->sr_text = "read not allowed on this listener";
1225 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1229 #endif /* SLAP_X_LISTENER_MOD */
1233 if( ( restrictops & opflag )
1234 || ( exopflag && ( restrictops & exopflag ) ) ) {
1235 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1236 rs->sr_text = "read operations restricted";
1237 } else if ( restrictops & exopflag ) {
1238 rs->sr_text = "extended operation restricted";
1240 rs->sr_text = "operation restricted";
1242 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1246 rs->sr_err = LDAP_SUCCESS;
1250 int backend_check_referrals( Operation *op, SlapReply *rs )
1252 rs->sr_err = LDAP_SUCCESS;
1254 if( op->o_bd->be_chk_referrals ) {
1255 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1257 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1258 send_ldap_result( op, rs );
1270 AttributeDescription *at,
1276 if ( op->o_bd == NULL ) {
1277 return LDAP_NO_SUCH_OBJECT;
1280 if ( op->o_bd->be_fetch ) {
1281 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1284 return LDAP_UNWILLING_TO_PERFORM;
1291 struct berval *gr_ndn,
1292 struct berval *op_ndn,
1293 ObjectClass *group_oc,
1294 AttributeDescription *group_at )
1297 void *o_priv = op->o_private, *e_priv = NULL;
1301 Backend *be = op->o_bd;
1303 op->o_bd = select_backend( gr_ndn, 0, 0 );
1305 for ( g = op->o_groups; g; g = g->ga_next ) {
1306 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1307 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1311 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1321 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1325 op->o_private = NULL;
1326 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1327 e_priv = op->o_private;
1328 op->o_private = o_priv;
1331 a = attr_find( e->e_attrs, group_at );
1333 /* If the attribute is a subtype of labeledURI, treat this as
1334 * a dynamic group ala groupOfURLs
1336 if (is_at_subtype( group_at->ad_type,
1337 slap_schema.si_ad_labeledURI->ad_type ) )
1341 struct berval bv, nbase;
1344 void *user_priv = NULL;
1345 Backend *b2 = op->o_bd;
1347 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1350 op->o_bd = select_backend( op_ndn, 0, 0 );
1351 op->o_private = NULL;
1352 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1353 user_priv = op->o_private;
1354 op->o_private = o_priv;
1358 rc = LDAP_COMPARE_FALSE;
1359 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1360 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1365 BER_BVZERO( &nbase );
1366 /* host part must be empty */
1367 /* attrs and extensions parts must be empty */
1368 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1369 ludp->lud_attrs || ludp->lud_exts )
1373 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1374 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1375 op->o_tmpmemctx ) != LDAP_SUCCESS )
1379 switch ( ludp->lud_scope ) {
1380 case LDAP_SCOPE_BASE:
1381 if ( !dn_match( &nbase, op_ndn ) ) {
1385 case LDAP_SCOPE_ONELEVEL:
1386 dnParent( op_ndn, &bv );
1387 if ( !dn_match( &nbase, &bv ) ) {
1391 case LDAP_SCOPE_SUBTREE:
1392 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1396 case LDAP_SCOPE_SUBORDINATE:
1397 if ( dn_match( &nbase, op_ndn ) ||
1398 !dnIsSuffix( op_ndn, &nbase ) )
1403 filter = str2filter_x( op, ludp->lud_filter );
1405 if ( test_filter( NULL, user, filter ) ==
1410 filter_free_x( op, filter );
1413 ldap_free_urldesc( ludp );
1414 if ( !BER_BVISNULL( &nbase ) ) {
1415 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1417 if ( rc == 0 ) break;
1419 if ( user != target ) {
1420 op->o_private = user_priv;
1421 be_entry_release_r( op, user );
1422 op->o_private = o_priv;
1427 rc = value_find_ex( group_at,
1428 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1429 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1430 a->a_nvals, op_ndn, op->o_tmpmemctx );
1431 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1432 rc = LDAP_COMPARE_FALSE;
1435 rc = LDAP_NO_SUCH_ATTRIBUTE;
1437 if ( e != target ) {
1438 op->o_private = e_priv;
1439 be_entry_release_r( op, e );
1440 op->o_private = o_priv;
1443 rc = LDAP_NO_SUCH_OBJECT;
1446 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1447 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1449 g->ga_be = op->o_bd;
1450 g->ga_oc = group_oc;
1451 g->ga_at = group_at;
1453 g->ga_len = gr_ndn->bv_len;
1454 strcpy( g->ga_ndn, gr_ndn->bv_val );
1455 g->ga_next = op->o_groups;
1467 struct berval *gr_ndn,
1468 struct berval *op_ndn,
1469 ObjectClass *group_oc,
1470 AttributeDescription *group_at )
1475 if ( op->o_abandon ) {
1476 return SLAPD_ABANDON;
1480 op->o_bd = frontendDB;
1481 rc = frontendDB->be_group( op, target, gr_ndn,
1482 op_ndn, group_oc, group_at );
1493 AttributeDescription *entry_at,
1495 slap_access_t access )
1498 void *o_priv = op->o_private, *e_priv = NULL;
1499 Attribute *a = NULL;
1500 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1501 AccessControlState acl_state = ACL_STATE_INIT;
1502 Backend *be = op->o_bd;
1504 op->o_bd = select_backend( edn, 0, 0 );
1506 if ( target && dn_match( &target->e_nname, edn ) ) {
1510 op->o_private = NULL;
1511 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1512 e_priv = op->o_private;
1513 op->o_private = o_priv;
1517 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1518 assert( vals == NULL );
1521 if ( op->o_conn && access > ACL_NONE &&
1522 access_allowed( op, e, entry_at, NULL,
1523 access, &acl_state ) == 0 )
1525 rc = LDAP_INSUFFICIENT_ACCESS;
1530 a = attr_find( e->e_attrs, entry_at );
1532 SlapReply rs = { 0 };
1533 AttributeName anlist[ 2 ];
1535 anlist[ 0 ].an_name = entry_at->ad_cname;
1536 anlist[ 0 ].an_desc = entry_at;
1537 BER_BVZERO( &anlist[ 1 ].an_name );
1538 rs.sr_attrs = anlist;
1540 /* NOTE: backend_operational() is also called
1541 * when returning results, so it's supposed
1542 * to do no harm to entries */
1544 rc = backend_operational( op, &rs );
1547 if ( rc == LDAP_SUCCESS ) {
1548 if ( rs.sr_operational_attrs ) {
1550 a = rs.sr_operational_attrs;
1553 rc = LDAP_NO_SUCH_ATTRIBUTE;
1561 if ( op->o_conn && access > ACL_NONE &&
1562 access_allowed( op, e, entry_at, NULL,
1563 access, &acl_state ) == 0 )
1565 rc = LDAP_INSUFFICIENT_ACCESS;
1569 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1572 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1574 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1576 if ( op->o_conn && access > ACL_NONE &&
1577 access_allowed( op, e, entry_at,
1584 ber_dupbv_x( &v[j], &a->a_nvals[i],
1586 if ( !BER_BVISNULL( &v[j] ) ) {
1591 op->o_tmpfree( v, op->o_tmpmemctx );
1593 rc = LDAP_INSUFFICIENT_ACCESS;
1596 BER_BVZERO( &v[j] );
1601 freeit: if ( e != target ) {
1602 op->o_private = e_priv;
1603 be_entry_release_r( op, e );
1604 op->o_private = o_priv;
1620 AttributeDescription *entry_at,
1622 slap_access_t access )
1628 op->o_bd = frontendDB;
1629 rc = frontendDB->be_attribute( op, target, edn,
1630 entry_at, vals, access );
1641 AttributeDescription *entry_at,
1642 struct berval *nval,
1643 slap_access_t access,
1647 void *o_priv = op->o_private, *e_priv = NULL;
1648 int rc = LDAP_INSUFFICIENT_ACCESS;
1649 Backend *be = op->o_bd;
1652 assert( op != NULL );
1653 assert( op->o_conn != NULL );
1654 assert( edn != NULL );
1655 assert( access > ACL_NONE );
1657 op->o_bd = select_backend( edn, 0, 0 );
1659 if ( target && dn_match( &target->e_nname, edn ) ) {
1663 op->o_private = NULL;
1664 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1665 e_priv = op->o_private;
1666 op->o_private = o_priv;
1670 Attribute *a = NULL;
1673 if ( entry_at == NULL ) {
1674 entry_at = slap_schema.si_ad_entry;
1677 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1679 if ( access_allowed_mask( op, e, entry_at,
1680 NULL, access, NULL, mask ) == 0 )
1682 rc = LDAP_INSUFFICIENT_ACCESS;
1689 a = attr_find( e->e_attrs, entry_at );
1691 SlapReply rs = { 0 };
1692 AttributeName anlist[ 2 ];
1694 anlist[ 0 ].an_name = entry_at->ad_cname;
1695 anlist[ 0 ].an_desc = entry_at;
1696 BER_BVZERO( &anlist[ 1 ].an_name );
1697 rs.sr_attrs = anlist;
1699 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1701 /* NOTE: backend_operational() is also called
1702 * when returning results, so it's supposed
1703 * to do no harm to entries */
1705 rc = backend_operational( op, &rs );
1708 if ( rc == LDAP_SUCCESS ) {
1709 if ( rs.sr_operational_attrs ) {
1711 a = rs.sr_operational_attrs;
1714 rc = LDAP_NO_SUCH_OBJECT;
1720 if ( access_allowed_mask( op, e, entry_at,
1721 nval, access, NULL, mask ) == 0 )
1723 rc = LDAP_INSUFFICIENT_ACCESS;
1729 freeit: if ( e != target ) {
1730 op->o_private = e_priv;
1731 be_entry_release_r( op, e );
1732 op->o_private = o_priv;
1751 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1752 /* just count them */ ;
1755 * If operational attributes (allegedly) are required,
1756 * and the backend supports specific operational attributes,
1757 * add them to the attribute list
1759 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1760 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1761 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1763 *ap = slap_operational_entryDN( rs->sr_entry );
1764 ap = &(*ap)->a_next;
1767 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1768 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1769 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1771 *ap = slap_operational_subschemaSubentry( op->o_bd );
1772 ap = &(*ap)->a_next;
1775 if ( op->o_bd != NULL ) {
1776 BackendDB *be_orig = op->o_bd;
1778 /* Let the overlays have a chance at this */
1779 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
1780 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1781 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1782 op->o_bd->be_operational != NULL )
1784 rc = op->o_bd->be_operational( op, rs );
1792 int backend_operational( Operation *op, SlapReply *rs )
1797 /* Moved this into the frontend so global overlays are called */
1800 op->o_bd = frontendDB;
1801 rc = frontendDB->be_operational( op, rs );