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-2008 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, &cr );
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, or if rootpw is null
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 void *old_authctx = NULL;
802 assert( op->o_tag == LDAP_REQ_BIND );
803 assert( op->orb_method == LDAP_AUTH_SIMPLE );
805 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
806 return SLAP_CB_CONTINUE;
809 if ( BER_BVISNULL( &op->o_bd->be_rootpw ) ) {
810 /* give the database a chance */
811 return SLAP_CB_CONTINUE;
814 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
815 /* rootdn bind explicitly disallowed */
816 rc = LDAP_INVALID_CREDENTIALS;
825 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
826 op->o_conn->c_sasl_authctx, 0, &old_authctx, NULL );
829 rc = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
832 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
833 old_authctx, 0, NULL, NULL );
836 rc = ( rc == 0 ? LDAP_SUCCESS : LDAP_INVALID_CREDENTIALS );
841 Debug( LDAP_DEBUG_TRACE, "%s: rootdn=\"%s\" bind%s\n",
842 op->o_log_prefix, op->o_bd->be_rootdn.bv_val,
843 rc == LDAP_SUCCESS ? " succeeded" : " failed" );
845 if ( rc == LDAP_SUCCESS ) {
846 /* Set to the pretty rootdn */
847 ber_dupbv( &op->orb_edn, &op->o_bd->be_rootdn );
850 send_ldap_result( op, rs );
863 if ( op->o_bd->be_release ) {
864 /* free and release entry from backend */
865 return op->o_bd->be_release( op, e, rw );
874 backend_unbind( Operation *op, SlapReply *rs )
878 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
879 if ( be->be_unbind ) {
881 be->be_unbind( op, rs );
889 backend_connection_init(
894 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
895 if ( be->be_connection_init ) {
896 be->be_connection_init( be, conn );
904 backend_connection_destroy(
909 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
910 if ( be->be_connection_destroy ) {
911 be->be_connection_destroy( be, conn);
919 backend_check_controls(
923 LDAPControl **ctrls = op->o_ctrls;
924 rs->sr_err = LDAP_SUCCESS;
927 for( ; *ctrls != NULL ; ctrls++ ) {
930 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
931 case LDAP_CONTROL_NOT_FOUND:
932 /* unrecognized control */
933 if ( (*ctrls)->ldctl_iscritical ) {
934 /* should not be reachable */
935 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
936 "unrecognized critical control: %s\n",
937 (*ctrls)->ldctl_oid, 0, 0 );
940 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
941 "unrecognized non-critical control: %s\n",
942 (*ctrls)->ldctl_oid, 0, 0 );
946 case LDAP_COMPARE_FALSE:
947 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
948 /* RFC 4511 allows unavailableCriticalExtension to be
949 * returned when the server is unwilling to perform
950 * an operation extended by a recognized critical
953 rs->sr_text = "critical control unavailable in context";
954 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
959 case LDAP_COMPARE_TRUE:
964 Debug( LDAP_DEBUG_ANY,
965 "backend_check_controls: unable to check control: %s\n",
966 (*ctrls)->ldctl_oid, 0, 0 );
969 rs->sr_text = "unable to check control";
970 rs->sr_err = LDAP_OTHER;
976 #if 0 /* temporarily removed */
977 /* check should be generalized */
978 if( get_relax(op) && !be_isroot(op)) {
979 rs->sr_text = "requires manager authorization";
980 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
989 backend_check_restrictions(
992 struct berval *opdata )
994 slap_mask_t restrictops;
995 slap_mask_t requires;
997 slap_mask_t exopflag = 0;
1004 int rc = SLAP_CB_CONTINUE;
1006 if ( op->o_bd->be_chk_controls ) {
1007 rc = ( *op->o_bd->be_chk_controls )( op, rs );
1010 if ( rc == SLAP_CB_CONTINUE ) {
1011 rc = backend_check_controls( op, rs );
1014 if ( rc != LDAP_SUCCESS ) {
1018 restrictops = op->o_bd->be_restrictops;
1019 requires = op->o_bd->be_requires;
1020 ssf = &op->o_bd->be_ssf_set;
1023 restrictops = frontendDB->be_restrictops;
1024 requires = frontendDB->be_requires;
1025 ssf = &frontendDB->be_ssf_set;
1028 switch( op->o_tag ) {
1030 opflag = SLAP_RESTRICT_OP_ADD;
1034 opflag = SLAP_RESTRICT_OP_BIND;
1037 case LDAP_REQ_COMPARE:
1038 opflag = SLAP_RESTRICT_OP_COMPARE;
1040 case LDAP_REQ_DELETE:
1042 opflag = SLAP_RESTRICT_OP_DELETE;
1044 case LDAP_REQ_EXTENDED:
1045 opflag = SLAP_RESTRICT_OP_EXTENDED;
1048 /* treat unspecified as a modify */
1049 opflag = SLAP_RESTRICT_OP_MODIFY;
1054 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1057 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1061 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1062 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1066 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1067 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1071 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1072 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1077 /* treat everything else as a modify */
1078 opflag = SLAP_RESTRICT_OP_MODIFY;
1082 case LDAP_REQ_MODIFY:
1084 opflag = SLAP_RESTRICT_OP_MODIFY;
1086 case LDAP_REQ_RENAME:
1088 opflag = SLAP_RESTRICT_OP_RENAME;
1090 case LDAP_REQ_SEARCH:
1091 opflag = SLAP_RESTRICT_OP_SEARCH;
1093 case LDAP_REQ_UNBIND:
1098 rs->sr_text = "restrict operations internal error";
1099 rs->sr_err = LDAP_OTHER;
1104 /* these checks don't apply to StartTLS */
1106 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1107 if( op->o_transport_ssf < ssf->sss_transport ) {
1108 rs->sr_text = op->o_transport_ssf
1109 ? "stronger transport confidentiality required"
1110 : "transport confidentiality required";
1114 if( op->o_tls_ssf < ssf->sss_tls ) {
1115 rs->sr_text = op->o_tls_ssf
1116 ? "stronger TLS confidentiality required"
1117 : "TLS confidentiality required";
1122 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1123 /* simple bind specific check */
1124 if( op->o_ssf < ssf->sss_simple_bind ) {
1125 rs->sr_text = op->o_ssf
1126 ? "stronger confidentiality required"
1127 : "confidentiality required";
1132 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1133 /* these checks don't apply to SASL bind */
1135 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1136 rs->sr_text = op->o_sasl_ssf
1137 ? "stronger SASL confidentiality required"
1138 : "SASL confidentiality required";
1142 if( op->o_ssf < ssf->sss_ssf ) {
1143 rs->sr_text = op->o_ssf
1144 ? "stronger confidentiality required"
1145 : "confidentiality required";
1151 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1152 rs->sr_text = op->o_transport_ssf
1153 ? "stronger transport confidentiality required for update"
1154 : "transport confidentiality required for update";
1158 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1159 rs->sr_text = op->o_tls_ssf
1160 ? "stronger TLS confidentiality required for update"
1161 : "TLS confidentiality required for update";
1165 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1166 rs->sr_text = op->o_sasl_ssf
1167 ? "stronger SASL confidentiality required for update"
1168 : "SASL confidentiality required for update";
1172 if( op->o_ssf < ssf->sss_update_ssf ) {
1173 rs->sr_text = op->o_ssf
1174 ? "stronger confidentiality required for update"
1175 : "confidentiality required for update";
1179 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1180 BER_BVISEMPTY( &op->o_ndn ) )
1182 rs->sr_text = "modifications require authentication";
1183 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1187 #ifdef SLAP_X_LISTENER_MOD
1188 if ( op->o_conn->c_listener &&
1189 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1190 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1192 /* no "w" mode means readonly */
1193 rs->sr_text = "modifications not allowed on this listener";
1194 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1197 #endif /* SLAP_X_LISTENER_MOD */
1202 /* these checks don't apply to Bind, StartTLS, or Unbind */
1204 if( requires & SLAP_REQUIRE_STRONG ) {
1205 /* should check mechanism */
1206 if( ( op->o_transport_ssf < ssf->sss_transport
1207 && op->o_authtype == LDAP_AUTH_SIMPLE )
1208 || BER_BVISEMPTY( &op->o_dn ) )
1210 rs->sr_text = "strong(er) authentication required";
1211 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1216 if( requires & SLAP_REQUIRE_SASL ) {
1217 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1218 rs->sr_text = "SASL authentication required";
1219 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1224 if( requires & SLAP_REQUIRE_AUTHC ) {
1225 if( BER_BVISEMPTY( &op->o_dn ) ) {
1226 rs->sr_text = "authentication required";
1227 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1232 if( requires & SLAP_REQUIRE_BIND ) {
1234 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1235 version = op->o_conn->c_protocol;
1236 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1239 /* no bind has occurred */
1240 rs->sr_text = "BIND required";
1241 rs->sr_err = LDAP_OPERATIONS_ERROR;
1246 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1247 if( op->o_protocol < LDAP_VERSION3 ) {
1248 /* no bind has occurred */
1249 rs->sr_text = "operation restricted to LDAPv3 clients";
1250 rs->sr_err = LDAP_OPERATIONS_ERROR;
1255 #ifdef SLAP_X_LISTENER_MOD
1256 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1257 if ( op->o_conn->c_listener &&
1258 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1260 /* no "x" mode means bind required */
1261 rs->sr_text = "bind required on this listener";
1262 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1267 if ( !starttls && !updateop ) {
1268 if ( op->o_conn->c_listener &&
1269 !( op->o_conn->c_listener->sl_perms &
1270 ( !BER_BVISEMPTY( &op->o_dn )
1271 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1273 /* no "r" mode means no read */
1274 rs->sr_text = "read not allowed on this listener";
1275 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1279 #endif /* SLAP_X_LISTENER_MOD */
1283 if( ( restrictops & opflag )
1284 || ( exopflag && ( restrictops & exopflag ) ) ) {
1285 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1286 rs->sr_text = "read operations restricted";
1287 } else if ( restrictops & exopflag ) {
1288 rs->sr_text = "extended operation restricted";
1290 rs->sr_text = "operation restricted";
1292 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1296 rs->sr_err = LDAP_SUCCESS;
1300 int backend_check_referrals( Operation *op, SlapReply *rs )
1302 rs->sr_err = LDAP_SUCCESS;
1304 if( op->o_bd->be_chk_referrals ) {
1305 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1307 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1308 send_ldap_result( op, rs );
1320 AttributeDescription *at,
1326 if ( op->o_bd == NULL ) {
1327 return LDAP_NO_SUCH_OBJECT;
1330 if ( op->o_bd->be_fetch ) {
1331 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1334 return LDAP_UNWILLING_TO_PERFORM;
1341 struct berval *gr_ndn,
1342 struct berval *op_ndn,
1343 ObjectClass *group_oc,
1344 AttributeDescription *group_at )
1347 void *o_priv = op->o_private, *e_priv = NULL;
1351 Backend *be = op->o_bd;
1354 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1355 if ( oex->oe_key == (void *)backend_group )
1359 if ( oex && ((OpExtraDB *)oex)->oe_db )
1360 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1362 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1363 op->o_bd = select_backend( gr_ndn, 0 );
1365 for ( g = op->o_groups; g; g = g->ga_next ) {
1366 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1367 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1371 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1381 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1386 op->o_private = NULL;
1387 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1388 e_priv = op->o_private;
1389 op->o_private = o_priv;
1393 a = attr_find( e->e_attrs, group_at );
1395 /* If the attribute is a subtype of labeledURI,
1396 * treat this as a dynamic group ala groupOfURLs
1398 if ( is_at_subtype( group_at->ad_type,
1399 slap_schema.si_ad_labeledURI->ad_type ) )
1403 struct berval bv, nbase;
1406 void *user_priv = NULL;
1407 Backend *b2 = op->o_bd;
1409 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1413 rc = LDAP_COMPARE_FALSE;
1414 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1415 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1421 BER_BVZERO( &nbase );
1423 /* host, attrs and extensions parts must be empty */
1424 if ( ( ludp->lud_host && *ludp->lud_host )
1431 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1432 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1433 op->o_tmpmemctx ) != LDAP_SUCCESS )
1438 switch ( ludp->lud_scope ) {
1439 case LDAP_SCOPE_BASE:
1440 if ( !dn_match( &nbase, op_ndn ) ) {
1444 case LDAP_SCOPE_ONELEVEL:
1445 dnParent( op_ndn, &bv );
1446 if ( !dn_match( &nbase, &bv ) ) {
1450 case LDAP_SCOPE_SUBTREE:
1451 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1455 case LDAP_SCOPE_SUBORDINATE:
1456 if ( dn_match( &nbase, op_ndn ) ||
1457 !dnIsSuffix( op_ndn, &nbase ) )
1463 /* NOTE: this could be NULL
1464 * if no filter is provided,
1465 * or if filter parsing fails.
1466 * In the latter case,
1467 * we should give up. */
1468 if ( ludp->lud_filter != NULL && ludp->lud_filter != '\0') {
1469 filter = str2filter_x( op, ludp->lud_filter );
1470 if ( filter == NULL ) {
1476 /* only get user if required
1477 * and not available yet */
1478 if ( user == NULL ) {
1481 op->o_bd = select_backend( op_ndn, 0 );
1482 op->o_private = NULL;
1483 rc2 = be_entry_get_rw( op, op_ndn, NULL, NULL, 0, &user );
1484 user_priv = op->o_private;
1485 op->o_private = o_priv;
1493 if ( test_filter( NULL, user, filter ) ==
1498 filter_free_x( op, filter );
1501 ldap_free_urldesc( ludp );
1502 if ( !BER_BVISNULL( &nbase ) ) {
1503 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1505 if ( rc != LDAP_COMPARE_FALSE ) {
1510 if ( user != NULL && user != target ) {
1511 op->o_private = user_priv;
1512 be_entry_release_r( op, user );
1513 op->o_private = o_priv;
1518 rc = attr_valfind( a,
1519 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1520 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1521 op_ndn, NULL, op->o_tmpmemctx );
1522 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
1523 rc = LDAP_COMPARE_FALSE;
1528 rc = LDAP_NO_SUCH_ATTRIBUTE;
1531 if ( e != target ) {
1532 op->o_private = e_priv;
1533 be_entry_release_r( op, e );
1534 op->o_private = o_priv;
1538 rc = LDAP_NO_SUCH_OBJECT;
1541 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1542 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1544 g->ga_be = op->o_bd;
1545 g->ga_oc = group_oc;
1546 g->ga_at = group_at;
1548 g->ga_len = gr_ndn->bv_len;
1549 strcpy( g->ga_ndn, gr_ndn->bv_val );
1550 g->ga_next = op->o_groups;
1563 struct berval *gr_ndn,
1564 struct berval *op_ndn,
1565 ObjectClass *group_oc,
1566 AttributeDescription *group_at )
1572 if ( op->o_abandon ) {
1573 return SLAPD_ABANDON;
1576 oex.oe_db = op->o_bd;
1577 oex.oe.oe_key = (void *)backend_group;
1578 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1581 op->o_bd = frontendDB;
1582 rc = frontendDB->be_group( op, target, gr_ndn,
1583 op_ndn, group_oc, group_at );
1585 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
1595 AttributeDescription *entry_at,
1597 slap_access_t access )
1600 void *o_priv = op->o_private, *e_priv = NULL;
1601 Attribute *a = NULL;
1602 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1603 AccessControlState acl_state = ACL_STATE_INIT;
1604 Backend *be = op->o_bd;
1607 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1608 if ( oex->oe_key == (void *)backend_attribute )
1612 if ( oex && ((OpExtraDB *)oex)->oe_db )
1613 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1615 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1616 op->o_bd = select_backend( edn, 0 );
1618 if ( target && dn_match( &target->e_nname, edn ) ) {
1622 op->o_private = NULL;
1623 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1624 e_priv = op->o_private;
1625 op->o_private = o_priv;
1629 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1630 assert( vals == NULL );
1633 if ( op->o_conn && access > ACL_NONE &&
1634 access_allowed( op, e, entry_at, NULL,
1635 access, &acl_state ) == 0 )
1637 rc = LDAP_INSUFFICIENT_ACCESS;
1642 a = attr_find( e->e_attrs, entry_at );
1644 SlapReply rs = { 0 };
1645 AttributeName anlist[ 2 ];
1647 anlist[ 0 ].an_name = entry_at->ad_cname;
1648 anlist[ 0 ].an_desc = entry_at;
1649 BER_BVZERO( &anlist[ 1 ].an_name );
1650 rs.sr_attrs = anlist;
1652 /* NOTE: backend_operational() is also called
1653 * when returning results, so it's supposed
1654 * to do no harm to entries */
1656 rc = backend_operational( op, &rs );
1659 if ( rc == LDAP_SUCCESS ) {
1660 if ( rs.sr_operational_attrs ) {
1662 a = rs.sr_operational_attrs;
1665 rc = LDAP_NO_SUCH_ATTRIBUTE;
1673 if ( op->o_conn && access > ACL_NONE &&
1674 access_allowed( op, e, entry_at, NULL,
1675 access, &acl_state ) == 0 )
1677 rc = LDAP_INSUFFICIENT_ACCESS;
1682 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1684 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1686 if ( op->o_conn && access > ACL_NONE &&
1687 access_allowed( op, e, entry_at,
1694 ber_dupbv_x( &v[j], &a->a_nvals[i],
1696 if ( !BER_BVISNULL( &v[j] ) ) {
1701 op->o_tmpfree( v, op->o_tmpmemctx );
1703 rc = LDAP_INSUFFICIENT_ACCESS;
1706 BER_BVZERO( &v[j] );
1711 freeit: if ( e != target ) {
1712 op->o_private = e_priv;
1713 be_entry_release_r( op, e );
1714 op->o_private = o_priv;
1730 AttributeDescription *entry_at,
1732 slap_access_t access )
1738 oex.oe_db = op->o_bd;
1739 oex.oe.oe_key = (void *)backend_attribute;
1740 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1743 op->o_bd = frontendDB;
1744 rc = frontendDB->be_attribute( op, target, edn,
1745 entry_at, vals, access );
1747 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
1757 AttributeDescription *entry_at,
1758 struct berval *nval,
1759 slap_access_t access,
1763 void *o_priv = op->o_private, *e_priv = NULL;
1764 int rc = LDAP_INSUFFICIENT_ACCESS;
1765 Backend *be = op->o_bd;
1768 assert( op != NULL );
1769 assert( op->o_conn != NULL );
1770 assert( edn != NULL );
1771 assert( access > ACL_NONE );
1774 op->o_bd = select_backend( edn, 0 );
1777 if ( target && dn_match( &target->e_nname, edn ) ) {
1781 op->o_private = NULL;
1782 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1783 e_priv = op->o_private;
1784 op->o_private = o_priv;
1788 Attribute *a = NULL;
1791 if ( entry_at == NULL ) {
1792 entry_at = slap_schema.si_ad_entry;
1795 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1797 if ( access_allowed_mask( op, e, entry_at,
1798 NULL, access, NULL, mask ) == 0 )
1800 rc = LDAP_INSUFFICIENT_ACCESS;
1807 a = attr_find( e->e_attrs, entry_at );
1809 SlapReply rs = { 0 };
1810 AttributeName anlist[ 2 ];
1812 anlist[ 0 ].an_name = entry_at->ad_cname;
1813 anlist[ 0 ].an_desc = entry_at;
1814 BER_BVZERO( &anlist[ 1 ].an_name );
1815 rs.sr_attrs = anlist;
1817 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1819 /* NOTE: backend_operational() is also called
1820 * when returning results, so it's supposed
1821 * to do no harm to entries */
1823 rc = backend_operational( op, &rs );
1826 if ( rc == LDAP_SUCCESS ) {
1827 if ( rs.sr_operational_attrs ) {
1829 a = rs.sr_operational_attrs;
1832 rc = LDAP_NO_SUCH_OBJECT;
1838 if ( access_allowed_mask( op, e, entry_at,
1839 nval, access, NULL, mask ) == 0 )
1841 rc = LDAP_INSUFFICIENT_ACCESS;
1847 freeit: if ( e != target ) {
1848 op->o_private = e_priv;
1849 be_entry_release_r( op, e );
1850 op->o_private = o_priv;
1867 int rc = LDAP_SUCCESS;
1868 BackendDB *be_orig = op->o_bd;
1871 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1872 if ( oex->oe_key == (void *)backend_operational )
1876 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1877 /* just count them */ ;
1880 * If operational attributes (allegedly) are required,
1881 * and the backend supports specific operational attributes,
1882 * add them to the attribute list
1884 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1885 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1886 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1888 *ap = slap_operational_entryDN( rs->sr_entry );
1889 ap = &(*ap)->a_next;
1892 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1893 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1894 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1896 *ap = slap_operational_subschemaSubentry( op->o_bd );
1897 ap = &(*ap)->a_next;
1900 /* Let the overlays have a chance at this */
1901 if ( oex && ((OpExtraDB *)oex)->oe_db )
1902 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1904 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1905 op->o_bd = select_backend( &op->o_req_ndn, 0 );
1907 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1908 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1909 op->o_bd->be_operational != NULL )
1911 rc = op->o_bd->be_operational( op, rs );
1918 int backend_operational( Operation *op, SlapReply *rs )
1924 oex.oe_db = op->o_bd;
1925 oex.oe.oe_key = (void *)backend_operational;
1926 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1928 /* Moved this into the frontend so global overlays are called */
1931 op->o_bd = frontendDB;
1932 rc = frontendDB->be_operational( op, rs );
1934 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);