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 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",
265 return backend_startup_one( be, &cr );
268 /* open frontend, if required */
269 if ( frontendDB->bd_info->bi_db_open ) {
270 rc = frontendDB->bd_info->bi_db_open( frontendDB, &cr );
272 Debug( LDAP_DEBUG_ANY,
273 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
279 /* open each backend type */
281 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
283 if( bi->bi_nDB == 0) {
284 /* no database of this type, don't open */
289 rc = bi->bi_open( bi );
291 Debug( LDAP_DEBUG_ANY,
292 "backend_startup: bi_open %d (%s) failed!\n",
298 (void)backend_init_controls( bi );
301 /* open each backend database */
303 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
305 if ( be->be_suffix == NULL ) {
306 Debug( LDAP_DEBUG_ANY,
307 "backend_startup: warning, database %d (%s) "
309 i, be->bd_info->bi_type, 0 );
312 rc = backend_startup_one( be, &cr );
320 int backend_num( Backend *be )
325 if( be == NULL ) return -1;
327 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
328 if( be == b2 ) return i;
334 int backend_shutdown( Backend *be )
340 /* shutdown a specific backend database */
342 if ( be->bd_info->bi_nDB == 0 ) {
343 /* no database of this type, we never opened it */
347 if ( be->bd_info->bi_db_close ) {
348 rc = be->bd_info->bi_db_close( be, NULL );
352 if( be->bd_info->bi_close ) {
353 rc = be->bd_info->bi_close( be->bd_info );
360 /* close each backend database */
361 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
362 if ( be->bd_info->bi_db_close ) {
363 be->bd_info->bi_db_close( be, NULL );
367 Debug( LDAP_DEBUG_ANY,
368 "backend_close: bi_db_close %s failed!\n",
373 /* close each backend type */
374 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
375 if( bi->bi_nDB == 0 ) {
376 /* no database of this type */
385 /* close frontend, if required */
386 if ( frontendDB->bd_info->bi_db_close ) {
387 rc = frontendDB->bd_info->bi_db_close ( frontendDB, NULL );
389 Debug( LDAP_DEBUG_ANY,
390 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
399 * This function is supposed to be the exact counterpart
400 * of backend_startup_one(), although this one calls bi_db_destroy()
401 * while backend_startup_one() calls bi_db_open().
403 * Make sure backend_stopdown_one() destroys resources allocated
404 * by backend_startup_one(); only call backend_destroy_one() when
405 * all stuff in a BackendDB needs to be destroyed
408 backend_stopdown_one( BackendDB *bd )
410 if ( bd->be_pending_csn_list ) {
411 struct slap_csn_entry *csne;
412 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
414 struct slap_csn_entry *tmp_csne = csne;
416 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
417 ch_free( csne->ce_csn.bv_val );
418 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
421 ch_free( bd->be_pending_csn_list );
424 if ( bd->bd_info->bi_db_destroy ) {
425 bd->bd_info->bi_db_destroy( bd, NULL );
429 void backend_destroy_one( BackendDB *bd, int dynamic )
432 LDAP_STAILQ_REMOVE(&backendDB, bd, BackendDB, be_next );
435 if ( bd->be_syncinfo ) {
436 syncinfo_free( bd->be_syncinfo, 1 );
439 backend_stopdown_one( bd );
441 ber_bvarray_free( bd->be_suffix );
442 ber_bvarray_free( bd->be_nsuffix );
443 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
444 free( bd->be_rootdn.bv_val );
446 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
447 free( bd->be_rootndn.bv_val );
449 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
450 free( bd->be_rootpw.bv_val );
452 acl_destroy( bd->be_acl );
453 limits_destroy( bd->be_limits );
454 if ( !BER_BVISNULL( &bd->be_update_ndn ) ) {
455 ch_free( bd->be_update_ndn.bv_val );
457 if ( bd->be_update_refs ) {
458 ber_bvarray_free( bd->be_update_refs );
466 int backend_destroy(void)
471 /* destroy each backend database */
472 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
473 backend_destroy_one( bd, 1 );
476 /* destroy each backend type */
477 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
478 if( bi->bi_destroy ) {
479 bi->bi_destroy( bi );
484 LDAP_STAILQ_INIT(&backendInfo);
486 /* destroy frontend database */
489 if ( bd->bd_info->bi_db_destroy ) {
490 bd->bd_info->bi_db_destroy( bd, NULL );
492 ber_bvarray_free( bd->be_suffix );
493 ber_bvarray_free( bd->be_nsuffix );
494 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
495 free( bd->be_rootdn.bv_val );
497 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
498 free( bd->be_rootndn.bv_val );
500 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
501 free( bd->be_rootpw.bv_val );
503 acl_destroy( bd->be_acl );
510 BackendInfo* backend_info(const char *type)
514 /* search for the backend type */
515 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
516 if( strcasecmp(bi->bi_type, type) == 0 ) {
530 /* If idx < 0, just add to end of list */
532 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
533 } else if ( idx == 0 ) {
534 LDAP_STAILQ_INSERT_HEAD(&backendDB, be, be_next);
539 b2 = LDAP_STAILQ_FIRST(&backendDB);
541 for (i=0; i<idx; i++) {
542 b2 = LDAP_STAILQ_NEXT(b2, be_next);
544 LDAP_STAILQ_INSERT_AFTER(&backendDB, b2, be, be_next);
554 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
555 backend_db_insert(be, idx);
565 BackendInfo *bi = backend_info(type);
570 fprintf( stderr, "Unrecognized database type (%s)\n", type );
574 /* If be is provided, treat it as private. Otherwise allocate
575 * one and add it to the global list.
578 be = ch_calloc( 1, sizeof(Backend) );
580 if ( idx >= nbackends )
583 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 ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
598 /* assign a default depth limit for alias deref */
599 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
601 if ( bi->bi_db_init ) {
602 rc = bi->bi_db_init( be, cr );
606 fprintf( stderr, "database init failed (%s)\n", type );
607 /* If we created and linked this be, remove it and free it */
609 LDAP_STAILQ_REMOVE(&backendDB, be, BackendDB, be_next);
616 backend_init_controls( bi );
628 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
629 if ( be->bd_info->bi_db_close ) {
630 be->bd_info->bi_db_close( be, NULL );
634 if ( frontendDB->bd_info->bi_db_close ) {
635 frontendDB->bd_info->bi_db_close( frontendDB, NULL );
646 ber_len_t len, dnlen = dn->bv_len;
649 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
650 if ( be->be_nsuffix == NULL || SLAP_DBHIDDEN( be )) {
654 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
656 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
661 len = be->be_nsuffix[j].bv_len;
664 /* suffix is longer than DN */
669 * input DN is normalized, so the separator check
670 * need not look at escaping
672 if ( len && len < dnlen &&
673 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
678 if ( strcmp( be->be_nsuffix[j].bv_val,
679 &dn->bv_val[dnlen-len] ) == 0 )
692 struct berval *bvsuffix )
696 if ( be->be_nsuffix == NULL ) {
700 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
701 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
712 struct berval *bvsubordinate )
716 if ( be->be_nsuffix == NULL ) {
720 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
721 if ( dnIsSuffix( bvsubordinate, &be->be_nsuffix[i] ) ) {
730 be_isroot_dn( Backend *be, struct berval *ndn )
732 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
736 return dn_match( &be->be_rootndn, ndn );
740 be_slurp_update( Operation *op )
742 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
743 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
747 be_shadow_update( Operation *op )
749 /* This assumes that all internal ops (connid == -1) on a syncrepl
750 * database are syncrepl operations.
752 return (( SLAP_SYNC_SHADOW( op->o_bd ) && op->o_connid == -1 ) ||
753 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
757 be_isupdate_dn( Backend *be, struct berval *ndn )
759 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
763 return dn_match( &be->be_update_ndn, ndn );
767 be_root_dn( Backend *be )
769 return &be->be_rootdn;
773 be_isroot( Operation *op )
775 return be_isroot_dn( op->o_bd, &op->o_ndn );
779 be_isroot_pw( Operation *op )
781 return be_rootdn_bind( op, NULL ) == LDAP_SUCCESS;
785 * checks if binding as rootdn
788 * SLAP_CB_CONTINUE if not the rootdn, or if rootpw is null
789 * LDAP_SUCCESS if rootdn & rootpw
790 * LDAP_INVALID_CREDENTIALS if rootdn & !rootpw
793 * if LDAP_SUCCESS, op->orb_edn is set
794 * if LDAP_INVALID_CREDENTIALS, response is sent to client
797 be_rootdn_bind( Operation *op, SlapReply *rs )
801 void *old_authctx = NULL;
804 assert( op->o_tag == LDAP_REQ_BIND );
805 assert( op->orb_method == LDAP_AUTH_SIMPLE );
807 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
808 return SLAP_CB_CONTINUE;
811 if ( BER_BVISNULL( &op->o_bd->be_rootpw ) ) {
812 /* give the database a chance */
813 return SLAP_CB_CONTINUE;
816 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
817 /* rootdn bind explicitly disallowed */
818 rc = LDAP_INVALID_CREDENTIALS;
827 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
828 op->o_conn->c_sasl_authctx, 0, &old_authctx, NULL );
831 rc = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
834 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
835 old_authctx, 0, NULL, NULL );
838 rc = ( rc == 0 ? LDAP_SUCCESS : LDAP_INVALID_CREDENTIALS );
843 Debug( LDAP_DEBUG_TRACE, "%s: rootdn=\"%s\" bind%s\n",
844 op->o_log_prefix, op->o_bd->be_rootdn.bv_val,
845 rc == LDAP_SUCCESS ? " succeeded" : " failed" );
847 if ( rc == LDAP_SUCCESS ) {
848 /* Set to the pretty rootdn */
849 ber_dupbv( &op->orb_edn, &op->o_bd->be_rootdn );
852 send_ldap_result( op, rs );
865 if ( op->o_bd->be_release ) {
866 /* free and release entry from backend */
867 return op->o_bd->be_release( op, e, rw );
876 backend_unbind( Operation *op, SlapReply *rs )
880 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
881 if ( be->be_unbind ) {
883 be->be_unbind( op, rs );
891 backend_connection_init(
896 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
897 if ( be->be_connection_init ) {
898 be->be_connection_init( be, conn );
906 backend_connection_destroy(
911 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
912 if ( be->be_connection_destroy ) {
913 be->be_connection_destroy( be, conn);
921 backend_check_controls(
925 LDAPControl **ctrls = op->o_ctrls;
926 rs->sr_err = LDAP_SUCCESS;
929 for( ; *ctrls != NULL ; ctrls++ ) {
932 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
933 case LDAP_CONTROL_NOT_FOUND:
934 /* unrecognized control */
935 if ( (*ctrls)->ldctl_iscritical ) {
936 /* should not be reachable */
937 Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
938 "unrecognized critical control: %s\n",
939 (*ctrls)->ldctl_oid, 0, 0 );
942 Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
943 "unrecognized non-critical control: %s\n",
944 (*ctrls)->ldctl_oid, 0, 0 );
948 case LDAP_COMPARE_FALSE:
949 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
950 #ifdef SLAP_CONTROL_X_WHATFAILED
951 if ( get_whatFailed( op ) ) {
953 oids[ 0 ] = (*ctrls)->ldctl_oid;
955 slap_ctrl_whatFailed_add( op, rs, oids );
958 /* RFC 4511 allows unavailableCriticalExtension to be
959 * returned when the server is unwilling to perform
960 * an operation extended by a recognized critical
963 rs->sr_text = "critical control unavailable in context";
964 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
969 case LDAP_COMPARE_TRUE:
974 Debug( LDAP_DEBUG_ANY,
975 "backend_check_controls: unable to check control: %s\n",
976 (*ctrls)->ldctl_oid, 0, 0 );
979 rs->sr_text = "unable to check control";
980 rs->sr_err = LDAP_OTHER;
986 #if 0 /* temporarily removed */
987 /* check should be generalized */
988 if( get_relax(op) && !be_isroot(op)) {
989 rs->sr_text = "requires manager authorization";
990 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
999 backend_check_restrictions(
1002 struct berval *opdata )
1004 slap_mask_t restrictops;
1005 slap_mask_t requires;
1007 slap_mask_t exopflag = 0;
1008 slap_ssf_set_t ssfs, *ssf;
1013 restrictops = frontendDB->be_restrictops;
1014 requires = frontendDB->be_requires;
1015 ssfs = frontendDB->be_ssf_set;
1019 slap_ssf_t *fssf, *bssf;
1020 int rc = SLAP_CB_CONTINUE, i;
1022 if ( op->o_bd->be_chk_controls ) {
1023 rc = ( *op->o_bd->be_chk_controls )( op, rs );
1026 if ( rc == SLAP_CB_CONTINUE ) {
1027 rc = backend_check_controls( op, rs );
1030 if ( rc != LDAP_SUCCESS ) {
1034 restrictops |= op->o_bd->be_restrictops;
1035 requires |= op->o_bd->be_requires;
1036 bssf = &op->o_bd->be_ssf_set.sss_ssf;
1037 fssf = &ssfs.sss_ssf;
1038 for ( i=0; i<sizeof(ssfs)/sizeof(slap_ssf_t); i++ ) {
1039 if ( bssf[i] ) fssf[i] = bssf[i];
1043 switch( op->o_tag ) {
1045 opflag = SLAP_RESTRICT_OP_ADD;
1049 opflag = SLAP_RESTRICT_OP_BIND;
1052 case LDAP_REQ_COMPARE:
1053 opflag = SLAP_RESTRICT_OP_COMPARE;
1055 case LDAP_REQ_DELETE:
1057 opflag = SLAP_RESTRICT_OP_DELETE;
1059 case LDAP_REQ_EXTENDED:
1060 opflag = SLAP_RESTRICT_OP_EXTENDED;
1063 /* treat unspecified as a modify */
1064 opflag = SLAP_RESTRICT_OP_MODIFY;
1069 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1072 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1076 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1077 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1081 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1082 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1086 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1087 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1092 /* treat everything else as a modify */
1093 opflag = SLAP_RESTRICT_OP_MODIFY;
1097 case LDAP_REQ_MODIFY:
1099 opflag = SLAP_RESTRICT_OP_MODIFY;
1101 case LDAP_REQ_RENAME:
1103 opflag = SLAP_RESTRICT_OP_RENAME;
1105 case LDAP_REQ_SEARCH:
1106 opflag = SLAP_RESTRICT_OP_SEARCH;
1108 case LDAP_REQ_UNBIND:
1113 rs->sr_text = "restrict operations internal error";
1114 rs->sr_err = LDAP_OTHER;
1119 /* these checks don't apply to StartTLS */
1121 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1122 if( op->o_transport_ssf < ssf->sss_transport ) {
1123 rs->sr_text = op->o_transport_ssf
1124 ? "stronger transport confidentiality required"
1125 : "transport confidentiality required";
1129 if( op->o_tls_ssf < ssf->sss_tls ) {
1130 rs->sr_text = op->o_tls_ssf
1131 ? "stronger TLS confidentiality required"
1132 : "TLS confidentiality required";
1137 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1138 /* simple bind specific check */
1139 if( op->o_ssf < ssf->sss_simple_bind ) {
1140 rs->sr_text = op->o_ssf
1141 ? "stronger confidentiality required"
1142 : "confidentiality required";
1147 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1148 /* these checks don't apply to SASL bind */
1150 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1151 rs->sr_text = op->o_sasl_ssf
1152 ? "stronger SASL confidentiality required"
1153 : "SASL confidentiality required";
1157 if( op->o_ssf < ssf->sss_ssf ) {
1158 rs->sr_text = op->o_ssf
1159 ? "stronger confidentiality required"
1160 : "confidentiality required";
1166 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1167 rs->sr_text = op->o_transport_ssf
1168 ? "stronger transport confidentiality required for update"
1169 : "transport confidentiality required for update";
1173 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1174 rs->sr_text = op->o_tls_ssf
1175 ? "stronger TLS confidentiality required for update"
1176 : "TLS confidentiality required for update";
1180 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1181 rs->sr_text = op->o_sasl_ssf
1182 ? "stronger SASL confidentiality required for update"
1183 : "SASL confidentiality required for update";
1187 if( op->o_ssf < ssf->sss_update_ssf ) {
1188 rs->sr_text = op->o_ssf
1189 ? "stronger confidentiality required for update"
1190 : "confidentiality required for update";
1194 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1195 BER_BVISEMPTY( &op->o_ndn ) )
1197 rs->sr_text = "modifications require authentication";
1198 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1202 #ifdef SLAP_X_LISTENER_MOD
1203 if ( op->o_conn->c_listener &&
1204 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1205 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1207 /* no "w" mode means readonly */
1208 rs->sr_text = "modifications not allowed on this listener";
1209 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1212 #endif /* SLAP_X_LISTENER_MOD */
1217 /* these checks don't apply to Bind, StartTLS, or Unbind */
1219 if( requires & SLAP_REQUIRE_STRONG ) {
1220 /* should check mechanism */
1221 if( ( op->o_transport_ssf < ssf->sss_transport
1222 && op->o_authtype == LDAP_AUTH_SIMPLE )
1223 || BER_BVISEMPTY( &op->o_dn ) )
1225 rs->sr_text = "strong(er) authentication required";
1226 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1231 if( requires & SLAP_REQUIRE_SASL ) {
1232 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1233 rs->sr_text = "SASL authentication required";
1234 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1239 if( requires & SLAP_REQUIRE_AUTHC ) {
1240 if( BER_BVISEMPTY( &op->o_dn ) ) {
1241 rs->sr_text = "authentication required";
1242 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1247 if( requires & SLAP_REQUIRE_BIND ) {
1249 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1250 version = op->o_conn->c_protocol;
1251 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1254 /* no bind has occurred */
1255 rs->sr_text = "BIND required";
1256 rs->sr_err = LDAP_OPERATIONS_ERROR;
1261 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1262 if( op->o_protocol < LDAP_VERSION3 ) {
1263 /* no bind has occurred */
1264 rs->sr_text = "operation restricted to LDAPv3 clients";
1265 rs->sr_err = LDAP_OPERATIONS_ERROR;
1270 #ifdef SLAP_X_LISTENER_MOD
1271 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1272 if ( op->o_conn->c_listener &&
1273 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1275 /* no "x" mode means bind required */
1276 rs->sr_text = "bind required on this listener";
1277 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1282 if ( !starttls && !updateop ) {
1283 if ( op->o_conn->c_listener &&
1284 !( op->o_conn->c_listener->sl_perms &
1285 ( !BER_BVISEMPTY( &op->o_dn )
1286 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1288 /* no "r" mode means no read */
1289 rs->sr_text = "read not allowed on this listener";
1290 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1294 #endif /* SLAP_X_LISTENER_MOD */
1298 if( ( restrictops & opflag )
1299 || ( exopflag && ( restrictops & exopflag ) ) ) {
1300 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1301 rs->sr_text = "read operations restricted";
1302 } else if ( restrictops & exopflag ) {
1303 rs->sr_text = "extended operation restricted";
1305 rs->sr_text = "operation restricted";
1307 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1311 rs->sr_err = LDAP_SUCCESS;
1315 int backend_check_referrals( Operation *op, SlapReply *rs )
1317 rs->sr_err = LDAP_SUCCESS;
1319 if( op->o_bd->be_chk_referrals ) {
1320 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1322 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1323 send_ldap_result( op, rs );
1335 AttributeDescription *at,
1341 if ( op->o_bd == NULL ) {
1342 return LDAP_NO_SUCH_OBJECT;
1345 if ( op->o_bd->be_fetch ) {
1346 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1349 return LDAP_UNWILLING_TO_PERFORM;
1356 struct berval *gr_ndn,
1357 struct berval *op_ndn,
1358 ObjectClass *group_oc,
1359 AttributeDescription *group_at )
1362 void *o_priv = op->o_private, *e_priv = NULL;
1366 Backend *be = op->o_bd;
1369 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1370 if ( oex->oe_key == (void *)backend_group )
1374 if ( oex && ((OpExtraDB *)oex)->oe_db )
1375 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1377 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1378 op->o_bd = select_backend( gr_ndn, 0 );
1380 for ( g = op->o_groups; g; g = g->ga_next ) {
1381 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1382 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1386 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1396 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1401 op->o_private = NULL;
1402 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1403 e_priv = op->o_private;
1404 op->o_private = o_priv;
1408 a = attr_find( e->e_attrs, group_at );
1410 /* If the attribute is a subtype of labeledURI,
1411 * treat this as a dynamic group ala groupOfURLs
1413 if ( is_at_subtype( group_at->ad_type,
1414 slap_schema.si_ad_labeledURI->ad_type ) )
1418 struct berval bv, nbase;
1421 void *user_priv = NULL;
1422 Backend *b2 = op->o_bd;
1424 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1428 rc = LDAP_COMPARE_FALSE;
1429 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1430 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1436 BER_BVZERO( &nbase );
1438 /* host, attrs and extensions parts must be empty */
1439 if ( ( ludp->lud_host && *ludp->lud_host )
1446 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1447 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1448 op->o_tmpmemctx ) != LDAP_SUCCESS )
1453 switch ( ludp->lud_scope ) {
1454 case LDAP_SCOPE_BASE:
1455 if ( !dn_match( &nbase, op_ndn ) ) {
1459 case LDAP_SCOPE_ONELEVEL:
1460 dnParent( op_ndn, &bv );
1461 if ( !dn_match( &nbase, &bv ) ) {
1465 case LDAP_SCOPE_SUBTREE:
1466 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1470 case LDAP_SCOPE_SUBORDINATE:
1471 if ( dn_match( &nbase, op_ndn ) ||
1472 !dnIsSuffix( op_ndn, &nbase ) )
1478 /* NOTE: this could be NULL
1479 * if no filter is provided,
1480 * or if filter parsing fails.
1481 * In the latter case,
1482 * we should give up. */
1483 if ( ludp->lud_filter != NULL && ludp->lud_filter != '\0') {
1484 filter = str2filter_x( op, ludp->lud_filter );
1485 if ( filter == NULL ) {
1491 /* only get user if required
1492 * and not available yet */
1493 if ( user == NULL ) {
1496 op->o_bd = select_backend( op_ndn, 0 );
1497 op->o_private = NULL;
1498 rc2 = be_entry_get_rw( op, op_ndn, NULL, NULL, 0, &user );
1499 user_priv = op->o_private;
1500 op->o_private = o_priv;
1508 if ( test_filter( NULL, user, filter ) ==
1513 filter_free_x( op, filter, 1 );
1516 ldap_free_urldesc( ludp );
1517 if ( !BER_BVISNULL( &nbase ) ) {
1518 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1520 if ( rc != LDAP_COMPARE_FALSE ) {
1525 if ( user != NULL && user != target ) {
1526 op->o_private = user_priv;
1527 be_entry_release_r( op, user );
1528 op->o_private = o_priv;
1533 rc = attr_valfind( a,
1534 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1535 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1536 op_ndn, NULL, op->o_tmpmemctx );
1537 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
1538 rc = LDAP_COMPARE_FALSE;
1543 rc = LDAP_NO_SUCH_ATTRIBUTE;
1546 if ( e != target ) {
1547 op->o_private = e_priv;
1548 be_entry_release_r( op, e );
1549 op->o_private = o_priv;
1553 rc = LDAP_NO_SUCH_OBJECT;
1556 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1557 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1559 g->ga_be = op->o_bd;
1560 g->ga_oc = group_oc;
1561 g->ga_at = group_at;
1563 g->ga_len = gr_ndn->bv_len;
1564 strcpy( g->ga_ndn, gr_ndn->bv_val );
1565 g->ga_next = op->o_groups;
1578 struct berval *gr_ndn,
1579 struct berval *op_ndn,
1580 ObjectClass *group_oc,
1581 AttributeDescription *group_at )
1587 if ( op->o_abandon ) {
1588 return SLAPD_ABANDON;
1591 oex.oe_db = op->o_bd;
1592 oex.oe.oe_key = (void *)backend_group;
1593 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1596 op->o_bd = frontendDB;
1597 rc = frontendDB->be_group( op, target, gr_ndn,
1598 op_ndn, group_oc, group_at );
1600 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
1610 AttributeDescription *entry_at,
1612 slap_access_t access )
1615 void *o_priv = op->o_private, *e_priv = NULL;
1616 Attribute *a = NULL;
1617 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1618 AccessControlState acl_state = ACL_STATE_INIT;
1619 Backend *be = op->o_bd;
1622 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1623 if ( oex->oe_key == (void *)backend_attribute )
1627 if ( oex && ((OpExtraDB *)oex)->oe_db )
1628 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1630 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1631 op->o_bd = select_backend( edn, 0 );
1633 if ( target && dn_match( &target->e_nname, edn ) ) {
1637 op->o_private = NULL;
1638 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1639 e_priv = op->o_private;
1640 op->o_private = o_priv;
1644 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1645 assert( vals == NULL );
1648 if ( op->o_conn && access > ACL_NONE &&
1649 access_allowed( op, e, entry_at, NULL,
1650 access, &acl_state ) == 0 )
1652 rc = LDAP_INSUFFICIENT_ACCESS;
1657 a = attr_find( e->e_attrs, entry_at );
1659 SlapReply rs = { 0 };
1660 AttributeName anlist[ 2 ];
1662 anlist[ 0 ].an_name = entry_at->ad_cname;
1663 anlist[ 0 ].an_desc = entry_at;
1664 BER_BVZERO( &anlist[ 1 ].an_name );
1665 rs.sr_attrs = anlist;
1667 /* NOTE: backend_operational() is also called
1668 * when returning results, so it's supposed
1669 * to do no harm to entries */
1671 rc = backend_operational( op, &rs );
1674 if ( rc == LDAP_SUCCESS ) {
1675 if ( rs.sr_operational_attrs ) {
1677 a = rs.sr_operational_attrs;
1680 rc = LDAP_NO_SUCH_ATTRIBUTE;
1688 if ( op->o_conn && access > ACL_NONE &&
1689 access_allowed( op, e, entry_at, NULL,
1690 access, &acl_state ) == 0 )
1692 rc = LDAP_INSUFFICIENT_ACCESS;
1697 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1699 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1701 if ( op->o_conn && access > ACL_NONE &&
1702 access_allowed( op, e, entry_at,
1709 ber_dupbv_x( &v[j], &a->a_nvals[i],
1711 if ( !BER_BVISNULL( &v[j] ) ) {
1716 op->o_tmpfree( v, op->o_tmpmemctx );
1718 rc = LDAP_INSUFFICIENT_ACCESS;
1721 BER_BVZERO( &v[j] );
1726 freeit: if ( e != target ) {
1727 op->o_private = e_priv;
1728 be_entry_release_r( op, e );
1729 op->o_private = o_priv;
1745 AttributeDescription *entry_at,
1747 slap_access_t access )
1753 oex.oe_db = op->o_bd;
1754 oex.oe.oe_key = (void *)backend_attribute;
1755 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1758 op->o_bd = frontendDB;
1759 rc = frontendDB->be_attribute( op, target, edn,
1760 entry_at, vals, access );
1762 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
1772 AttributeDescription *entry_at,
1773 struct berval *nval,
1774 slap_access_t access,
1778 void *o_priv = op->o_private, *e_priv = NULL;
1779 int rc = LDAP_INSUFFICIENT_ACCESS;
1780 Backend *be = op->o_bd;
1783 assert( op != NULL );
1784 assert( op->o_conn != NULL );
1785 assert( edn != NULL );
1786 assert( access > ACL_NONE );
1789 op->o_bd = select_backend( edn, 0 );
1792 if ( target && dn_match( &target->e_nname, edn ) ) {
1796 op->o_private = NULL;
1797 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1798 e_priv = op->o_private;
1799 op->o_private = o_priv;
1803 Attribute *a = NULL;
1806 if ( entry_at == NULL ) {
1807 entry_at = slap_schema.si_ad_entry;
1810 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1812 if ( access_allowed_mask( op, e, entry_at,
1813 NULL, access, NULL, mask ) == 0 )
1815 rc = LDAP_INSUFFICIENT_ACCESS;
1822 a = attr_find( e->e_attrs, entry_at );
1824 SlapReply rs = { 0 };
1825 AttributeName anlist[ 2 ];
1827 anlist[ 0 ].an_name = entry_at->ad_cname;
1828 anlist[ 0 ].an_desc = entry_at;
1829 BER_BVZERO( &anlist[ 1 ].an_name );
1830 rs.sr_attrs = anlist;
1832 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1834 /* NOTE: backend_operational() is also called
1835 * when returning results, so it's supposed
1836 * to do no harm to entries */
1838 rc = backend_operational( op, &rs );
1841 if ( rc == LDAP_SUCCESS ) {
1842 if ( rs.sr_operational_attrs ) {
1844 a = rs.sr_operational_attrs;
1847 rc = LDAP_NO_SUCH_OBJECT;
1853 if ( access_allowed_mask( op, e, entry_at,
1854 nval, access, NULL, mask ) == 0 )
1856 rc = LDAP_INSUFFICIENT_ACCESS;
1862 freeit: if ( e != target ) {
1863 op->o_private = e_priv;
1864 be_entry_release_r( op, e );
1865 op->o_private = o_priv;
1882 int rc = LDAP_SUCCESS;
1883 BackendDB *be_orig = op->o_bd;
1886 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
1887 if ( oex->oe_key == (void *)backend_operational )
1891 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1892 /* just count them */ ;
1895 * If operational attributes (allegedly) are required,
1896 * and the backend supports specific operational attributes,
1897 * add them to the attribute list
1899 if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1900 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1901 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1903 *ap = slap_operational_entryDN( rs->sr_entry );
1904 ap = &(*ap)->a_next;
1907 if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1908 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1909 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1911 *ap = slap_operational_subschemaSubentry( op->o_bd );
1912 ap = &(*ap)->a_next;
1915 /* Let the overlays have a chance at this */
1916 if ( oex && ((OpExtraDB *)oex)->oe_db )
1917 op->o_bd = ((OpExtraDB *)oex)->oe_db;
1919 if ( !op->o_bd || !SLAP_DBHIDDEN( op->o_bd ))
1920 op->o_bd = select_backend( &op->o_req_ndn, 0 );
1922 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1923 ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1924 op->o_bd->be_operational != NULL )
1926 rc = op->o_bd->be_operational( op, rs );
1933 int backend_operational( Operation *op, SlapReply *rs )
1939 oex.oe_db = op->o_bd;
1940 oex.oe.oe_key = (void *)backend_operational;
1941 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
1943 /* Moved this into the frontend so global overlays are called */
1946 op->o_bd = frontendDB;
1947 rc = frontendDB->be_operational( op, rs );
1949 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);