2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1997,2002-2003 IBM Corporation.
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>.
17 * This work was initially developed by IBM Corporation for use in
18 * IBM products and subsequently ported to OpenLDAP Software by
19 * Steve Omrani. Additional significant contributors include:
25 #include <ac/string.h>
26 #include <ac/stdarg.h>
28 #include <ac/unistd.h>
35 * use a fake listener when faking a connection,
36 * so it can be used in ACLs
38 static struct slap_listener slap_unknown_listener = {
39 BER_BVC("unknown"), /* FIXME: use a URI form? (e.g. slapi://) */
49 /* XXX needs review after internal API change */
50 /* rs->sr_nentries appears to always be 0 */
51 if (op->o_tag == LDAP_REQ_SEARCH)
52 slapi_pblock_set( (Slapi_PBlock *)op->o_pb,
53 SLAPI_NENTRIES, (void *)rs->sr_nentries );
60 internal_search_entry(
64 int nentries = 0, len = 0, i = 0;
65 Slapi_Entry **head = NULL, **tp;
68 entry = slapi_entry_dup( rs->sr_entry );
69 if ( entry == NULL ) {
73 slapi_pblock_get( (Slapi_PBlock *)op->o_pb,
74 SLAPI_NENTRIES, &nentries );
75 slapi_pblock_get( (Slapi_PBlock *)op->o_pb,
76 SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &head );
79 if ( nentries == 0 ) {
80 tp = (Slapi_Entry **)slapi_ch_malloc( 2 * sizeof(Slapi_Entry *) );
82 slapi_entry_free( entry );
88 tp = (Slapi_Entry **)slapi_ch_realloc( (char *)head,
89 sizeof(Slapi_Entry *) * ( i + 1 ) );
91 slapi_entry_free( entry );
98 slapi_pblock_set( (Slapi_PBlock *)op->o_pb,
99 SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, (void *)tp );
100 slapi_pblock_set( (Slapi_PBlock *)op->o_pb,
101 SLAPI_NENTRIES, (void *)i );
115 internal_search_reference(
123 slapi_int_init_connection(
127 Connection *pConn, *c;
128 ber_len_t max = sockbuf_max_incoming;
130 pConn = (Connection *) slapi_ch_calloc(1, sizeof(Connection));
132 return (Connection *)NULL;
135 LDAP_STAILQ_INIT( &pConn->c_pending_ops );
137 pConn->c_pending_ops.stqh_first =
138 (Operation *) slapi_ch_calloc( 1, sizeof(Operation) );
139 if ( pConn->c_pending_ops.stqh_first == NULL ) {
140 slapi_ch_free( (void **)&pConn );
141 return (Connection *)NULL;
144 pConn->c_pending_ops.stqh_first->o_pb =
145 (Slapi_PBlock *) slapi_pblock_new();
146 if ( pConn->c_pending_ops.stqh_first->o_pb == NULL ) {
147 slapi_ch_free( (void **)&pConn->c_pending_ops.stqh_first );
148 slapi_ch_free( (void **)&pConn );
149 return (Connection *)NULL;
154 /* operation object */
155 c->c_pending_ops.stqh_first->o_tag = OpType;
156 c->c_pending_ops.stqh_first->o_protocol = LDAP_VERSION3;
157 c->c_pending_ops.stqh_first->o_authmech.bv_val = NULL;
158 c->c_pending_ops.stqh_first->o_authmech.bv_len = 0;
159 c->c_pending_ops.stqh_first->o_time = slap_get_time();
160 c->c_pending_ops.stqh_first->o_do_not_cache = 1;
161 c->c_pending_ops.stqh_first->o_threadctx = ldap_pvt_thread_pool_context();
162 c->c_pending_ops.stqh_first->o_tmpmemctx = NULL;
163 c->c_pending_ops.stqh_first->o_tmpmfuncs = &ch_mfuncs;
164 c->c_pending_ops.stqh_first->o_conn = c;
166 /* connection object */
167 c->c_authmech.bv_val = NULL;
168 c->c_authmech.bv_len = 0;
169 c->c_dn.bv_val = NULL;
171 c->c_ndn.bv_val = NULL;
174 c->c_listener = &slap_unknown_listener;
175 ber_dupbv( &c->c_peer_domain, (struct berval *)&slap_unknown_bv );
176 ber_dupbv( &c->c_peer_name, (struct berval *)&slap_unknown_bv );
178 LDAP_STAILQ_INIT( &c->c_ops );
180 c->c_sasl_bind_mech.bv_val = NULL;
181 c->c_sasl_bind_mech.bv_len = 0;
182 c->c_sasl_authctx = NULL;
183 c->c_sasl_sockctx = NULL;
184 c->c_sasl_extra = NULL;
186 c->c_sb = ber_sockbuf_alloc( );
188 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
190 c->c_currentber = NULL;
192 /* should check status of thread calls */
193 ldap_pvt_thread_mutex_init( &c->c_mutex );
194 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
195 ldap_pvt_thread_cond_init( &c->c_write_cv );
197 c->c_n_ops_received = 0;
198 c->c_n_ops_executing = 0;
199 c->c_n_ops_pending = 0;
200 c->c_n_ops_completed = 0;
206 c->c_protocol = LDAP_VERSION3;
208 c->c_activitytime = c->c_starttime = slap_get_time();
212 c->c_conn_state = 0x01; /* SLAP_C_ACTIVE */
213 c->c_struct_state = 0x02; /* SLAP_C_USED */
215 c->c_ssf = c->c_transport_ssf = 0;
218 backend_connection_init( c );
220 pConn->c_send_ldap_result = internal_result_v3;
221 pConn->c_send_search_entry = internal_search_entry;
222 pConn->c_send_ldap_extended = internal_result_ext;
223 pConn->c_send_search_reference = internal_search_reference;
228 void slapi_int_connection_destroy( Connection **pConn )
230 Connection *conn = *pConn;
233 if ( pConn == NULL ) {
237 op = (Operation *)conn->c_pending_ops.stqh_first;
239 if ( op->o_req_dn.bv_val != NULL ) {
240 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
242 if ( op->o_req_ndn.bv_val != NULL ) {
243 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
246 if ( conn->c_sb != NULL ) {
247 ber_sockbuf_free( conn->c_sb );
250 slapi_ch_free( (void **)&op );
252 slapi_ch_free( (void **)pConn );
256 * Function : values2obj
257 * Convert an array of strings into a BerVarray.
268 if ( ppValue == NULL ) {
273 for ( i = 0; ppValue[i] != NULL; i++ )
276 tmpberval = (BerVarray)slapi_ch_malloc( (i+1) * (sizeof(struct berval)) );
277 if ( tmpberval == NULL ) {
278 return LDAP_NO_MEMORY;
280 for ( i = 0; ppValue[i] != NULL; i++ ) {
281 size_t len = strlen( ppValue[i] );
283 tmpberval[i].bv_val = slapi_ch_malloc( len + 1 );
284 AC_MEMCPY( tmpberval[i].bv_val, ppValue[i], len + 1 );
285 tmpberval[i].bv_len = len;
287 tmpberval[i].bv_val = NULL;
288 tmpberval[i].bv_len = 0;
297 struct berval **bvptr,
303 if ( bvptr == NULL ) {
308 for ( i = 0; bvptr[i] != NULL; i++ )
311 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1) * sizeof(struct berval));
312 if ( tmpberval == NULL ) {
313 return LDAP_NO_MEMORY;
316 for ( i = 0; bvptr[i] != NULL; i++ ) {
317 tmpberval[i].bv_val = slapi_ch_malloc( bvptr[i]->bv_len );
318 tmpberval[i].bv_len = bvptr[i]->bv_len;
319 AC_MEMCPY( tmpberval[i].bv_val, bvptr[i]->bv_val, bvptr[i]->bv_len );
322 tmpberval[i].bv_val = NULL;
323 tmpberval[i].bv_len = 0;
331 * Function : slapi_int_ldapmod_to_entry
332 * convert a dn plus an array of LDAPMod struct ptrs to an entry structure
333 * with a link list of the correspondent attributes.
334 * Return value : LDAP_SUCCESS
339 slapi_int_ldapmod_to_entry(
343 struct berval dn = BER_BVNULL;
349 Modifications *modlist = NULL;
350 Modifications **modtail = &modlist;
353 int rc = LDAP_SUCCESS;
356 const char *text = NULL;
359 op = (Operation *) slapi_ch_calloc(1, sizeof(Operation));
364 op->o_tag = LDAP_REQ_ADD;
366 pEntry = (Entry *) ch_calloc( 1, sizeof(Entry) );
367 if ( pEntry == NULL) {
372 dn.bv_val = slapi_ch_strdup(ldn);
373 dn.bv_len = strlen(ldn);
375 rc = dnPrettyNormal( NULL, &dn, &pEntry->e_name, &pEntry->e_nname, NULL );
376 if ( rc != LDAP_SUCCESS ) {
380 if ( rc == LDAP_SUCCESS ) {
381 for ( i = 0, pMod = mods[0]; rc == LDAP_SUCCESS && pMod != NULL; pMod = mods[++i]) {
384 if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
386 * Convert an array of pointers to bervals to
387 * an array of bervals. Note that we need to copy the
388 * values too, as the slap_mods_check() will free the
389 * original values after prettying; the modifications
390 * being passed in may not have been allocated on the
393 rc = bvptr2obj_copy( pMod->mod_bvalues, &bv );
394 if ( rc != LDAP_SUCCESS ) goto cleanup;
395 tmp.sml_type.bv_val = pMod->mod_type;
396 tmp.sml_type.bv_len = strlen( pMod->mod_type );
398 tmp.sml_nvalues = NULL;
400 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
402 mod->sml_op = LDAP_MOD_ADD;
403 mod->sml_next = NULL;
404 mod->sml_desc = NULL;
405 mod->sml_type = tmp.sml_type;
406 mod->sml_values = tmp.sml_values;
407 mod->sml_nvalues = tmp.sml_nvalues;
410 modtail = &mod->sml_next;
413 /* attr values are in string format, need to be converted */
414 /* to an array of bervals */
415 if ( pMod->mod_values == NULL ) {
418 rc = values2obj_copy( pMod->mod_values, &bv );
419 if ( rc != LDAP_SUCCESS ) goto cleanup;
420 tmp.sml_type.bv_val = pMod->mod_type;
421 tmp.sml_type.bv_len = strlen( pMod->mod_type );
423 tmp.sml_nvalues = NULL;
425 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
427 mod->sml_op = LDAP_MOD_ADD;
428 mod->sml_next = NULL;
429 mod->sml_desc = NULL;
430 mod->sml_type = tmp.sml_type;
431 mod->sml_values = tmp.sml_values;
432 mod->sml_nvalues = tmp.sml_nvalues;
435 modtail = &mod->sml_next;
438 } /* for each LDAPMod */
441 op->o_bd = select_backend( &pEntry->e_nname, 0, 0 );
442 if ( op->o_bd == NULL ) {
443 rc = LDAP_PARTIAL_RESULTS;
445 int repl_user = be_isupdate_dn( op->o_bd, &op->o_bd->be_rootdn );
446 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
447 int update = op->o_bd->be_update_ndn.bv_len;
448 char textbuf[SLAP_TEXT_BUFLEN];
449 size_t textlen = sizeof textbuf;
451 rc = slap_mods_check( modlist, update, &text,
452 textbuf, textlen, NULL );
453 if ( rc != LDAP_SUCCESS) {
458 rc = slap_mods_opattrs( op,
459 modlist, modtail, &text,
461 if ( rc != LDAP_SUCCESS) {
466 rc = slap_mods2entry( modlist, &pEntry, repl_user,
467 0, &text, textbuf, textlen );
468 if (rc != LDAP_SUCCESS) {
480 slapi_ch_free( (void **)&dn.bv_val );
482 slapi_ch_free( (void **)&op );
483 if ( modlist != NULL )
484 slap_mods_free( modlist );
485 if ( rc != LDAP_SUCCESS ) {
486 if ( pEntry != NULL ) {
487 slapi_entry_free( pEntry );
495 /* Function : slapi_delete_internal
497 * Description : Plugin functions call this routine to delete an entry
498 * in the backend directly
499 * Return values : LDAP_SUCCESS
503 * LDAP_UNWILLING_TO_PERFORM
506 slapi_delete_internal(
508 LDAPControl **controls,
512 Connection *pConn = NULL;
513 Operation *op = NULL;
514 Slapi_PBlock *pPB = NULL;
515 Slapi_PBlock *pSavePB = NULL;
516 SlapReply rs = { REP_RESULT };
517 struct berval dn = BER_BVNULL;
523 rs.sr_err = LDAP_PARAM_ERROR;
527 pConn = slapi_int_init_connection( NULL, LDAP_REQ_DELETE );
529 rs.sr_err = LDAP_NO_MEMORY;
533 op = (Operation *)pConn->c_pending_ops.stqh_first;
534 pPB = (Slapi_PBlock *)op->o_pb;
535 op->o_ctrls = controls;
537 dn.bv_val = slapi_ch_strdup(ldn);
538 dn.bv_len = strlen(ldn);
539 rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
540 if ( rs.sr_err != LDAP_SUCCESS )
543 if ( slapi_control_present( controls,
544 SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical) ) {
548 op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
549 if ( op->o_bd == NULL ) {
550 rs.sr_err = LDAP_PARTIAL_RESULTS;
554 op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
555 op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
557 if ( op->o_bd->be_delete ) {
558 int repl_user = be_isupdate( op );
559 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
560 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
561 if ( log_change ) op->o_callback = &cb;
562 if ( (*op->o_bd->be_delete)( op, &rs ) ) {
563 rs.sr_err = LDAP_OTHER;
566 rs.sr_err = LDAP_REFERRAL;
569 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
574 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
577 slapi_ch_free( (void **)&dn.bv_val );
579 if ( pConn != NULL ) {
583 slapi_int_connection_destroy( &pConn );
588 #endif /* LDAP_SLAPI */
592 static Slapi_PBlock *
593 slapi_int_add_entry_locked(
595 LDAPControl **controls,
598 Connection *pConn = NULL;
599 Operation *op = NULL;
600 Slapi_PBlock *pPB = NULL, *pSavePB = NULL;
604 SlapReply rs = { REP_RESULT };
607 rs.sr_err = LDAP_PARAM_ERROR;
611 pConn = slapi_int_init_connection( NULL, LDAP_REQ_ADD );
612 if ( pConn == NULL ) {
613 rs.sr_err = LDAP_NO_MEMORY;
617 if ( slapi_control_present( controls, LDAP_CONTROL_MANAGEDSAIT,
618 NULL, &isCritical ) ) {
622 op = (Operation *)pConn->c_pending_ops.stqh_first;
623 pPB = (Slapi_PBlock *)op->o_pb;
624 op->o_ctrls = controls;
626 op->o_bd = select_backend( &((*e)->e_nname), manageDsaIt, 0 );
627 if ( op->o_bd == NULL ) {
628 rs.sr_err = LDAP_PARTIAL_RESULTS;
632 op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
633 op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
634 op->oq_add.rs_e = *e;
636 if ( op->o_bd->be_add ) {
637 int repl_user = be_isupdate( op );
638 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
639 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
640 if ( log_changes ) op->o_callback = &cb;
641 if ( (*op->o_bd->be_add)( op, &rs ) == 0 ) {
642 be_entry_release_w( op, *e );
646 rs.sr_err = LDAP_REFERRAL;
649 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
655 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
658 if ( pConn != NULL ) {
662 slapi_int_connection_destroy( &pConn );
666 #endif /* LDAP_SLAPI */
669 slapi_add_entry_internal(
671 LDAPControl **controls,
679 * We make a copy to avoid an entry that may be freed later
680 * by the caller being placed in the cache.
682 entry = slapi_entry_dup( e );
683 pb = slapi_int_add_entry_locked( &entry, controls, log_changes );
684 if ( entry != NULL ) {
685 slapi_entry_free( entry );
697 LDAPControl **controls,
701 LDAPMod *pMod = NULL;
702 Slapi_PBlock *pb = NULL;
703 Entry *pEntry = NULL;
704 int i, rc = LDAP_SUCCESS;
706 if ( mods == NULL || *mods == NULL || dn == NULL || *dn == '\0' ) {
707 rc = LDAP_PARAM_ERROR ;
710 if ( rc == LDAP_SUCCESS ) {
711 for ( i = 0, pMod = mods[0]; pMod != NULL; pMod = mods[++i] ) {
712 if ( (pMod->mod_op & LDAP_MOD_OP ) != LDAP_MOD_ADD ) {
719 if ( rc == LDAP_SUCCESS ) {
720 pEntry = slapi_int_ldapmod_to_entry( dn, mods );
721 if ( pEntry == NULL ) {
726 if ( rc != LDAP_SUCCESS ) {
727 pb = slapi_pblock_new();
728 slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
730 pb = slapi_int_add_entry_locked( &pEntry, controls, log_changes );
733 if ( pEntry != NULL ) {
734 slapi_entry_free(pEntry);
740 #endif /* LDAP_SLAPI */
743 /* Function : slapi_modrdn_internal
745 * Description : Plugin functions call this routine to modify the rdn
746 * of an entry in the backend directly
747 * Return values : LDAP_SUCCESS
751 * LDAP_UNWILLING_TO_PERFORM
753 * NOTE: This function does not support the "newSuperior" option from LDAP V3.
756 slapi_modrdn_internal(
760 LDAPControl **controls,
764 struct berval dn = BER_BVNULL;
765 struct berval newrdn = BER_BVNULL;
766 Connection *pConn = NULL;
767 Operation *op = NULL;
768 Slapi_PBlock *pPB = NULL;
769 Slapi_PBlock *pSavePB = NULL;
772 SlapReply rs = { REP_RESULT };
774 pConn = slapi_int_init_connection( NULL, LDAP_REQ_MODRDN);
775 if ( pConn == NULL) {
776 rs.sr_err = LDAP_NO_MEMORY;
780 op = (Operation *)pConn->c_pending_ops.stqh_first;
781 pPB = (Slapi_PBlock *)op->o_pb;
782 op->o_ctrls = controls;
784 if ( slapi_control_present( controls,
785 SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
789 op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
790 if ( op->o_bd == NULL ) {
791 rs.sr_err = LDAP_PARTIAL_RESULTS;
795 op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
796 op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
798 dn.bv_val = slapi_ch_strdup( olddn );
799 dn.bv_len = strlen( olddn );
801 rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
802 if ( rs.sr_err != LDAP_SUCCESS ) {
806 if ( op->o_req_dn.bv_len == 0 ) {
807 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
811 newrdn.bv_val = slapi_ch_strdup( lnewrdn );
812 newrdn.bv_len = strlen( lnewrdn );
814 rs.sr_err = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn, NULL );
815 if ( rs.sr_err != LDAP_SUCCESS ) {
819 if ( rdn_validate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
823 op->oq_modrdn.rs_newSup = NULL;
824 op->oq_modrdn.rs_nnewSup = NULL;
825 op->oq_modrdn.rs_deleteoldrdn = deloldrdn;
827 if ( op->o_bd->be_modrdn ) {
828 int repl_user = be_isupdate( op );
829 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
830 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
831 if ( log_change ) op->o_callback = &cb;
832 if ( (*op->o_bd->be_modrdn)( op, &rs ) ) {
833 rs.sr_err = LDAP_OTHER;
836 rs.sr_err = LDAP_REFERRAL;
839 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
845 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
849 slapi_ch_free( (void **)&dn.bv_val );
852 slapi_ch_free( (void **)&newrdn.bv_val );
853 if ( op->oq_modrdn.rs_newrdn.bv_val )
854 slapi_ch_free( (void **)&op->oq_modrdn.rs_newrdn.bv_val );
855 if ( op->oq_modrdn.rs_nnewrdn.bv_val )
856 slapi_ch_free( (void **)&op->oq_modrdn.rs_nnewrdn.bv_val );
858 if ( pConn != NULL ) {
862 slapi_int_connection_destroy( &pConn );
867 #endif /* LDAP_SLAPI */
870 /* Function : slapi_modify_internal
872 * Description: Plugin functions call this routine to modify an entry
873 * in the backend directly
874 * Return values : LDAP_SUCCESS
878 * LDAP_UNWILLING_TO_PERFORM
881 slapi_modify_internal(
884 LDAPControl **controls,
889 Connection *pConn = NULL;
890 Operation *op = NULL;
891 Slapi_PBlock *pPB = NULL;
892 Slapi_PBlock *pSavePB = NULL;
894 struct berval dn = BER_BVNULL;
901 Modifications *modlist = NULL;
902 Modifications **modtail = &modlist;
905 SlapReply rs = { REP_RESULT };
907 if ( mods == NULL || *mods == NULL || ldn == NULL ) {
908 rs.sr_err = LDAP_PARAM_ERROR ;
912 pConn = slapi_int_init_connection( NULL, LDAP_REQ_MODIFY );
913 if ( pConn == NULL ) {
914 rs.sr_err = LDAP_NO_MEMORY;
918 op = (Operation *)pConn->c_pending_ops.stqh_first;
919 pPB = (Slapi_PBlock *)op->o_pb;
920 op->o_ctrls = controls;
922 dn.bv_val = slapi_ch_strdup( ldn );
923 dn.bv_len = strlen( ldn );
924 rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
925 if ( rs.sr_err != LDAP_SUCCESS ) {
929 if ( slapi_control_present( controls,
930 SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
934 op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
935 if ( op->o_bd == NULL ) {
936 rs.sr_err = LDAP_PARTIAL_RESULTS;
940 op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
941 op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
943 for ( i = 0, pMod = mods[0];
944 rs.sr_err == LDAP_SUCCESS && pMod != NULL;
949 if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
951 * attr values are in berval format
952 * convert an array of pointers to bervals
953 * to an array of bervals
955 rs.sr_err = bvptr2obj_copy( pMod->mod_bvalues, &bv );
956 if ( rs.sr_err != LDAP_SUCCESS )
958 tmp.sml_type.bv_val = pMod->mod_type;
959 tmp.sml_type.bv_len = strlen( pMod->mod_type );
961 tmp.sml_nvalues = NULL;
963 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
965 mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
966 mod->sml_next = NULL;
967 mod->sml_desc = NULL;
968 mod->sml_type = tmp.sml_type;
969 mod->sml_values = tmp.sml_values;
970 mod->sml_nvalues = tmp.sml_nvalues;
972 rs.sr_err = values2obj_copy( pMod->mod_values, &bv );
973 if ( rs.sr_err != LDAP_SUCCESS )
975 tmp.sml_type.bv_val = pMod->mod_type;
976 tmp.sml_type.bv_len = strlen( pMod->mod_type );
978 tmp.sml_nvalues = NULL;
980 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
982 mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
983 mod->sml_next = NULL;
984 mod->sml_desc = NULL;
985 mod->sml_type = tmp.sml_type;
986 mod->sml_values = tmp.sml_values;
987 mod->sml_nvalues = tmp.sml_nvalues;
990 modtail = &mod->sml_next;
992 switch( pMod->mod_op & LDAP_MOD_OP ) {
994 if ( mod->sml_values == NULL ) {
995 rs.sr_err = LDAP_PROTOCOL_ERROR;
1000 case LDAP_MOD_DELETE:
1001 case LDAP_MOD_REPLACE:
1005 rs.sr_err = LDAP_PROTOCOL_ERROR;
1011 if ( op->o_req_ndn.bv_len == 0 ) {
1012 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1016 op->oq_modify.rs_modlist = modlist;
1018 if ( op->o_bd->be_modify ) {
1019 int repl_user = be_isupdate( op );
1020 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
1021 int update = op->o_bd->be_update_ndn.bv_len;
1022 const char *text = NULL;
1023 char textbuf[SLAP_TEXT_BUFLEN];
1024 size_t textlen = sizeof( textbuf );
1025 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
1027 rs.sr_err = slap_mods_check( modlist, update,
1028 &text, textbuf, textlen, NULL );
1029 if ( rs.sr_err != LDAP_SUCCESS ) {
1034 rs.sr_err = slap_mods_opattrs( op, modlist,
1035 modtail, &text, textbuf,
1037 if ( rs.sr_err != LDAP_SUCCESS ) {
1041 if ( log_change ) op->o_callback = &cb;
1042 if ( (*op->o_bd->be_modify)( op, &rs ) ) {
1043 rs.sr_err = LDAP_OTHER;
1046 rs.sr_err = LDAP_REFERRAL;
1049 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1055 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1058 slapi_ch_free( (void **)&dn.bv_val );
1060 if ( modlist != NULL )
1061 slap_mods_free( modlist );
1063 if ( pConn != NULL ) {
1067 slapi_int_connection_destroy( &pConn );
1072 #endif /* LDAP_SLAPI */
1076 slapi_search_internal(
1080 LDAPControl **controls,
1086 Operation *op = NULL;
1087 Slapi_PBlock *ptr = NULL;
1088 Slapi_PBlock *pSavePB = NULL;
1089 struct berval dn = BER_BVNULL;
1090 Filter *filter=NULL;
1091 struct berval fstr = BER_BVNULL;
1092 AttributeName *an = NULL;
1093 const char *text = NULL;
1095 int manageDsaIt = 0;
1099 SlapReply rs = { REP_RESULT };
1101 c = slapi_int_init_connection( NULL, LDAP_REQ_SEARCH );
1103 rs.sr_err = LDAP_NO_MEMORY;
1107 op = (Operation *)c->c_pending_ops.stqh_first;
1108 ptr = (Slapi_PBlock *)op->o_pb;
1109 op->o_ctrls = controls;
1111 if ( ldn != NULL ) {
1112 dn.bv_val = slapi_ch_strdup(ldn);
1113 dn.bv_len = strlen(ldn);
1116 rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
1117 if ( rs.sr_err != LDAP_SUCCESS ) {
1121 if ( scope != LDAP_SCOPE_BASE &&
1122 scope != LDAP_SCOPE_ONELEVEL &&
1123 scope != LDAP_SCOPE_SUBTREE ) {
1124 rs.sr_err = LDAP_PROTOCOL_ERROR;
1128 filter = slapi_str2filter(filStr);
1129 if ( filter == NULL ) {
1130 rs.sr_err = LDAP_PROTOCOL_ERROR;
1134 filter2bv( filter, &fstr );
1136 for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
1137 ; /* count the number of attributes */
1141 an = (AttributeName *)slapi_ch_calloc( (i + 1), sizeof(AttributeName) );
1142 for (i = 0; attrs[i] != 0; i++) {
1143 an[i].an_desc = NULL;
1145 an[i].an_oc_exclude = 0;
1146 an[i].an_name.bv_val = slapi_ch_strdup(attrs[i]);
1147 an[i].an_name.bv_len = strlen(attrs[i]);
1148 slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1150 an[i].an_name.bv_val = NULL;
1153 memset( &rs, 0, sizeof(rs) );
1154 rs.sr_type = REP_RESULT;
1155 rs.sr_err = LDAP_SUCCESS;
1156 rs.sr_entry = NULL; /* paranoia */
1158 if ( scope == LDAP_SCOPE_BASE ) {
1161 if ( op->o_req_ndn.bv_len == 0 ) {
1162 rs.sr_err = root_dse_info( c, &rs.sr_entry, &rs.sr_text );
1165 if( rs.sr_err != LDAP_SUCCESS ) {
1166 send_ldap_result( op, &rs );
1168 } else if ( rs.sr_entry != NULL ) {
1169 rs.sr_err = test_filter( op, rs.sr_entry, filter );
1171 if ( rs.sr_err == LDAP_COMPARE_TRUE ) {
1172 rs.sr_type = REP_SEARCH;
1173 rs.sr_err = LDAP_SUCCESS;
1175 rs.sr_operational_attrs = NULL;
1176 rs.sr_flags = REP_ENTRY_MODIFIABLE;
1178 send_search_entry( op, &rs );
1181 entry_free( rs.sr_entry );
1183 rs.sr_type = REP_RESULT;
1184 rs.sr_err = LDAP_SUCCESS;
1186 send_ldap_result( op, &rs );
1192 if ( !op->o_req_ndn.bv_len && default_search_nbase.bv_len ) {
1193 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
1194 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
1196 ber_dupbv( &op->o_req_dn, &default_search_base );
1197 ber_dupbv( &op->o_req_ndn, &default_search_nbase );
1200 if ( slapi_control_present( controls,
1201 LDAP_CONTROL_MANAGEDSAIT, NULL, &isCritical ) ) {
1205 op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
1206 if ( op->o_bd == NULL ) {
1207 if ( manageDsaIt == 1 ) {
1208 rs.sr_err = LDAP_NO_SUCH_OBJECT;
1210 rs.sr_err = LDAP_PARTIAL_RESULTS;
1215 op->o_dn = c->c_dn = op->o_bd->be_rootdn;
1216 op->o_ndn = c->c_ndn = op->o_bd->be_rootndn;
1218 op->oq_search.rs_scope = scope;
1219 op->oq_search.rs_deref = 0;
1220 op->oq_search.rs_slimit = SLAP_NO_LIMIT;
1221 op->oq_search.rs_tlimit = SLAP_NO_LIMIT;
1222 op->oq_search.rs_attrsonly = attrsonly;
1223 op->oq_search.rs_attrs = an;
1224 op->oq_search.rs_filter = filter;
1225 op->oq_search.rs_filterstr = fstr;
1227 if ( op->o_bd->be_search ) {
1228 if ( (*op->o_bd->be_search)( op, &rs ) != 0 ) {
1229 rs.sr_err = LDAP_OTHER;
1232 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1238 slapi_pblock_set( ptr, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1241 slapi_ch_free( (void **)&dn.bv_val );
1243 slapi_filter_free( filter, 1 );
1245 slapi_ch_free( (void **)&fstr.bv_val );
1247 slapi_ch_free( (void **)&an );
1253 slapi_int_connection_destroy( &c );
1258 #endif /* LDAP_SLAPI */