1 /* backend.c - routines for dealing with back-end databases */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/string.h>
33 #include <ac/socket.h>
42 * If a module is configured as dynamic, its header should not
43 * get included into slapd. While this is a general rule and does
44 * not have much of an effect in UNIX, this rule should be adhered
45 * to for Windows, where dynamic object code should not be implicitly
46 * imported into slapd without appropriate __declspec(dllimport) directives.
50 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
53 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
56 backend_init_controls( BackendInfo *bi )
58 if ( bi->bi_controls ) {
61 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
64 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
65 == LDAP_CONTROL_NOT_FOUND )
67 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
74 bi->bi_ctrls[ cid ] = 1;
81 int backend_init(void)
86 if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
87 /* already initialized */
88 Debug( LDAP_DEBUG_ANY,
89 "backend_init: already initialized\n", 0, 0, 0 );
93 for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ ) {
94 assert( bi->bi_init != 0 );
96 rc = bi->bi_init( bi );
99 Debug( LDAP_DEBUG_ANY,
100 "backend_init: initialized for type \"%s\"\n",
102 /* destroy those we've already inited */
107 if ( slap_binfo[nBackendInfo].bi_destroy ) {
108 slap_binfo[nBackendInfo].bi_destroy(
109 &slap_binfo[nBackendInfo] );
115 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
118 if ( nBackendInfo > 0) {
126 Debug( LDAP_DEBUG_ANY,
127 "backend_init: failed\n",
131 #endif /* SLAPD_MODULES */
134 int backend_add(BackendInfo *aBackendInfo)
138 if ( aBackendInfo->bi_init == NULL ) {
139 Debug( LDAP_DEBUG_ANY, "backend_add: "
140 "backend type \"%s\" does not have the (mandatory)init function\n",
141 aBackendInfo->bi_type, 0, 0 );
145 rc = aBackendInfo->bi_init(aBackendInfo);
147 Debug( LDAP_DEBUG_ANY,
148 "backend_add: initialization for type \"%s\" failed\n",
149 aBackendInfo->bi_type, 0, 0 );
153 (void)backend_init_controls( aBackendInfo );
155 /* now add the backend type to the Backend Info List */
156 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
162 backend_set_controls( BackendDB *be )
164 BackendInfo *bi = be->bd_info;
166 /* back-relay takes care of itself; so may do other */
167 if ( overlay_is_over( be ) ) {
168 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
171 if ( bi->bi_controls ) {
172 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
173 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
174 sizeof( be->be_ctrls ) );
175 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
180 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
181 if ( bi->bi_ctrls[ i ] ) {
182 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
192 /* startup a specific backend database */
193 int backend_startup_one(Backend *be, ConfigReply *cr)
197 assert( be != NULL );
199 be->be_pending_csn_list = (struct be_pcl *)
200 ch_calloc( 1, sizeof( struct be_pcl ) );
202 LDAP_TAILQ_INIT( be->be_pending_csn_list );
204 Debug( LDAP_DEBUG_TRACE,
205 "backend_startup_one: starting \"%s\"\n",
206 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
209 /* set database controls */
210 (void)backend_set_controls( be );
213 if ( !BER_BVISEMPTY( &be->be_rootndn )
214 && select_backend( &be->be_rootndn, 0 ) == be
215 && BER_BVISNULL( &be->be_rootpw ) )
217 /* warning: if rootdn entry is created,
218 * it can take rootdn privileges;
219 * set empty rootpw to prevent */
223 if ( be->bd_info->bi_db_open ) {
224 rc = be->bd_info->bi_db_open( be, cr );
226 (void)backend_set_controls( be );
229 Debug( LDAP_DEBUG_ANY,
230 "backend_startup_one: bi_db_open failed! (%d)\n",
238 int backend_startup(Backend *be)
243 ConfigReply cr={0, ""};
245 if( ! ( nBackendDB > 0 ) ) {
247 Debug( LDAP_DEBUG_ANY,
248 "backend_startup: %d databases to startup.\n",
254 if ( be->bd_info->bi_open ) {
255 rc = be->bd_info->bi_open( be->bd_info );
257 Debug( LDAP_DEBUG_ANY,
258 "backend_startup: bi_open failed!\n",
264 /* append global access controls */
265 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
267 return backend_startup_one( be, &cr );
270 /* open frontend, if required */
271 if ( frontendDB->bd_info->bi_db_open ) {
272 rc = frontendDB->bd_info->bi_db_open( frontendDB, NULL );
274 Debug( LDAP_DEBUG_ANY,
275 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
281 /* open each backend type */
283 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
285 if( bi->bi_nDB == 0) {
286 /* no database of this type, don't open */
291 rc = bi->bi_open( bi );
293 Debug( LDAP_DEBUG_ANY,
294 "backend_startup: bi_open %d (%s) failed!\n",
300 (void)backend_init_controls( bi );
303 /* open each backend database */
305 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
307 if ( be->be_suffix == NULL ) {
308 Debug( LDAP_DEBUG_ANY,
309 "backend_startup: warning, database %d (%s) "
311 i, be->bd_info->bi_type, 0 );
313 /* append global access controls */
314 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
316 rc = backend_startup_one( be, &cr );
324 int backend_num( Backend *be )
329 if( be == NULL ) return -1;
331 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
332 if( be == b2 ) return i;
338 int backend_shutdown( Backend *be )
344 /* shutdown a specific backend database */
346 if ( be->bd_info->bi_nDB == 0 ) {
347 /* no database of this type, we never opened it */
351 if ( be->bd_info->bi_db_close ) {
352 be->bd_info->bi_db_close( be, NULL );
355 if( be->bd_info->bi_close ) {
356 be->bd_info->bi_close( be->bd_info );
362 /* close each backend database */
363 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
364 if ( be->bd_info->bi_db_close ) {
365 be->bd_info->bi_db_close( be, NULL );
369 Debug( LDAP_DEBUG_ANY,
370 "backend_close: bi_db_close %s failed!\n",
375 /* close each backend type */
376 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
377 if( bi->bi_nDB == 0 ) {
378 /* no database of this type */
387 /* close frontend, if required */
388 if ( frontendDB->bd_info->bi_db_close ) {
389 rc = frontendDB->bd_info->bi_db_close ( frontendDB, NULL );
391 Debug( LDAP_DEBUG_ANY,
392 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
401 * This function is supposed to be the exact counterpart
402 * of backend_startup_one(), although this one calls bi_db_destroy()
403 * while backend_startup_one() calls bi_db_open().
405 * Make sure backend_stopdown_one() destroys resources allocated
406 * by backend_startup_one(); only call backend_destroy_one() when
407 * all stuff in a BackendDB needs to be destroyed
410 backend_stopdown_one( BackendDB *bd )
412 if ( bd->be_pending_csn_list ) {
413 struct slap_csn_entry *csne;
414 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
416 struct slap_csn_entry *tmp_csne = csne;
418 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
419 ch_free( csne->ce_csn.bv_val );
420 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
423 ch_free( bd->be_pending_csn_list );
426 if ( bd->bd_info->bi_db_destroy ) {
427 bd->bd_info->bi_db_destroy( bd, NULL );
431 void backend_destroy_one( BackendDB *bd, int dynamic )
434 LDAP_STAILQ_REMOVE(&backendDB, bd, BackendDB, be_next );
437 if ( bd->be_syncinfo ) {
438 syncinfo_free( bd->be_syncinfo, 1 );
441 backend_stopdown_one( bd );
443 ber_bvarray_free( bd->be_suffix );
444 ber_bvarray_free( bd->be_nsuffix );
445 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
446 free( bd->be_rootdn.bv_val );
448 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
449 free( bd->be_rootndn.bv_val );
451 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
452 free( bd->be_rootpw.bv_val );
454 acl_destroy( bd->be_acl, frontendDB->be_acl );
455 limits_destroy( bd->be_limits );
456 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
457 ch_free( bd->be_update_ndn.bv_val );
459 if ( bd->be_update_refs ) {
460 ber_bvarray_free( bd->be_update_refs );
468 int backend_destroy(void)
473 /* destroy each backend database */
474 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
475 backend_destroy_one( bd, 1 );
478 /* destroy each backend type */
479 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
480 if( bi->bi_destroy ) {
481 bi->bi_destroy( bi );
486 LDAP_STAILQ_INIT(&backendInfo);
488 /* destroy frontend database */
491 if ( bd->bd_info->bi_db_destroy ) {
492 bd->bd_info->bi_db_destroy( bd, NULL );
494 ber_bvarray_free( bd->be_suffix );
495 ber_bvarray_free( bd->be_nsuffix );
496 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
497 free( bd->be_rootdn.bv_val );
499 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
500 free( bd->be_rootndn.bv_val );
502 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
503 free( bd->be_rootpw.bv_val );
505 acl_destroy( bd->be_acl, frontendDB->be_acl );
511 BackendInfo* backend_info(const char *type)
515 /* search for the backend type */
516 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
517 if( strcasecmp(bi->bi_type, type) == 0 ) {
531 /* If idx < 0, just add to end of list */
533 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
534 } else if ( idx == 0 ) {
535 LDAP_STAILQ_INSERT_HEAD(&backendDB, be, be_next);
540 b2 = LDAP_STAILQ_FIRST(&backendDB);
542 for (i=0; i<idx; i++) {
543 b2 = LDAP_STAILQ_NEXT(b2, be_next);
545 LDAP_STAILQ_INSERT_AFTER(&backendDB, b2, be, be_next);
555 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
556 backend_db_insert(be, idx);
566 BackendInfo *bi = backend_info(type);
571 fprintf( stderr, "Unrecognized database type (%s)\n", type );
575 /* If be is provided, treat it as private. Otherwise allocate
576 * one and add it to the global list.
579 be = ch_calloc( 1, sizeof(Backend) );
581 if ( idx >= nbackends )
584 backend_db_insert( be, idx );
589 be->be_def_limit = frontendDB->be_def_limit;
590 be->be_dfltaccess = frontendDB->be_dfltaccess;
592 be->be_restrictops = frontendDB->be_restrictops;
593 be->be_requires = frontendDB->be_requires;
594 be->be_ssf_set = frontendDB->be_ssf_set;
596 be->be_pcl_mutexp = &be->be_pcl_mutex;
597 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
599 /* assign a default depth limit for alias deref */
600 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
602 if ( bi->bi_db_init ) {
603 rc = bi->bi_db_init( be, cr );
607 fprintf( stderr, "database init failed (%s)\n", type );
608 /* If we created and linked this be, remove it and free it */
610 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
626 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
627 if ( be->bd_info->bi_db_close ) {
628 be->bd_info->bi_db_close( be, NULL );
632 if ( frontendDB->bd_info->bi_db_close ) {
633 frontendDB->bd_info->bi_db_close( frontendDB, NULL );
644 ber_len_t len, dnlen = dn->bv_len;
647 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
648 if ( be->be_nsuffix == NULL || SLAP_DBHIDDEN( be )) {
652 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
654 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
659 len = be->be_nsuffix[j].bv_len;
662 /* suffix is longer than DN */
667 * input DN is normalized, so the separator check
668 * need not look at escaping
670 if ( len && len < dnlen &&
671 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
676 if ( strcmp( be->be_nsuffix[j].bv_val,
677 &dn->bv_val[dnlen-len] ) == 0 )
690 struct berval *bvsuffix )
694 if ( be->be_nsuffix == NULL ) {
698 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
699 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
710 struct berval *bvsubordinate )
714 if ( be->be_nsuffix == NULL ) {
718 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
719 if ( dnIsSuffix( bvsubordinate, &be->be_nsuffix[i] ) ) {
728 be_isroot_dn( Backend *be, struct berval *ndn )
730 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
734 return dn_match( &be->be_rootndn, ndn );
738 be_slurp_update( Operation *op )
740 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
741 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
745 be_shadow_update( Operation *op )
747 /* This assumes that all internal ops (connid == -1) on a syncrepl
748 * database are syncrepl operations.
750 return (( SLAP_SYNC_SHADOW( op->o_bd ) && op->o_connid == -1 ) ||
751 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
755 be_isupdate_dn( Backend *be, struct berval *ndn )
757 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
761 return dn_match( &be->be_update_ndn, ndn );
765 be_root_dn( Backend *be )
767 return &be->be_rootdn;
771 be_isroot( Operation *op )
773 return be_isroot_dn( op->o_bd, &op->o_ndn );
777 be_isroot_pw( Operation *op )
779 return be_rootdn_bind( op, NULL ) == LDAP_SUCCESS;
783 * checks if binding as rootdn
786 * SLAP_CB_CONTINUE if not the rootdn
787 * LDAP_SUCCESS if rootdn & rootpw
788 * LDAP_INVALID_CREDENTIALS if rootdn & !rootpw
791 * if LDAP_SUCCESS, op->orb_edn is set
792 * if LDAP_INVALID_CREDENTIALS, response is sent to client
795 be_rootdn_bind( Operation *op, SlapReply *rs )
799 assert( op->o_tag == LDAP_REQ_BIND );
800 assert( op->orb_method == LDAP_AUTH_SIMPLE );
802 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
803 return SLAP_CB_CONTINUE;
806 if ( BER_BVISNULL( &op->o_bd->be_rootpw ) ) {
807 /* give the database a chance */
808 return SLAP_CB_CONTINUE;
811 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
812 /* rootdn bind explicitly disallowed */
813 rc = LDAP_INVALID_CREDENTIALS;
822 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
823 op->o_conn->c_sasl_authctx, NULL );
826 rc = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
829 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
832 rc = ( rc == 0 ? LDAP_SUCCESS : LDAP_INVALID_CREDENTIALS );
837 Debug( LDAP_DEBUG_TRACE, "%s: rootdn=\"%s\" bind%s\n",
838 op->o_log_prefix, op->o_bd->be_rootdn.bv_val,
839 rc == LDAP_SUCCESS ? " succeeded" : " failed" );
841 if ( rc == LDAP_SUCCESS ) {
842 /* Set to the pretty rootdn */
843 ber_dupbv( &op->orb_edn, &op->o_bd->be_rootdn );
846 send_ldap_result( op, rs );
859 if ( op->o_bd->be_release ) {
860 /* free and release entry from backend */
861 return op->o_bd->be_release( op, e, rw );
870 backend_unbind( Operation *op, SlapReply *rs )
874 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
875 if ( be->be_unbind ) {
877 be->be_unbind( op, rs );
885 backend_connection_init(
890 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
891 if ( be->be_connection_init ) {
892 be->be_connection_init( be, conn );
900 backend_connection_destroy(
905 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
906 if ( be->be_connection_destroy ) {
907 be->be_connection_destroy( be, conn);
915 backend_check_controls(
919 LDAPControl **ctrls = op->o_ctrls;
920 rs->sr_err = LDAP_SUCCESS;
923 for( ; *ctrls != NULL ; ctrls++ ) {
926 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
927 case LDAP_CONTROL_NOT_FOUND:
928 /* unrecognized control */
929 if ( (*ctrls)->ldctl_iscritical ) {
930 /* should not be reachable */
931 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
932 "unrecognized critical control: %s\n",
933 (*ctrls)->ldctl_oid, 0, 0 );
936 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
937 "unrecognized non-critical control: %s\n",
938 (*ctrls)->ldctl_oid, 0, 0 );
942 case LDAP_COMPARE_FALSE:
943 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
944 /* RFC 4511 allows unavailableCriticalExtension to be
945 * returned when the server is unwilling to perform
946 * an operation extended by a recognized critical
949 rs->sr_text = "critical control unavailable in context";
950 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
955 case LDAP_COMPARE_TRUE:
960 Debug( LDAP_DEBUG_ANY,
961 "backend_check_controls: unable to check control: %s\n",
962 (*ctrls)->ldctl_oid, 0, 0 );
965 rs->sr_text = "unable to check control";
966 rs->sr_err = LDAP_OTHER;
972 #if 0 /* temporarily removed */
973 /* check should be generalized */
974 if( get_relax(op) && !be_isroot(op)) {
975 rs->sr_text = "requires manager authorization";
976 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
985 backend_check_restrictions(
988 struct berval *opdata )
990 slap_mask_t restrictops;
991 slap_mask_t requires;
993 slap_mask_t exopflag = 0;
1000 int rc = SLAP_CB_CONTINUE;
1002 if ( op->o_bd->be_chk_controls ) {
1003 rc = ( *op->o_bd->be_chk_controls )( op, rs );
1006 if ( rc == SLAP_CB_CONTINUE ) {
1007 rc = backend_check_controls( op, rs );
1010 if ( rc != LDAP_SUCCESS ) {
1014 restrictops = op->o_bd->be_restrictops;
1015 requires = op->o_bd->be_requires;
1016 ssf = &op->o_bd->be_ssf_set;
1019 restrictops = frontendDB->be_restrictops;
1020 requires = frontendDB->be_requires;
1021 ssf = &frontendDB->be_ssf_set;
1024 switch( op->o_tag ) {
1026 opflag = SLAP_RESTRICT_OP_ADD;
1030 opflag = SLAP_RESTRICT_OP_BIND;
1033 case LDAP_REQ_COMPARE:
1034 opflag = SLAP_RESTRICT_OP_COMPARE;
1036 case LDAP_REQ_DELETE:
1038 opflag = SLAP_RESTRICT_OP_DELETE;
1040 case LDAP_REQ_EXTENDED:
1041 opflag = SLAP_RESTRICT_OP_EXTENDED;
1044 /* treat unspecified as a modify */
1045 opflag = SLAP_RESTRICT_OP_MODIFY;
1050 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1053 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1057 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1058 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1062 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1063 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1067 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1068 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1073 /* treat everything else as a modify */
1074 opflag = SLAP_RESTRICT_OP_MODIFY;
1078 case LDAP_REQ_MODIFY:
1080 opflag = SLAP_RESTRICT_OP_MODIFY;
1082 case LDAP_REQ_RENAME:
1084 opflag = SLAP_RESTRICT_OP_RENAME;
1086 case LDAP_REQ_SEARCH:
1087 opflag = SLAP_RESTRICT_OP_SEARCH;
1089 case LDAP_REQ_UNBIND:
1094 rs->sr_text = "restrict operations internal error";
1095 rs->sr_err = LDAP_OTHER;
1100 /* these checks don't apply to StartTLS */
1102 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1103 if( op->o_transport_ssf < ssf->sss_transport ) {
1104 rs->sr_text = op->o_transport_ssf
1105 ? "stronger transport confidentiality required"
1106 : "transport confidentiality required";
1110 if( op->o_tls_ssf < ssf->sss_tls ) {
1111 rs->sr_text = op->o_tls_ssf
1112 ? "stronger TLS confidentiality required"
1113 : "TLS confidentiality required";
1118 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1119 /* simple bind specific check */
1120 if( op->o_ssf < ssf->sss_simple_bind ) {
1121 rs->sr_text = op->o_ssf
1122 ? "stronger confidentiality required"
1123 : "confidentiality required";
1128 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1129 /* these checks don't apply to SASL bind */
1131 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1132 rs->sr_text = op->o_sasl_ssf
1133 ? "stronger SASL confidentiality required"
1134 : "SASL confidentiality required";
1138 if( op->o_ssf < ssf->sss_ssf ) {
1139 rs->sr_text = op->o_ssf
1140 ? "stronger confidentiality required"
1141 : "confidentiality required";
1147 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1148 rs->sr_text = op->o_transport_ssf
1149 ? "stronger transport confidentiality required for update"
1150 : "transport confidentiality required for update";
1154 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1155 rs->sr_text = op->o_tls_ssf
1156 ? "stronger TLS confidentiality required for update"
1157 : "TLS confidentiality required for update";
1161 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1162 rs->sr_text = op->o_sasl_ssf
1163 ? "stronger SASL confidentiality required for update"
1164 : "SASL confidentiality required for update";
1168 if( op->o_ssf < ssf->sss_update_ssf ) {
1169 rs->sr_text = op->o_ssf
1170 ? "stronger confidentiality required for update"
1171 : "confidentiality required for update";
1175 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1176 BER_BVISEMPTY( &op->o_ndn ) )
1178 rs->sr_text = "modifications require authentication";
1179 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1183 #ifdef SLAP_X_LISTENER_MOD
1184 if ( op->o_conn->c_listener &&
1185 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1186 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1188 /* no "w" mode means readonly */
1189 rs->sr_text = "modifications not allowed on this listener";
1190 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1193 #endif /* SLAP_X_LISTENER_MOD */
1198 /* these checks don't apply to Bind, StartTLS, or Unbind */
1200 if( requires & SLAP_REQUIRE_STRONG ) {
1201 /* should check mechanism */
1202 if( ( op->o_transport_ssf < ssf->sss_transport
1203 && op->o_authtype == LDAP_AUTH_SIMPLE )
1204 || BER_BVISEMPTY( &op->o_dn ) )
1206 rs->sr_text = "strong(er) authentication required";
1207 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1212 if( requires & SLAP_REQUIRE_SASL ) {
1213 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1214 rs->sr_text = "SASL authentication required";
1215 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1220 if( requires & SLAP_REQUIRE_AUTHC ) {
1221 if( BER_BVISEMPTY( &op->o_dn ) ) {
1222 rs->sr_text = "authentication required";
1223 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1228 if( requires & SLAP_REQUIRE_BIND ) {
1230 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1231 version = op->o_conn->c_protocol;
1232 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1235 /* no bind has occurred */
1236 rs->sr_text = "BIND required";
1237 rs->sr_err = LDAP_OPERATIONS_ERROR;
1242 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1243 if( op->o_protocol < LDAP_VERSION3 ) {
1244 /* no bind has occurred */
1245 rs->sr_text = "operation restricted to LDAPv3 clients";
1246 rs->sr_err = LDAP_OPERATIONS_ERROR;
1251 #ifdef SLAP_X_LISTENER_MOD
1252 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1253 if ( op->o_conn->c_listener &&
1254 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1256 /* no "x" mode means bind required */
1257 rs->sr_text = "bind required on this listener";
1258 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1263 if ( !starttls && !updateop ) {
1264 if ( op->o_conn->c_listener &&
1265 !( op->o_conn->c_listener->sl_perms &
1266 ( !BER_BVISEMPTY( &op->o_dn )
1267 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1269 /* no "r" mode means no read */
1270 rs->sr_text = "read not allowed on this listener";
1271 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1275 #endif /* SLAP_X_LISTENER_MOD */
1279 if( ( restrictops & opflag )
1280 || ( exopflag && ( restrictops & exopflag ) ) ) {
1281 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1282 rs->sr_text = "read operations restricted";
1283 } else if ( restrictops & exopflag ) {
1284 rs->sr_text = "extended operation restricted";
1286 rs->sr_text = "operation restricted";
1288 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1292 rs->sr_err = LDAP_SUCCESS;
1296 int backend_check_referrals( Operation *op, SlapReply *rs )
1298 rs->sr_err = LDAP_SUCCESS;
1300 if( op->o_bd->be_chk_referrals ) {
1301 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1303 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1304 send_ldap_result( op, rs );
1316 AttributeDescription *at,
1322 if ( op->o_bd == NULL ) {
1323 return LDAP_NO_SUCH_OBJECT;
1326 if ( op->o_bd->be_fetch ) {
1327 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1330 return LDAP_UNWILLING_TO_PERFORM;
1337 struct berval *gr_ndn,
1338 struct berval *op_ndn,
1339 ObjectClass *group_oc,
1340 AttributeDescription *group_at )
1343 void *o_priv = op->o_private, *e_priv = NULL;
1347 Backend *be = op->o_bd;
1349 op->o_bd = select_backend( gr_ndn, 0 );
1351 for ( g = op->o_groups; g; g = g->ga_next ) {
1352 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1353 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1357 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1367 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1372 op->o_private = NULL;
1373 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1374 e_priv = op->o_private;
1375 op->o_private = o_priv;
1379 a = attr_find( e->e_attrs, group_at );
1381 /* If the attribute is a subtype of labeledURI,
1382 * treat this as a dynamic group ala groupOfURLs
1384 if ( is_at_subtype( group_at->ad_type,
1385 slap_schema.si_ad_labeledURI->ad_type ) )
1389 struct berval bv, nbase;
1392 void *user_priv = NULL;
1393 Backend *b2 = op->o_bd;
1395 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1399 rc = LDAP_COMPARE_FALSE;
1400 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1401 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1407 BER_BVZERO( &nbase );
1409 /* host, attrs and extensions parts must be empty */
1410 if ( ( ludp->lud_host && *ludp->lud_host )
1417 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1418 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1419 op->o_tmpmemctx ) != LDAP_SUCCESS )
1424 switch ( ludp->lud_scope ) {
1425 case LDAP_SCOPE_BASE:
1426 if ( !dn_match( &nbase, op_ndn ) ) {
1430 case LDAP_SCOPE_ONELEVEL:
1431 dnParent( op_ndn, &bv );
1432 if ( !dn_match( &nbase, &bv ) ) {
1436 case LDAP_SCOPE_SUBTREE:
1437 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1441 case LDAP_SCOPE_SUBORDINATE:
1442 if ( dn_match( &nbase, op_ndn ) ||
1443 !dnIsSuffix( op_ndn, &nbase ) )
1449 /* NOTE: this could be NULL
1450 * if no filter is provided,
1451 * or if filter parsing fails.
1452 * In the latter case,
1453 * we should give up. */
1454 if ( ludp->lud_filter != NULL && ludp->lud_filter != '\0') {
1455 filter = str2filter_x( op, ludp->lud_filter );
1456 if ( filter == NULL ) {
1462 /* only get user if required
1463 * and not available yet */
1464 if ( user == NULL ) {
1467 op->o_bd = select_backend( op_ndn, 0 );
1468 op->o_private = NULL;
1469 rc2 = be_entry_get_rw( op, op_ndn, NULL, NULL, 0, &user );
1470 user_priv = op->o_private;
1471 op->o_private = o_priv;
1479 if ( test_filter( NULL, user, filter ) ==
1484 filter_free_x( op, filter );
1487 ldap_free_urldesc( ludp );
1488 if ( !BER_BVISNULL( &nbase ) ) {
1489 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1491 if ( rc != LDAP_COMPARE_FALSE ) {
1496 if ( user != NULL && user != target ) {
1497 op->o_private = user_priv;
1498 be_entry_release_r( op, user );
1499 op->o_private = o_priv;
1504 rc = value_find_ex( group_at,
1505 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1506 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1507 a->a_nvals, op_ndn, op->o_tmpmemctx );
1508 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
1509 rc = LDAP_COMPARE_FALSE;
1514 rc = LDAP_NO_SUCH_ATTRIBUTE;
1517 if ( e != target ) {
1518 op->o_private = e_priv;
1519 be_entry_release_r( op, e );
1520 op->o_private = o_priv;
1524 rc = LDAP_NO_SUCH_OBJECT;
1527 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1528 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1530 g->ga_be = op->o_bd;
1531 g->ga_oc = group_oc;
1532 g->ga_at = group_at;
1534 g->ga_len = gr_ndn->bv_len;
1535 strcpy( g->ga_ndn, gr_ndn->bv_val );
1536 g->ga_next = op->o_groups;
1549 struct berval *gr_ndn,
1550 struct berval *op_ndn,
1551 ObjectClass *group_oc,
1552 AttributeDescription *group_at )
1557 if ( op->o_abandon ) {
1558 return SLAPD_ABANDON;
1562 op->o_bd = frontendDB;
1563 rc = frontendDB->be_group( op, target, gr_ndn,
1564 op_ndn, group_oc, group_at );
1575 AttributeDescription *entry_at,
1577 slap_access_t access )
1580 void *o_priv = op->o_private, *e_priv = NULL;
1581 Attribute *a = NULL;
1582 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1583 AccessControlState acl_state = ACL_STATE_INIT;
1584 Backend *be = op->o_bd;
1586 op->o_bd = select_backend( edn, 0 );
1588 if ( target && dn_match( &target->e_nname, edn ) ) {
1592 op->o_private = NULL;
1593 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1594 e_priv = op->o_private;
1595 op->o_private = o_priv;
1599 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1600 assert( vals == NULL );
1603 if ( op->o_conn && access > ACL_NONE &&
1604 access_allowed( op, e, entry_at, NULL,
1605 access, &acl_state ) == 0 )
1607 rc = LDAP_INSUFFICIENT_ACCESS;
1612 a = attr_find( e->e_attrs, entry_at );
1614 SlapReply rs = { 0 };
1615 AttributeName anlist[ 2 ];
1617 anlist[ 0 ].an_name = entry_at->ad_cname;
1618 anlist[ 0 ].an_desc = entry_at;
1619 BER_BVZERO( &anlist[ 1 ].an_name );
1620 rs.sr_attrs = anlist;
1622 /* NOTE: backend_operational() is also called
1623 * when returning results, so it's supposed
1624 * to do no harm to entries */
1626 rc = backend_operational( op, &rs );
1629 if ( rc == LDAP_SUCCESS ) {
1630 if ( rs.sr_operational_attrs ) {
1632 a = rs.sr_operational_attrs;
1635 rc = LDAP_NO_SUCH_ATTRIBUTE;
1643 if ( op->o_conn && access > ACL_NONE &&
1644 access_allowed( op, e, entry_at, NULL,
1645 access, &acl_state ) == 0 )
1647 rc = LDAP_INSUFFICIENT_ACCESS;
1651 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1654 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1656 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1658 if ( op->o_conn && access > ACL_NONE &&
1659 access_allowed( op, e, entry_at,
1666 ber_dupbv_x( &v[j], &a->a_nvals[i],
1668 if ( !BER_BVISNULL( &v[j] ) ) {
1673 op->o_tmpfree( v, op->o_tmpmemctx );
1675 rc = LDAP_INSUFFICIENT_ACCESS;
1678 BER_BVZERO( &v[j] );
1683 freeit: if ( e != target ) {
1684 op->o_private = e_priv;
1685 be_entry_release_r( op, e );
1686 op->o_private = o_priv;
1702 AttributeDescription *entry_at,
1704 slap_access_t access )
1710 op->o_bd = frontendDB;
1711 rc = frontendDB->be_attribute( op, target, edn,
1712 entry_at, vals, access );
1723 AttributeDescription *entry_at,
1724 struct berval *nval,
1725 slap_access_t access,
1729 void *o_priv = op->o_private, *e_priv = NULL;
1730 int rc = LDAP_INSUFFICIENT_ACCESS;
1731 Backend *be = op->o_bd;
1734 assert( op != NULL );
1735 assert( op->o_conn != NULL );
1736 assert( edn != NULL );
1737 assert( access > ACL_NONE );
1739 op->o_bd = select_backend( edn, 0 );
1741 if ( target && dn_match( &target->e_nname, edn ) ) {
1745 op->o_private = NULL;
1746 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1747 e_priv = op->o_private;
1748 op->o_private = o_priv;
1752 Attribute *a = NULL;
1755 if ( entry_at == NULL ) {
1756 entry_at = slap_schema.si_ad_entry;
1759 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1761 if ( access_allowed_mask( op, e, entry_at,
1762 NULL, access, NULL, mask ) == 0 )
1764 rc = LDAP_INSUFFICIENT_ACCESS;
1771 a = attr_find( e->e_attrs, entry_at );
1773 SlapReply rs = { 0 };
1774 AttributeName anlist[ 2 ];
1776 anlist[ 0 ].an_name = entry_at->ad_cname;
1777 anlist[ 0 ].an_desc = entry_at;
1778 BER_BVZERO( &anlist[ 1 ].an_name );
1779 rs.sr_attrs = anlist;
1781 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1783 /* NOTE: backend_operational() is also called
1784 * when returning results, so it's supposed
1785 * to do no harm to entries */
1787 rc = backend_operational( op, &rs );
1790 if ( rc == LDAP_SUCCESS ) {
1791 if ( rs.sr_operational_attrs ) {
1793 a = rs.sr_operational_attrs;
1796 rc = LDAP_NO_SUCH_OBJECT;
1802 if ( access_allowed_mask( op, e, entry_at,
1803 nval, access, NULL, mask ) == 0 )
1805 rc = LDAP_INSUFFICIENT_ACCESS;
1811 freeit: if ( e != target ) {
1812 op->o_private = e_priv;
1813 be_entry_release_r( op, e );
1814 op->o_private = o_priv;
1833 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1834 /* just count them */ ;
1837 * If operational attributes (allegedly) are required,
1838 * and the backend supports specific operational attributes,
1839 * add them to the attribute list
1841 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1842 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1843 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1845 *ap = slap_operational_entryDN( rs->sr_entry );
1846 ap = &(*ap)->a_next;
1849 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1850 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1851 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1853 *ap = slap_operational_subschemaSubentry( op->o_bd );
1854 ap = &(*ap)->a_next;
1857 if ( op->o_bd != NULL ) {
1858 BackendDB *be_orig = op->o_bd;
1860 /* Let the overlays have a chance at this */
1861 op->o_bd = select_backend( &op->o_req_ndn, 0 );
1862 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1863 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1864 op->o_bd->be_operational != NULL )
1866 rc = op->o_bd->be_operational( op, rs );
1874 int backend_operational( Operation *op, SlapReply *rs )
1879 /* Moved this into the frontend so global overlays are called */
1882 op->o_bd = frontendDB;
1883 rc = frontendDB->be_operational( op, rs );