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-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/string.h>
33 #include <ac/socket.h>
42 * If a module is configured as dynamic, its header should not
43 * get included into slapd. While this is a general rule and does
44 * not have much of an effect in UNIX, this rule should be adhered
45 * to for Windows, where dynamic object code should not be implicitly
46 * imported into slapd without appropriate __declspec(dllimport) directives.
50 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
53 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
56 backend_init_controls( BackendInfo *bi )
58 if ( bi->bi_controls ) {
61 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
64 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
65 == LDAP_CONTROL_NOT_FOUND )
67 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
74 bi->bi_ctrls[ cid ] = 1;
81 int backend_init(void)
86 if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
87 /* already initialized */
88 Debug( LDAP_DEBUG_ANY,
89 "backend_init: already initialized\n", 0, 0, 0 );
93 for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ ) {
94 assert( bi->bi_init != 0 );
96 rc = bi->bi_init( bi );
99 Debug( LDAP_DEBUG_ANY,
100 "backend_init: initialized for type \"%s\"\n",
102 /* destroy those we've already inited */
107 if ( slap_binfo[nBackendInfo].bi_destroy ) {
108 slap_binfo[nBackendInfo].bi_destroy(
109 &slap_binfo[nBackendInfo] );
115 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
118 if ( nBackendInfo > 0) {
126 Debug( LDAP_DEBUG_ANY,
127 "backend_init: failed\n",
131 #endif /* SLAPD_MODULES */
134 int backend_add(BackendInfo *aBackendInfo)
138 if ( aBackendInfo->bi_init == NULL ) {
139 Debug( LDAP_DEBUG_ANY, "backend_add: "
140 "backend type \"%s\" does not have the (mandatory)init function\n",
141 aBackendInfo->bi_type, 0, 0 );
145 rc = aBackendInfo->bi_init(aBackendInfo);
147 Debug( LDAP_DEBUG_ANY,
148 "backend_add: initialization for type \"%s\" failed\n",
149 aBackendInfo->bi_type, 0, 0 );
153 (void)backend_init_controls( aBackendInfo );
155 /* now add the backend type to the Backend Info List */
156 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
162 backend_set_controls( BackendDB *be )
164 BackendInfo *bi = be->bd_info;
166 /* back-relay takes care of itself; so may do other */
167 if ( overlay_is_over( be ) ) {
168 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
171 if ( bi->bi_controls ) {
172 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
173 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
174 sizeof( be->be_ctrls ) );
175 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
180 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
181 if ( bi->bi_ctrls[ i ] ) {
182 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
192 /* startup a specific backend database */
193 int backend_startup_one(Backend *be, ConfigReply *cr)
197 assert( be != NULL );
199 be->be_pending_csn_list = (struct be_pcl *)
200 ch_calloc( 1, sizeof( struct be_pcl ) );
202 LDAP_TAILQ_INIT( be->be_pending_csn_list );
204 Debug( LDAP_DEBUG_TRACE,
205 "backend_startup_one: starting \"%s\"\n",
206 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
209 /* set database controls */
210 (void)backend_set_controls( be );
213 if ( !BER_BVISEMPTY( &be->be_rootndn )
214 && select_backend( &be->be_rootndn, 0 ) == be
215 && BER_BVISNULL( &be->be_rootpw ) )
217 /* warning: if rootdn entry is created,
218 * it can take rootdn privileges;
219 * set empty rootpw to prevent */
223 if ( be->bd_info->bi_db_open ) {
224 rc = be->bd_info->bi_db_open( be, cr );
226 (void)backend_set_controls( be );
229 char *type = be->bd_info->bi_type;
230 char *suffix = "(null)";
232 if ( overlay_is_over( be ) ) {
233 slap_overinfo *oi = (slap_overinfo *)be->bd_info->bi_private;
234 type = oi->oi_orig->bi_type;
237 if ( be->be_suffix != NULL && !BER_BVISNULL( &be->be_suffix[0] ) ) {
238 suffix = be->be_suffix[0].bv_val;
241 Debug( LDAP_DEBUG_ANY,
242 "backend_startup_one (type=%s, suffix=\"%s\"): "
243 "bi_db_open failed! (%d)\n",
251 int backend_startup(Backend *be)
256 ConfigReply cr={0, ""};
258 if( ! ( nBackendDB > 0 ) ) {
260 Debug( LDAP_DEBUG_ANY,
261 "backend_startup: %d databases to startup.\n",
267 if ( be->bd_info->bi_open ) {
268 rc = be->bd_info->bi_open( be->bd_info );
270 Debug( LDAP_DEBUG_ANY,
271 "backend_startup: bi_open failed!\n",
278 return backend_startup_one( be, &cr );
281 /* open frontend, if required */
282 if ( frontendDB->bd_info->bi_db_open ) {
283 rc = frontendDB->bd_info->bi_db_open( frontendDB, &cr );
285 Debug( LDAP_DEBUG_ANY,
286 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
292 /* open each backend type */
294 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
296 if( bi->bi_nDB == 0) {
297 /* no database of this type, don't open */
302 rc = bi->bi_open( bi );
304 Debug( LDAP_DEBUG_ANY,
305 "backend_startup: bi_open %d (%s) failed!\n",
311 (void)backend_init_controls( bi );
314 /* open each backend database */
316 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
318 if ( be->be_suffix == NULL ) {
319 Debug( LDAP_DEBUG_ANY,
320 "backend_startup: warning, database %d (%s) "
322 i, be->bd_info->bi_type, 0 );
325 rc = backend_startup_one( be, &cr );
333 int backend_num( Backend *be )
338 if( be == NULL ) return -1;
340 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
341 if( be == b2 ) return i;
347 int backend_shutdown( Backend *be )
353 /* shutdown a specific backend database */
355 if ( be->bd_info->bi_nDB == 0 ) {
356 /* no database of this type, we never opened it */
360 if ( be->bd_info->bi_db_close ) {
361 rc = be->bd_info->bi_db_close( be, NULL );
365 if( be->bd_info->bi_close ) {
366 rc = be->bd_info->bi_close( be->bd_info );
373 /* close each backend database */
374 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
375 if ( be->bd_info->bi_db_close ) {
376 be->bd_info->bi_db_close( be, NULL );
380 Debug( LDAP_DEBUG_ANY,
381 "backend_close: bi_db_close %s failed!\n",
386 /* close each backend type */
387 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
388 if( bi->bi_nDB == 0 ) {
389 /* no database of this type */
398 /* close frontend, if required */
399 if ( frontendDB->bd_info->bi_db_close ) {
400 rc = frontendDB->bd_info->bi_db_close ( frontendDB, NULL );
402 Debug( LDAP_DEBUG_ANY,
403 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
412 * This function is supposed to be the exact counterpart
413 * of backend_startup_one(), although this one calls bi_db_destroy()
414 * while backend_startup_one() calls bi_db_open().
416 * Make sure backend_stopdown_one() destroys resources allocated
417 * by backend_startup_one(); only call backend_destroy_one() when
418 * all stuff in a BackendDB needs to be destroyed
421 backend_stopdown_one( BackendDB *bd )
423 if ( bd->be_pending_csn_list ) {
424 struct slap_csn_entry *csne;
425 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
427 struct slap_csn_entry *tmp_csne = csne;
429 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
430 ch_free( csne->ce_csn.bv_val );
431 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
434 ch_free( bd->be_pending_csn_list );
437 if ( bd->bd_info->bi_db_destroy ) {
438 bd->bd_info->bi_db_destroy( bd, NULL );
442 void backend_destroy_one( BackendDB *bd, int dynamic )
445 LDAP_STAILQ_REMOVE(&backendDB, bd, BackendDB, be_next );
448 if ( bd->be_syncinfo ) {
449 syncinfo_free( bd->be_syncinfo, 1 );
452 backend_stopdown_one( bd );
454 ber_bvarray_free( bd->be_suffix );
455 ber_bvarray_free( bd->be_nsuffix );
456 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
457 free( bd->be_rootdn.bv_val );
459 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
460 free( bd->be_rootndn.bv_val );
462 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
463 free( bd->be_rootpw.bv_val );
465 acl_destroy( bd->be_acl );
466 limits_destroy( bd->be_limits );
467 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
468 ch_free( bd->be_update_ndn.bv_val );
470 if ( bd->be_update_refs ) {
471 ber_bvarray_free( bd->be_update_refs );
479 int backend_destroy(void)
484 /* destroy each backend database */
485 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
486 backend_destroy_one( bd, 1 );
489 /* destroy each backend type */
490 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
491 if( bi->bi_destroy ) {
492 bi->bi_destroy( bi );
497 LDAP_STAILQ_INIT(&backendInfo);
499 /* destroy frontend database */
502 if ( bd->bd_info->bi_db_destroy ) {
503 bd->bd_info->bi_db_destroy( bd, NULL );
505 ber_bvarray_free( bd->be_suffix );
506 ber_bvarray_free( bd->be_nsuffix );
507 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
508 free( bd->be_rootdn.bv_val );
510 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
511 free( bd->be_rootndn.bv_val );
513 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
514 free( bd->be_rootpw.bv_val );
516 acl_destroy( bd->be_acl );
523 BackendInfo* backend_info(const char *type)
527 /* search for the backend type */
528 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
529 if( strcasecmp(bi->bi_type, type) == 0 ) {
543 /* If idx < 0, just add to end of list */
545 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
546 } else if ( idx == 0 ) {
547 LDAP_STAILQ_INSERT_HEAD(&backendDB, be, be_next);
552 b2 = LDAP_STAILQ_FIRST(&backendDB);
554 for (i=0; i<idx; i++) {
555 b2 = LDAP_STAILQ_NEXT(b2, be_next);
557 LDAP_STAILQ_INSERT_AFTER(&backendDB, b2, be, be_next);
567 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
568 backend_db_insert(be, idx);
578 BackendInfo *bi = backend_info(type);
583 fprintf( stderr, "Unrecognized database type (%s)\n", type );
587 /* If be is provided, treat it as private. Otherwise allocate
588 * one and add it to the global list.
591 be = ch_calloc( 1, sizeof(Backend) );
593 if ( idx >= nbackends )
596 backend_db_insert( be, idx );
602 be->be_def_limit = frontendDB->be_def_limit;
603 be->be_dfltaccess = frontendDB->be_dfltaccess;
605 be->be_restrictops = frontendDB->be_restrictops;
606 be->be_requires = frontendDB->be_requires;
607 be->be_ssf_set = frontendDB->be_ssf_set;
609 ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
611 /* assign a default depth limit for alias deref */
612 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
614 if ( bi->bi_db_init ) {
615 rc = bi->bi_db_init( be, cr );
619 fprintf( stderr, "database init failed (%s)\n", type );
620 /* If we created and linked this be, remove it and free it */
622 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
629 backend_init_controls( bi );
641 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
642 if ( be->bd_info->bi_db_close ) {
643 be->bd_info->bi_db_close( be, NULL );
647 if ( frontendDB->bd_info->bi_db_close ) {
648 frontendDB->bd_info->bi_db_close( frontendDB, NULL );
659 ber_len_t len, dnlen = dn->bv_len;
662 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
663 if ( be->be_nsuffix == NULL || SLAP_DBHIDDEN( be )) {
667 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
669 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
674 len = be->be_nsuffix[j].bv_len;
677 /* suffix is longer than DN */
682 * input DN is normalized, so the separator check
683 * need not look at escaping
685 if ( len && len < dnlen &&
686 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
691 if ( strcmp( be->be_nsuffix[j].bv_val,
692 &dn->bv_val[dnlen-len] ) == 0 )
705 struct berval *bvsuffix )
709 if ( be->be_nsuffix == NULL ) {
713 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
714 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
725 struct berval *bvsubordinate )
729 if ( be->be_nsuffix == NULL ) {
733 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
734 if ( dnIsSuffix( bvsubordinate, &be->be_nsuffix[i] ) ) {
743 be_isroot_dn( Backend *be, struct berval *ndn )
745 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
749 return dn_match( &be->be_rootndn, ndn );
753 be_slurp_update( Operation *op )
755 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
756 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
760 be_shadow_update( Operation *op )
762 /* This assumes that all internal ops (connid <= -1000) on a syncrepl
763 * database are syncrepl operations.
765 return ( ( SLAP_SYNC_SHADOW( op->o_bd ) && SLAPD_SYNC_IS_SYNCCONN( op->o_connid ) ) ||
766 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
770 be_isupdate_dn( Backend *be, struct berval *ndn )
772 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
776 return dn_match( &be->be_update_ndn, ndn );
780 be_root_dn( Backend *be )
782 return &be->be_rootdn;
786 be_isroot( Operation *op )
788 return be_isroot_dn( op->o_bd, &op->o_ndn );
792 be_isroot_pw( Operation *op )
794 return be_rootdn_bind( op, NULL ) == LDAP_SUCCESS;
798 * checks if binding as rootdn
801 * SLAP_CB_CONTINUE if not the rootdn, or if rootpw is null
802 * LDAP_SUCCESS if rootdn & rootpw
803 * LDAP_INVALID_CREDENTIALS if rootdn & !rootpw
806 * if LDAP_SUCCESS, op->orb_edn is set
807 * if LDAP_INVALID_CREDENTIALS, response is sent to client
810 be_rootdn_bind( Operation *op, SlapReply *rs )
814 void *old_authctx = NULL;
817 assert( op->o_tag == LDAP_REQ_BIND );
818 assert( op->orb_method == LDAP_AUTH_SIMPLE );
820 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
821 return SLAP_CB_CONTINUE;
824 if ( BER_BVISNULL( &op->o_bd->be_rootpw ) ) {
825 /* give the database a chance */
826 return SLAP_CB_CONTINUE;
829 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
830 /* rootdn bind explicitly disallowed */
831 rc = LDAP_INVALID_CREDENTIALS;
840 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
841 op->o_conn->c_sasl_authctx, 0, &old_authctx, NULL );
844 rc = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
847 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
848 old_authctx, 0, NULL, NULL );
851 rc = ( rc == 0 ? LDAP_SUCCESS : LDAP_INVALID_CREDENTIALS );
856 Debug( LDAP_DEBUG_TRACE, "%s: rootdn=\"%s\" bind%s\n",
857 op->o_log_prefix, op->o_bd->be_rootdn.bv_val,
858 rc == LDAP_SUCCESS ? " succeeded" : " failed" );
860 if ( rc == LDAP_SUCCESS ) {
861 /* Set to the pretty rootdn */
862 ber_dupbv( &op->orb_edn, &op->o_bd->be_rootdn );
865 send_ldap_result( op, rs );
878 if ( op->o_bd->be_release ) {
879 /* free and release entry from backend */
880 return op->o_bd->be_release( op, e, rw );
889 backend_unbind( Operation *op, SlapReply *rs )
893 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
894 if ( be->be_unbind ) {
896 be->be_unbind( op, rs );
904 backend_connection_init(
909 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
910 if ( be->be_connection_init ) {
911 be->be_connection_init( be, conn );
919 backend_connection_destroy(
924 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
925 if ( be->be_connection_destroy ) {
926 be->be_connection_destroy( be, conn);
934 backend_check_controls(
938 LDAPControl **ctrls = op->o_ctrls;
939 rs->sr_err = LDAP_SUCCESS;
942 for( ; *ctrls != NULL ; ctrls++ ) {
945 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
946 case LDAP_CONTROL_NOT_FOUND:
947 /* unrecognized control */
948 if ( (*ctrls)->ldctl_iscritical ) {
949 /* should not be reachable */
950 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
951 "unrecognized critical control: %s\n",
952 (*ctrls)->ldctl_oid, 0, 0 );
955 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
956 "unrecognized non-critical control: %s\n",
957 (*ctrls)->ldctl_oid, 0, 0 );
961 case LDAP_COMPARE_FALSE:
962 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
963 #ifdef SLAP_CONTROL_X_WHATFAILED
964 if ( get_whatFailed( op ) ) {
966 oids[ 0 ] = (*ctrls)->ldctl_oid;
968 slap_ctrl_whatFailed_add( op, rs, oids );
971 /* RFC 4511 allows unavailableCriticalExtension to be
972 * returned when the server is unwilling to perform
973 * an operation extended by a recognized critical
976 rs->sr_text = "critical control unavailable in context";
977 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
982 case LDAP_COMPARE_TRUE:
987 Debug( LDAP_DEBUG_ANY,
988 "backend_check_controls: unable to check control: %s\n",
989 (*ctrls)->ldctl_oid, 0, 0 );
992 rs->sr_text = "unable to check control";
993 rs->sr_err = LDAP_OTHER;
999 #if 0 /* temporarily removed */
1000 /* check should be generalized */
1001 if( get_relax(op) && !be_isroot(op)) {
1002 rs->sr_text = "requires manager authorization";
1003 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1012 backend_check_restrictions(
1015 struct berval *opdata )
1017 slap_mask_t restrictops;
1018 slap_mask_t requires;
1020 slap_mask_t exopflag = 0;
1021 slap_ssf_set_t ssfs, *ssf;
1026 restrictops = frontendDB->be_restrictops;
1027 requires = frontendDB->be_requires;
1028 ssfs = frontendDB->be_ssf_set;
1032 slap_ssf_t *fssf, *bssf;
1033 int rc = SLAP_CB_CONTINUE, i;
1035 if ( op->o_bd->be_chk_controls ) {
1036 rc = ( *op->o_bd->be_chk_controls )( op, rs );
1039 if ( rc == SLAP_CB_CONTINUE ) {
1040 rc = backend_check_controls( op, rs );
1043 if ( rc != LDAP_SUCCESS ) {
1047 restrictops |= op->o_bd->be_restrictops;
1048 requires |= op->o_bd->be_requires;
1049 bssf = &op->o_bd->be_ssf_set.sss_ssf;
1050 fssf = &ssfs.sss_ssf;
1051 for ( i=0; i<sizeof(ssfs)/sizeof(slap_ssf_t); i++ ) {
1052 if ( bssf[i] ) fssf[i] = bssf[i];
1056 switch( op->o_tag ) {
1058 opflag = SLAP_RESTRICT_OP_ADD;
1062 opflag = SLAP_RESTRICT_OP_BIND;
1065 case LDAP_REQ_COMPARE:
1066 opflag = SLAP_RESTRICT_OP_COMPARE;
1068 case LDAP_REQ_DELETE:
1070 opflag = SLAP_RESTRICT_OP_DELETE;
1072 case LDAP_REQ_EXTENDED:
1073 opflag = SLAP_RESTRICT_OP_EXTENDED;
1076 /* treat unspecified as a modify */
1077 opflag = SLAP_RESTRICT_OP_MODIFY;
1082 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1085 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1089 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1090 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1094 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1095 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1099 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1100 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1105 /* treat everything else as a modify */
1106 opflag = SLAP_RESTRICT_OP_MODIFY;
1110 case LDAP_REQ_MODIFY:
1112 opflag = SLAP_RESTRICT_OP_MODIFY;
1114 case LDAP_REQ_RENAME:
1116 opflag = SLAP_RESTRICT_OP_RENAME;
1118 case LDAP_REQ_SEARCH:
1119 opflag = SLAP_RESTRICT_OP_SEARCH;
1121 case LDAP_REQ_UNBIND:
1126 rs->sr_text = "restrict operations internal error";
1127 rs->sr_err = LDAP_OTHER;
1132 /* these checks don't apply to StartTLS */
1134 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1135 if( op->o_transport_ssf < ssf->sss_transport ) {
1136 rs->sr_text = op->o_transport_ssf
1137 ? "stronger transport confidentiality required"
1138 : "transport confidentiality required";
1142 if( op->o_tls_ssf < ssf->sss_tls ) {
1143 rs->sr_text = op->o_tls_ssf
1144 ? "stronger TLS confidentiality required"
1145 : "TLS confidentiality required";
1150 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1151 /* simple bind specific check */
1152 if( op->o_ssf < ssf->sss_simple_bind ) {
1153 rs->sr_text = op->o_ssf
1154 ? "stronger confidentiality required"
1155 : "confidentiality required";
1160 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1161 /* these checks don't apply to SASL bind */
1163 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1164 rs->sr_text = op->o_sasl_ssf
1165 ? "stronger SASL confidentiality required"
1166 : "SASL confidentiality required";
1170 if( op->o_ssf < ssf->sss_ssf ) {
1171 rs->sr_text = op->o_ssf
1172 ? "stronger confidentiality required"
1173 : "confidentiality required";
1179 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1180 rs->sr_text = op->o_transport_ssf
1181 ? "stronger transport confidentiality required for update"
1182 : "transport confidentiality required for update";
1186 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1187 rs->sr_text = op->o_tls_ssf
1188 ? "stronger TLS confidentiality required for update"
1189 : "TLS confidentiality required for update";
1193 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1194 rs->sr_text = op->o_sasl_ssf
1195 ? "stronger SASL confidentiality required for update"
1196 : "SASL confidentiality required for update";
1200 if( op->o_ssf < ssf->sss_update_ssf ) {
1201 rs->sr_text = op->o_ssf
1202 ? "stronger confidentiality required for update"
1203 : "confidentiality required for update";
1207 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1208 BER_BVISEMPTY( &op->o_ndn ) )
1210 rs->sr_text = "modifications require authentication";
1211 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1215 #ifdef SLAP_X_LISTENER_MOD
1216 if ( op->o_conn->c_listener &&
1217 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1218 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1220 /* no "w" mode means readonly */
1221 rs->sr_text = "modifications not allowed on this listener";
1222 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1225 #endif /* SLAP_X_LISTENER_MOD */
1230 /* these checks don't apply to Bind, StartTLS, or Unbind */
1232 if( requires & SLAP_REQUIRE_STRONG ) {
1233 /* should check mechanism */
1234 if( ( op->o_transport_ssf < ssf->sss_transport
1235 && op->o_authtype == LDAP_AUTH_SIMPLE )
1236 || BER_BVISEMPTY( &op->o_dn ) )
1238 rs->sr_text = "strong(er) authentication required";
1239 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1244 if( requires & SLAP_REQUIRE_SASL ) {
1245 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1246 rs->sr_text = "SASL authentication required";
1247 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1252 if( requires & SLAP_REQUIRE_AUTHC ) {
1253 if( BER_BVISEMPTY( &op->o_dn ) ) {
1254 rs->sr_text = "authentication required";
1255 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1260 if( requires & SLAP_REQUIRE_BIND ) {
1262 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1263 version = op->o_conn->c_protocol;
1264 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1267 /* no bind has occurred */
1268 rs->sr_text = "BIND required";
1269 rs->sr_err = LDAP_OPERATIONS_ERROR;
1274 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1275 if( op->o_protocol < LDAP_VERSION3 ) {
1276 /* no bind has occurred */
1277 rs->sr_text = "operation restricted to LDAPv3 clients";
1278 rs->sr_err = LDAP_OPERATIONS_ERROR;
1283 #ifdef SLAP_X_LISTENER_MOD
1284 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1285 if ( op->o_conn->c_listener &&
1286 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1288 /* no "x" mode means bind required */
1289 rs->sr_text = "bind required on this listener";
1290 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1295 if ( !starttls && !updateop ) {
1296 if ( op->o_conn->c_listener &&
1297 !( op->o_conn->c_listener->sl_perms &
1298 ( !BER_BVISEMPTY( &op->o_dn )
1299 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1301 /* no "r" mode means no read */
1302 rs->sr_text = "read not allowed on this listener";
1303 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1307 #endif /* SLAP_X_LISTENER_MOD */
1311 if( ( restrictops & opflag )
1312 || ( exopflag && ( restrictops & exopflag ) )
1313 || (( restrictops & SLAP_RESTRICT_READONLY ) && updateop )) {
1314 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1315 rs->sr_text = "read operations restricted";
1316 } else if ( restrictops & exopflag ) {
1317 rs->sr_text = "extended operation restricted";
1319 rs->sr_text = "operation restricted";
1321 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1325 rs->sr_err = LDAP_SUCCESS;
1329 int backend_check_referrals( Operation *op, SlapReply *rs )
1331 rs->sr_err = LDAP_SUCCESS;
1333 if( op->o_bd->be_chk_referrals ) {
1334 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1336 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1337 send_ldap_result( op, rs );
1349 AttributeDescription *at,
1355 if ( op->o_bd == NULL ) {
1356 return LDAP_NO_SUCH_OBJECT;
1359 if ( op->o_bd->be_fetch ) {
1360 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1363 return LDAP_UNWILLING_TO_PERFORM;
1370 struct berval *gr_ndn,
1371 struct berval *op_ndn,
1372 ObjectClass *group_oc,
1373 AttributeDescription *group_at )
1376 void *o_priv = op->o_private, *e_priv = NULL;
1380 Backend *be = op->o_bd;
1383 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1384 if ( oex->oe_key == (void *)backend_group )
1388 if ( oex && ((OpExtraDB *)oex)->oe_db )
1389 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1391 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1392 op->o_bd = select_backend( gr_ndn, 0 );
1394 for ( g = op->o_groups; g; g = g->ga_next ) {
1395 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1396 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1400 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1410 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1415 op->o_private = NULL;
1416 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1417 e_priv = op->o_private;
1418 op->o_private = o_priv;
1422 a = attr_find( e->e_attrs, group_at );
1424 /* If the attribute is a subtype of labeledURI,
1425 * treat this as a dynamic group ala groupOfURLs
1427 if ( is_at_subtype( group_at->ad_type,
1428 slap_schema.si_ad_labeledURI->ad_type ) )
1432 struct berval bv, nbase;
1435 void *user_priv = NULL;
1436 Backend *b2 = op->o_bd;
1438 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1442 rc = LDAP_COMPARE_FALSE;
1443 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1444 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1450 BER_BVZERO( &nbase );
1452 /* host, attrs and extensions parts must be empty */
1453 if ( ( ludp->lud_host && *ludp->lud_host )
1460 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1461 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1462 op->o_tmpmemctx ) != LDAP_SUCCESS )
1467 switch ( ludp->lud_scope ) {
1468 case LDAP_SCOPE_BASE:
1469 if ( !dn_match( &nbase, op_ndn ) ) {
1473 case LDAP_SCOPE_ONELEVEL:
1474 dnParent( op_ndn, &bv );
1475 if ( !dn_match( &nbase, &bv ) ) {
1479 case LDAP_SCOPE_SUBTREE:
1480 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1484 case LDAP_SCOPE_SUBORDINATE:
1485 if ( dn_match( &nbase, op_ndn ) ||
1486 !dnIsSuffix( op_ndn, &nbase ) )
1492 /* NOTE: this could be NULL
1493 * if no filter is provided,
1494 * or if filter parsing fails.
1495 * In the latter case,
1496 * we should give up. */
1497 if ( ludp->lud_filter != NULL && ludp->lud_filter != '\0') {
1498 filter = str2filter_x( op, ludp->lud_filter );
1499 if ( filter == NULL ) {
1505 /* only get user if required
1506 * and not available yet */
1507 if ( user == NULL ) {
1510 op->o_bd = select_backend( op_ndn, 0 );
1511 op->o_private = NULL;
1512 rc2 = be_entry_get_rw( op, op_ndn, NULL, NULL, 0, &user );
1513 user_priv = op->o_private;
1514 op->o_private = o_priv;
1522 if ( test_filter( NULL, user, filter ) ==
1527 filter_free_x( op, filter, 1 );
1530 ldap_free_urldesc( ludp );
1531 if ( !BER_BVISNULL( &nbase ) ) {
1532 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1534 if ( rc != LDAP_COMPARE_FALSE ) {
1539 if ( user != NULL && user != target ) {
1540 op->o_private = user_priv;
1541 be_entry_release_r( op, user );
1542 op->o_private = o_priv;
1547 rc = attr_valfind( a,
1548 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1549 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1550 op_ndn, NULL, op->o_tmpmemctx );
1551 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
1552 rc = LDAP_COMPARE_FALSE;
1557 rc = LDAP_NO_SUCH_ATTRIBUTE;
1560 if ( e != target ) {
1561 op->o_private = e_priv;
1562 be_entry_release_r( op, e );
1563 op->o_private = o_priv;
1567 rc = LDAP_NO_SUCH_OBJECT;
1570 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1571 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1573 g->ga_be = op->o_bd;
1574 g->ga_oc = group_oc;
1575 g->ga_at = group_at;
1577 g->ga_len = gr_ndn->bv_len;
1578 strcpy( g->ga_ndn, gr_ndn->bv_val );
1579 g->ga_next = op->o_groups;
1592 struct berval *gr_ndn,
1593 struct berval *op_ndn,
1594 ObjectClass *group_oc,
1595 AttributeDescription *group_at )
1601 if ( op->o_abandon ) {
1602 return SLAPD_ABANDON;
1605 oex.oe_db = op->o_bd;
1606 oex.oe.oe_key = (void *)backend_group;
1607 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1610 op->o_bd = frontendDB;
1611 rc = frontendDB->be_group( op, target, gr_ndn,
1612 op_ndn, group_oc, group_at );
1614 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
1624 AttributeDescription *entry_at,
1626 slap_access_t access )
1629 void *o_priv = op->o_private, *e_priv = NULL;
1630 Attribute *a = NULL;
1631 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1632 AccessControlState acl_state = ACL_STATE_INIT;
1633 Backend *be = op->o_bd;
1636 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1637 if ( oex->oe_key == (void *)backend_attribute )
1641 if ( oex && ((OpExtraDB *)oex)->oe_db )
1642 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1644 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1645 op->o_bd = select_backend( edn, 0 );
1647 if ( target && dn_match( &target->e_nname, edn ) ) {
1651 op->o_private = NULL;
1652 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1653 e_priv = op->o_private;
1654 op->o_private = o_priv;
1658 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1659 assert( vals == NULL );
1662 if ( op->o_conn && access > ACL_NONE &&
1663 access_allowed( op, e, entry_at, NULL,
1664 access, &acl_state ) == 0 )
1666 rc = LDAP_INSUFFICIENT_ACCESS;
1671 a = attr_find( e->e_attrs, entry_at );
1673 SlapReply rs = { 0 };
1674 AttributeName anlist[ 2 ];
1676 anlist[ 0 ].an_name = entry_at->ad_cname;
1677 anlist[ 0 ].an_desc = entry_at;
1678 BER_BVZERO( &anlist[ 1 ].an_name );
1679 rs.sr_attrs = anlist;
1681 /* NOTE: backend_operational() is also called
1682 * when returning results, so it's supposed
1683 * to do no harm to entries */
1685 rc = backend_operational( op, &rs );
1688 if ( rc == LDAP_SUCCESS ) {
1689 if ( rs.sr_operational_attrs ) {
1691 a = rs.sr_operational_attrs;
1694 rc = LDAP_NO_SUCH_ATTRIBUTE;
1702 if ( op->o_conn && access > ACL_NONE &&
1703 access_allowed( op, e, entry_at, NULL,
1704 access, &acl_state ) == 0 )
1706 rc = LDAP_INSUFFICIENT_ACCESS;
1711 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1713 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1715 if ( op->o_conn && access > ACL_NONE &&
1716 access_allowed( op, e, entry_at,
1723 ber_dupbv_x( &v[j], &a->a_nvals[i],
1725 if ( !BER_BVISNULL( &v[j] ) ) {
1730 op->o_tmpfree( v, op->o_tmpmemctx );
1732 rc = LDAP_INSUFFICIENT_ACCESS;
1735 BER_BVZERO( &v[j] );
1740 freeit: if ( e != target ) {
1741 op->o_private = e_priv;
1742 be_entry_release_r( op, e );
1743 op->o_private = o_priv;
1759 AttributeDescription *entry_at,
1761 slap_access_t access )
1767 oex.oe_db = op->o_bd;
1768 oex.oe.oe_key = (void *)backend_attribute;
1769 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1772 op->o_bd = frontendDB;
1773 rc = frontendDB->be_attribute( op, target, edn,
1774 entry_at, vals, access );
1776 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
1786 AttributeDescription *entry_at,
1787 struct berval *nval,
1788 slap_access_t access,
1792 void *o_priv = op->o_private, *e_priv = NULL;
1793 int rc = LDAP_INSUFFICIENT_ACCESS;
1794 Backend *be = op->o_bd;
1797 assert( op != NULL );
1798 assert( op->o_conn != NULL );
1799 assert( edn != NULL );
1800 assert( access > ACL_NONE );
1803 op->o_bd = select_backend( edn, 0 );
1806 if ( target && dn_match( &target->e_nname, edn ) ) {
1810 op->o_private = NULL;
1811 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1812 e_priv = op->o_private;
1813 op->o_private = o_priv;
1817 Attribute *a = NULL;
1820 if ( entry_at == NULL ) {
1821 entry_at = slap_schema.si_ad_entry;
1824 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1826 if ( access_allowed_mask( op, e, entry_at,
1827 NULL, access, NULL, mask ) == 0 )
1829 rc = LDAP_INSUFFICIENT_ACCESS;
1836 a = attr_find( e->e_attrs, entry_at );
1838 SlapReply rs = { 0 };
1839 AttributeName anlist[ 2 ];
1841 anlist[ 0 ].an_name = entry_at->ad_cname;
1842 anlist[ 0 ].an_desc = entry_at;
1843 BER_BVZERO( &anlist[ 1 ].an_name );
1844 rs.sr_attrs = anlist;
1846 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1848 /* NOTE: backend_operational() is also called
1849 * when returning results, so it's supposed
1850 * to do no harm to entries */
1852 rc = backend_operational( op, &rs );
1855 if ( rc == LDAP_SUCCESS ) {
1856 if ( rs.sr_operational_attrs ) {
1858 a = rs.sr_operational_attrs;
1861 rc = LDAP_NO_SUCH_OBJECT;
1867 if ( access_allowed_mask( op, e, entry_at,
1868 nval, access, NULL, mask ) == 0 )
1870 rc = LDAP_INSUFFICIENT_ACCESS;
1876 freeit: if ( e != target ) {
1877 op->o_private = e_priv;
1878 be_entry_release_r( op, e );
1879 op->o_private = o_priv;
1896 int rc = LDAP_SUCCESS;
1897 BackendDB *be_orig = op->o_bd;
1900 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1901 if ( oex->oe_key == (void *)backend_operational )
1905 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1906 /* just count them */ ;
1909 * If operational attributes (allegedly) are required,
1910 * and the backend supports specific operational attributes,
1911 * add them to the attribute list
1913 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1914 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1915 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1917 *ap = slap_operational_entryDN( rs->sr_entry );
1918 ap = &(*ap)->a_next;
1921 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1922 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1923 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1925 *ap = slap_operational_subschemaSubentry( op->o_bd );
1926 ap = &(*ap)->a_next;
1929 /* Let the overlays have a chance at this */
1930 if ( oex && ((OpExtraDB *)oex)->oe_db )
1931 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1933 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1934 op->o_bd = select_backend( &op->o_req_ndn, 0 );
1936 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1937 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1938 op->o_bd->be_operational != NULL )
1940 rc = op->o_bd->be_operational( op, rs );
1947 int backend_operational( Operation *op, SlapReply *rs )
1953 oex.oe_db = op->o_bd;
1954 oex.oe.oe_key = (void *)backend_operational;
1955 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1957 /* Moved this into the frontend so global overlays are called */
1960 op->o_bd = frontendDB;
1961 rc = frontendDB->be_operational( op, rs );
1963 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);