2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2005 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(
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;
152 /* connection object */
153 pConn->c_authmech.bv_val = NULL;
154 pConn->c_authmech.bv_len = 0;
155 pConn->c_dn.bv_val = NULL;
156 pConn->c_dn.bv_len = 0;
157 pConn->c_ndn.bv_val = NULL;
158 pConn->c_ndn.bv_len = 0;
160 pConn->c_listener = &slap_unknown_listener;
161 ber_dupbv( &pConn->c_peer_domain, (struct berval *)&slap_unknown_bv );
162 ber_dupbv( &pConn->c_peer_name, (struct berval *)&slap_unknown_bv );
164 LDAP_STAILQ_INIT( &pConn->c_ops );
166 pConn->c_sasl_bind_mech.bv_val = NULL;
167 pConn->c_sasl_bind_mech.bv_len = 0;
168 pConn->c_sasl_authctx = NULL;
169 pConn->c_sasl_sockctx = NULL;
170 pConn->c_sasl_extra = NULL;
172 pConn->c_sb = ber_sockbuf_alloc( );
174 ber_sockbuf_ctrl( pConn->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
176 pConn->c_currentber = NULL;
178 /* should check status of thread calls */
179 ldap_pvt_thread_mutex_init( &pConn->c_mutex );
180 ldap_pvt_thread_mutex_init( &pConn->c_write_mutex );
181 ldap_pvt_thread_cond_init( &pConn->c_write_cv );
183 ldap_pvt_thread_mutex_lock( &pConn->c_mutex );
185 pConn->c_n_ops_received = 0;
186 pConn->c_n_ops_executing = 0;
187 pConn->c_n_ops_pending = 0;
188 pConn->c_n_ops_completed = 0;
192 pConn->c_n_write = 0;
194 pConn->c_protocol = LDAP_VERSION3;
196 pConn->c_activitytime = pConn->c_starttime = slap_get_time();
199 * A real connection ID is required, because syncrepl associates
200 * pending CSNs with unique ( connection, operation ) tuples.
201 * Setting a fake connection ID will cause slap_get_commit_csn()
202 * to return a stale value.
204 connection_assign_nextid( pConn );
206 pConn->c_conn_state = 0x01; /* SLAP_C_ACTIVE */
207 pConn->c_struct_state = 0x02; /* SLAP_C_USED */
209 pConn->c_ssf = pConn->c_transport_ssf = 0;
210 pConn->c_tls_ssf = 0;
212 backend_connection_init( pConn );
214 pConn->c_send_ldap_result = internal_result_v3;
215 pConn->c_send_search_entry = internal_search_entry;
216 pConn->c_send_ldap_extended = internal_result_ext;
217 pConn->c_send_search_reference = internal_search_reference;
219 /* operation object */
220 pConn->c_pending_ops.stqh_first->o_tag = OpType;
221 pConn->c_pending_ops.stqh_first->o_protocol = LDAP_VERSION3;
222 pConn->c_pending_ops.stqh_first->o_authmech.bv_val = NULL;
223 pConn->c_pending_ops.stqh_first->o_authmech.bv_len = 0;
224 pConn->c_pending_ops.stqh_first->o_time = slap_get_time();
225 pConn->c_pending_ops.stqh_first->o_do_not_cache = 1;
226 pConn->c_pending_ops.stqh_first->o_threadctx = ldap_pvt_thread_pool_context();
227 pConn->c_pending_ops.stqh_first->o_tmpmemctx = NULL;
228 pConn->c_pending_ops.stqh_first->o_tmpmfuncs = &ch_mfuncs;
229 pConn->c_pending_ops.stqh_first->o_conn = pConn;
230 pConn->c_pending_ops.stqh_first->o_connid = pConn->c_connid;
232 ldap_pvt_thread_mutex_unlock( &pConn->c_mutex );
237 void slapi_int_connection_destroy( Connection **pConn )
239 Connection *conn = *pConn;
242 if ( pConn == NULL ) {
246 op = (Operation *)conn->c_pending_ops.stqh_first;
248 slap_graduate_commit_csn( op );
250 if ( op->o_req_dn.bv_val != NULL ) {
251 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
253 if ( op->o_req_ndn.bv_val != NULL ) {
254 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
257 if ( conn->c_sb != NULL ) {
258 ber_sockbuf_free( conn->c_sb );
261 slapi_ch_free( (void **)&op );
263 slapi_ch_free( (void **)pConn );
267 * Function : values2obj
268 * Convert an array of strings into a BerVarray.
279 if ( ppValue == NULL ) {
284 for ( i = 0; ppValue[i] != NULL; i++ )
287 tmpberval = (BerVarray)slapi_ch_malloc( (i+1) * (sizeof(struct berval)) );
288 if ( tmpberval == NULL ) {
289 return LDAP_NO_MEMORY;
291 for ( i = 0; ppValue[i] != NULL; i++ ) {
292 size_t len = strlen( ppValue[i] );
294 tmpberval[i].bv_val = slapi_ch_malloc( len + 1 );
295 AC_MEMCPY( tmpberval[i].bv_val, ppValue[i], len + 1 );
296 tmpberval[i].bv_len = len;
298 tmpberval[i].bv_val = NULL;
299 tmpberval[i].bv_len = 0;
308 struct berval **bvptr,
314 if ( bvptr == NULL ) {
319 for ( i = 0; bvptr[i] != NULL; i++ )
322 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1) * sizeof(struct berval));
323 if ( tmpberval == NULL ) {
324 return LDAP_NO_MEMORY;
327 for ( i = 0; bvptr[i] != NULL; i++ ) {
328 tmpberval[i].bv_val = slapi_ch_malloc( bvptr[i]->bv_len );
329 tmpberval[i].bv_len = bvptr[i]->bv_len;
330 AC_MEMCPY( tmpberval[i].bv_val, bvptr[i]->bv_val, bvptr[i]->bv_len );
333 tmpberval[i].bv_val = NULL;
334 tmpberval[i].bv_len = 0;
342 * Function : slapi_int_ldapmod_to_entry
343 * convert a dn plus an array of LDAPMod struct ptrs to an entry structure
344 * with a link list of the correspondent attributes.
345 * Return value : LDAP_SUCCESS
350 slapi_int_ldapmod_to_entry(
355 struct berval dn = BER_BVNULL;
361 Modifications *modlist = NULL;
362 Modifications **modtail = &modlist;
365 int rc = LDAP_SUCCESS;
368 const char *text = NULL;
370 op = (Operation *)pConn->c_pending_ops.stqh_first;
372 pEntry = (Entry *) ch_calloc( 1, sizeof(Entry) );
373 if ( pEntry == NULL) {
378 dn.bv_val = slapi_ch_strdup(ldn);
379 dn.bv_len = strlen(ldn);
381 rc = dnPrettyNormal( NULL, &dn, &pEntry->e_name, &pEntry->e_nname, NULL );
382 if ( rc != LDAP_SUCCESS ) {
386 if ( rc == LDAP_SUCCESS ) {
387 for ( i = 0, pMod = mods[0]; rc == LDAP_SUCCESS && pMod != NULL; pMod = mods[++i]) {
390 if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
392 * Convert an array of pointers to bervals to
393 * an array of bervals. Note that we need to copy the
394 * values too, as the slap_mods_check() will free the
395 * original values after prettying; the modifications
396 * being passed in may not have been allocated on the
399 rc = bvptr2obj_copy( pMod->mod_bvalues, &bv );
400 if ( rc != LDAP_SUCCESS ) goto cleanup;
401 tmp.sml_type.bv_val = pMod->mod_type;
402 tmp.sml_type.bv_len = strlen( pMod->mod_type );
404 tmp.sml_nvalues = NULL;
406 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
408 mod->sml_op = LDAP_MOD_ADD;
409 mod->sml_next = NULL;
410 mod->sml_desc = NULL;
411 mod->sml_type = tmp.sml_type;
412 mod->sml_values = tmp.sml_values;
413 mod->sml_nvalues = tmp.sml_nvalues;
416 modtail = &mod->sml_next;
419 /* attr values are in string format, need to be converted */
420 /* to an array of bervals */
421 if ( pMod->mod_values == NULL ) {
424 rc = values2obj_copy( pMod->mod_values, &bv );
425 if ( rc != LDAP_SUCCESS ) goto cleanup;
426 tmp.sml_type.bv_val = pMod->mod_type;
427 tmp.sml_type.bv_len = strlen( pMod->mod_type );
429 tmp.sml_nvalues = NULL;
431 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
433 mod->sml_op = LDAP_MOD_ADD;
434 mod->sml_next = NULL;
435 mod->sml_desc = NULL;
436 mod->sml_type = tmp.sml_type;
437 mod->sml_values = tmp.sml_values;
438 mod->sml_nvalues = tmp.sml_nvalues;
441 modtail = &mod->sml_next;
444 } /* for each LDAPMod */
447 op->o_bd = select_backend( &pEntry->e_nname, 0, 0 );
448 if ( op->o_bd == NULL ) {
449 rc = LDAP_PARTIAL_RESULTS;
451 int repl_user = be_isupdate_dn( op->o_bd, &op->o_bd->be_rootdn );
452 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
453 int update = !BER_BVISNULL( &op->o_bd->be_update_ndn );
454 char textbuf[ SLAP_TEXT_BUFLEN ];
455 size_t textlen = sizeof( textbuf );
457 rc = slap_mods_check( modlist, &text,
458 textbuf, textlen, NULL );
459 if ( rc != LDAP_SUCCESS) {
464 rc = slap_mods_no_update_check( modlist,
465 &text, textbuf, textlen );
466 if ( rc != LDAP_SUCCESS) {
472 rc = slap_mods_opattrs( op,
473 modlist, modtail, &text,
474 textbuf, textlen, 1 );
475 if ( rc != LDAP_SUCCESS) {
480 rc = slap_mods2entry( modlist, &pEntry, repl_user,
481 0, &text, textbuf, textlen );
482 if (rc != LDAP_SUCCESS) {
494 slapi_ch_free( (void **)&dn.bv_val );
495 if ( modlist != NULL )
496 slap_mods_free( modlist );
497 if ( rc != LDAP_SUCCESS ) {
498 if ( pEntry != NULL ) {
499 slapi_entry_free( pEntry );
507 /* Function : slapi_delete_internal
509 * Description : Plugin functions call this routine to delete an entry
510 * in the backend directly
511 * Return values : LDAP_SUCCESS
515 * LDAP_UNWILLING_TO_PERFORM
518 slapi_delete_internal(
520 LDAPControl **controls,
524 Connection *pConn = NULL;
525 Operation *op = NULL;
526 Slapi_PBlock *pPB = NULL;
527 SlapReply rs = { REP_RESULT };
528 struct berval dn = BER_BVNULL;
530 int manageDsaIt = SLAP_CONTROL_NONE;
534 rs.sr_err = LDAP_PARAM_ERROR;
538 pConn = slapi_int_init_connection( NULL, LDAP_REQ_DELETE );
540 rs.sr_err = LDAP_NO_MEMORY;
544 op = (Operation *)pConn->c_pending_ops.stqh_first;
545 pPB = (Slapi_PBlock *)op->o_pb;
546 op->o_ctrls = controls;
548 dn.bv_val = slapi_ch_strdup(ldn);
549 dn.bv_len = strlen(ldn);
550 rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
551 if ( rs.sr_err != LDAP_SUCCESS )
554 if ( slapi_control_present( controls,
555 SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical) ) {
556 manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
559 op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
560 if ( op->o_bd == NULL ) {
561 rs.sr_err = LDAP_PARTIAL_RESULTS;
565 op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
566 op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
568 if ( op->o_bd->be_delete ) {
569 int repl_user = be_isupdate( op );
570 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
571 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
572 if ( log_change ) op->o_callback = &cb;
573 if ( (*op->o_bd->be_delete)( op, &rs ) ) {
574 rs.sr_err = LDAP_OTHER;
577 rs.sr_err = LDAP_REFERRAL;
580 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
585 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
588 slapi_ch_free( (void **)&dn.bv_val );
591 slapi_int_connection_destroy( &pConn );
596 #endif /* LDAP_SLAPI */
600 static Slapi_PBlock *
601 slapi_int_add_entry_locked(
604 LDAPControl **controls,
607 Operation *op = NULL;
608 Slapi_PBlock *pPB = NULL;
610 int manageDsaIt = SLAP_CONTROL_NONE;
612 SlapReply rs = { REP_RESULT };
615 rs.sr_err = LDAP_PARAM_ERROR;
619 if ( slapi_control_present( controls, LDAP_CONTROL_MANAGEDSAIT,
620 NULL, &isCritical ) ) {
621 manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
624 op = (Operation *)pConn->c_pending_ops.stqh_first;
625 pPB = (Slapi_PBlock *)op->o_pb;
626 op->o_ctrls = controls;
628 op->o_bd = select_backend( &((*e)->e_nname), manageDsaIt, 1 );
629 if ( op->o_bd == NULL ) {
630 rs.sr_err = LDAP_PARTIAL_RESULTS;
634 op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
635 op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
636 op->oq_add.rs_e = *e;
638 if ( op->o_bd->be_add ) {
639 int repl_user = be_isupdate( op );
640 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
641 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
642 if ( log_changes ) op->o_callback = &cb;
643 if ( (*op->o_bd->be_add)( op, &rs ) == 0 ) {
644 be_entry_release_w( op, *e );
648 rs.sr_err = LDAP_REFERRAL;
651 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
656 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
661 #endif /* LDAP_SLAPI */
664 slapi_add_entry_internal(
666 LDAPControl **controls,
670 Slapi_PBlock *pb = NULL;
671 Slapi_Entry *entry = NULL;
672 Connection *pConn = NULL;
674 pConn = slapi_int_init_connection( NULL, LDAP_REQ_ADD );
675 if ( pConn == NULL ) {
680 * We make a copy to avoid an entry that may be freed later
681 * by the caller being placed in the cache.
683 entry = slapi_entry_dup( e );
684 pb = slapi_int_add_entry_locked( pConn, &entry, controls, log_changes );
685 if ( entry != NULL ) {
686 slapi_entry_free( entry );
689 slapi_int_connection_destroy( &pConn );
701 LDAPControl **controls,
705 LDAPMod *pMod = NULL;
706 Connection *pConn = NULL;
707 Slapi_PBlock *pb = NULL;
708 Entry *pEntry = NULL;
709 int i, rc = LDAP_SUCCESS;
711 if ( mods == NULL || *mods == NULL || dn == NULL || *dn == '\0' ) {
712 rc = LDAP_PARAM_ERROR ;
715 if ( rc == LDAP_SUCCESS ) {
716 for ( i = 0, pMod = mods[0]; pMod != NULL; pMod = mods[++i] ) {
717 if ( (pMod->mod_op & LDAP_MOD_OP ) != LDAP_MOD_ADD ) {
724 if ( rc == LDAP_SUCCESS ) {
725 pConn = slapi_int_init_connection( NULL, LDAP_REQ_ADD );
726 if ( pConn != NULL ) {
727 pEntry = slapi_int_ldapmod_to_entry( pConn, dn, mods );
728 if ( pEntry == NULL ) {
734 if ( rc != LDAP_SUCCESS ) {
735 pb = slapi_pblock_new();
736 slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
738 pb = slapi_int_add_entry_locked( pConn, &pEntry, controls, log_changes );
741 if ( pEntry != NULL ) {
742 slapi_entry_free( pEntry );
745 slapi_int_connection_destroy( &pConn );
750 #endif /* LDAP_SLAPI */
753 /* Function : slapi_modrdn_internal
755 * Description : Plugin functions call this routine to modify the rdn
756 * of an entry in the backend directly
757 * Return values : LDAP_SUCCESS
761 * LDAP_UNWILLING_TO_PERFORM
763 * NOTE: This function does not support the "newSuperior" option from LDAP V3.
766 slapi_modrdn_internal(
770 LDAPControl **controls,
774 struct berval dn = BER_BVNULL;
775 struct berval newrdn = BER_BVNULL;
776 Connection *pConn = NULL;
777 Operation *op = NULL;
778 Slapi_PBlock *pPB = NULL;
779 int manageDsaIt = SLAP_CONTROL_NONE;
781 SlapReply rs = { REP_RESULT };
783 pConn = slapi_int_init_connection( NULL, LDAP_REQ_MODRDN );
784 if ( pConn == NULL) {
785 rs.sr_err = LDAP_NO_MEMORY;
789 op = (Operation *)pConn->c_pending_ops.stqh_first;
790 pPB = (Slapi_PBlock *)op->o_pb;
791 op->o_ctrls = controls;
793 if ( slapi_control_present( controls,
794 SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
795 manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
798 op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
799 if ( op->o_bd == NULL ) {
800 rs.sr_err = LDAP_PARTIAL_RESULTS;
804 op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
805 op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
807 dn.bv_val = slapi_ch_strdup( olddn );
808 dn.bv_len = strlen( olddn );
810 rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
811 if ( rs.sr_err != LDAP_SUCCESS ) {
815 if ( op->o_req_dn.bv_len == 0 ) {
816 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
820 newrdn.bv_val = slapi_ch_strdup( lnewrdn );
821 newrdn.bv_len = strlen( lnewrdn );
823 rs.sr_err = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn, NULL );
824 if ( rs.sr_err != LDAP_SUCCESS ) {
828 if ( rdn_validate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
832 op->oq_modrdn.rs_newSup = NULL;
833 op->oq_modrdn.rs_nnewSup = NULL;
834 op->oq_modrdn.rs_deleteoldrdn = deloldrdn;
836 if ( op->o_bd->be_modrdn ) {
837 int repl_user = be_isupdate( op );
838 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
839 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
840 if ( log_change ) op->o_callback = &cb;
841 if ( (*op->o_bd->be_modrdn)( op, &rs ) ) {
842 rs.sr_err = LDAP_OTHER;
845 rs.sr_err = LDAP_REFERRAL;
848 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
854 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
858 slapi_ch_free( (void **)&dn.bv_val );
861 slapi_ch_free( (void **)&newrdn.bv_val );
862 if ( op->oq_modrdn.rs_newrdn.bv_val )
863 slapi_ch_free( (void **)&op->oq_modrdn.rs_newrdn.bv_val );
864 if ( op->oq_modrdn.rs_nnewrdn.bv_val )
865 slapi_ch_free( (void **)&op->oq_modrdn.rs_nnewrdn.bv_val );
867 slapi_int_connection_destroy( &pConn );
872 #endif /* LDAP_SLAPI */
875 /* Function : slapi_modify_internal
877 * Description: Plugin functions call this routine to modify an entry
878 * in the backend directly
879 * Return values : LDAP_SUCCESS
883 * LDAP_UNWILLING_TO_PERFORM
886 slapi_modify_internal(
889 LDAPControl **controls,
894 Connection *pConn = NULL;
895 Operation *op = NULL;
896 Slapi_PBlock *pPB = NULL;
898 struct berval dn = BER_BVNULL;
900 int manageDsaIt = SLAP_CONTROL_NONE;
905 Modifications *modlist = NULL;
906 Modifications **modtail = &modlist;
909 SlapReply rs = { REP_RESULT };
911 if ( mods == NULL || *mods == NULL || ldn == NULL ) {
912 rs.sr_err = LDAP_PARAM_ERROR ;
916 pConn = slapi_int_init_connection( NULL, LDAP_REQ_MODIFY );
917 if ( pConn == NULL ) {
918 rs.sr_err = LDAP_NO_MEMORY;
922 op = (Operation *)pConn->c_pending_ops.stqh_first;
923 pPB = (Slapi_PBlock *)op->o_pb;
924 op->o_ctrls = controls;
926 dn.bv_val = slapi_ch_strdup( ldn );
927 dn.bv_len = strlen( ldn );
928 rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
929 if ( rs.sr_err != LDAP_SUCCESS ) {
933 if ( slapi_control_present( controls,
934 SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
935 manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
938 op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
939 if ( op->o_bd == NULL ) {
940 rs.sr_err = LDAP_PARTIAL_RESULTS;
944 op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
945 op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
947 for ( i = 0, pMod = mods[0];
948 rs.sr_err == LDAP_SUCCESS && pMod != NULL;
953 if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
955 * attr values are in berval format
956 * convert an array of pointers to bervals
957 * to an array of bervals
959 rs.sr_err = bvptr2obj_copy( pMod->mod_bvalues, &bv );
960 if ( rs.sr_err != LDAP_SUCCESS )
962 tmp.sml_type.bv_val = pMod->mod_type;
963 tmp.sml_type.bv_len = strlen( pMod->mod_type );
965 tmp.sml_nvalues = NULL;
967 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
969 mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
970 mod->sml_next = NULL;
971 mod->sml_desc = NULL;
972 mod->sml_type = tmp.sml_type;
973 mod->sml_values = tmp.sml_values;
974 mod->sml_nvalues = tmp.sml_nvalues;
976 rs.sr_err = values2obj_copy( pMod->mod_values, &bv );
977 if ( rs.sr_err != LDAP_SUCCESS )
979 tmp.sml_type.bv_val = pMod->mod_type;
980 tmp.sml_type.bv_len = strlen( pMod->mod_type );
982 tmp.sml_nvalues = NULL;
984 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
986 mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
987 mod->sml_next = NULL;
988 mod->sml_desc = NULL;
989 mod->sml_type = tmp.sml_type;
990 mod->sml_values = tmp.sml_values;
991 mod->sml_nvalues = tmp.sml_nvalues;
994 modtail = &mod->sml_next;
996 switch( pMod->mod_op & LDAP_MOD_OP ) {
998 if ( mod->sml_values == NULL ) {
999 rs.sr_err = LDAP_PROTOCOL_ERROR;
1004 case LDAP_MOD_DELETE:
1005 case LDAP_MOD_REPLACE:
1006 case LDAP_MOD_INCREMENT:
1010 rs.sr_err = LDAP_PROTOCOL_ERROR;
1016 if ( op->o_req_ndn.bv_len == 0 ) {
1017 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1021 op->oq_modify.rs_modlist = modlist;
1023 if ( op->o_bd->be_modify ) {
1024 int repl_user = be_isupdate( op );
1025 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
1026 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
1027 const char *text = NULL;
1028 char textbuf[ SLAP_TEXT_BUFLEN ];
1029 size_t textlen = sizeof( textbuf );
1030 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
1032 rs.sr_err = slap_mods_check( modlist,
1033 &text, textbuf, textlen, NULL );
1034 if ( rs.sr_err != LDAP_SUCCESS ) {
1039 rs.sr_err = slap_mods_no_update_check( modlist,
1040 &text, textbuf, textlen );
1041 if ( rs.sr_err != LDAP_SUCCESS ) {
1047 rs.sr_err = slap_mods_opattrs( op, modlist,
1048 modtail, &text, textbuf,
1050 if ( rs.sr_err != LDAP_SUCCESS ) {
1054 if ( log_change ) op->o_callback = &cb;
1055 if ( (*op->o_bd->be_modify)( op, &rs ) ) {
1056 rs.sr_err = LDAP_OTHER;
1059 rs.sr_err = LDAP_REFERRAL;
1062 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1068 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1071 slapi_ch_free( (void **)&dn.bv_val );
1073 if ( modlist != NULL )
1074 slap_mods_free( modlist );
1076 slapi_int_connection_destroy( &pConn );
1081 #endif /* LDAP_SLAPI */
1085 slapi_search_internal(
1089 LDAPControl **controls,
1095 Operation *op = NULL;
1096 Slapi_PBlock *pPB = NULL;
1097 struct berval dn = BER_BVNULL;
1098 Filter *filter=NULL;
1099 struct berval fstr = BER_BVNULL;
1100 AttributeName *an = NULL;
1101 const char *text = NULL;
1103 int manageDsaIt = SLAP_CONTROL_NONE;
1107 SlapReply rs = { REP_RESULT };
1109 c = slapi_int_init_connection( NULL, LDAP_REQ_SEARCH );
1111 rs.sr_err = LDAP_NO_MEMORY;
1115 op = (Operation *)c->c_pending_ops.stqh_first;
1116 pPB = (Slapi_PBlock *)op->o_pb;
1117 op->o_ctrls = controls;
1119 if ( ldn != NULL ) {
1120 dn.bv_val = slapi_ch_strdup(ldn);
1121 dn.bv_len = strlen(ldn);
1124 rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
1125 if ( rs.sr_err != LDAP_SUCCESS ) {
1129 if ( scope != LDAP_SCOPE_BASE &&
1130 scope != LDAP_SCOPE_ONELEVEL &&
1131 scope != LDAP_SCOPE_SUBTREE ) {
1132 rs.sr_err = LDAP_PROTOCOL_ERROR;
1136 filter = slapi_str2filter(filStr);
1137 if ( filter == NULL ) {
1138 rs.sr_err = LDAP_PROTOCOL_ERROR;
1142 filter2bv( filter, &fstr );
1144 for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
1145 ; /* count the number of attributes */
1149 an = (AttributeName *)slapi_ch_calloc( (i + 1), sizeof(AttributeName) );
1150 for (i = 0; attrs[i] != 0; i++) {
1151 an[i].an_desc = NULL;
1153 an[i].an_oc_exclude = 0;
1154 an[i].an_name.bv_val = slapi_ch_strdup(attrs[i]);
1155 an[i].an_name.bv_len = strlen(attrs[i]);
1156 slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1158 an[i].an_name.bv_val = NULL;
1161 memset( &rs, 0, sizeof(rs) );
1162 rs.sr_type = REP_RESULT;
1163 rs.sr_err = LDAP_SUCCESS;
1164 rs.sr_entry = NULL; /* paranoia */
1166 if ( scope == LDAP_SCOPE_BASE ) {
1169 if ( op->o_req_ndn.bv_len == 0 ) {
1170 rs.sr_err = root_dse_info( c, &rs.sr_entry, &rs.sr_text );
1173 if( rs.sr_err != LDAP_SUCCESS ) {
1174 send_ldap_result( op, &rs );
1176 } else if ( rs.sr_entry != NULL ) {
1177 rs.sr_err = test_filter( op, rs.sr_entry, filter );
1179 if ( rs.sr_err == LDAP_COMPARE_TRUE ) {
1180 rs.sr_type = REP_SEARCH;
1181 rs.sr_err = LDAP_SUCCESS;
1183 rs.sr_operational_attrs = NULL;
1184 rs.sr_flags = REP_ENTRY_MODIFIABLE;
1186 send_search_entry( op, &rs );
1189 entry_free( rs.sr_entry );
1191 rs.sr_type = REP_RESULT;
1192 rs.sr_err = LDAP_SUCCESS;
1194 send_ldap_result( op, &rs );
1200 if ( !op->o_req_ndn.bv_len && default_search_nbase.bv_len ) {
1201 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
1202 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
1204 ber_dupbv( &op->o_req_dn, &default_search_base );
1205 ber_dupbv( &op->o_req_ndn, &default_search_nbase );
1208 if ( slapi_control_present( controls,
1209 LDAP_CONTROL_MANAGEDSAIT, NULL, &isCritical ) ) {
1210 manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1213 op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
1214 if ( op->o_bd == NULL ) {
1215 if ( manageDsaIt > SLAP_CONTROL_NONE ) {
1216 rs.sr_err = LDAP_NO_SUCH_OBJECT;
1218 rs.sr_err = LDAP_PARTIAL_RESULTS;
1223 op->o_dn = c->c_dn = op->o_bd->be_rootdn;
1224 op->o_ndn = c->c_ndn = op->o_bd->be_rootndn;
1226 op->oq_search.rs_scope = scope;
1227 op->oq_search.rs_deref = 0;
1228 op->oq_search.rs_slimit = SLAP_NO_LIMIT;
1229 op->oq_search.rs_tlimit = SLAP_NO_LIMIT;
1230 op->oq_search.rs_attrsonly = attrsonly;
1231 op->oq_search.rs_attrs = an;
1232 op->oq_search.rs_filter = filter;
1233 op->oq_search.rs_filterstr = fstr;
1235 if ( op->o_bd->be_search ) {
1236 if ( (*op->o_bd->be_search)( op, &rs ) != 0 ) {
1237 rs.sr_err = LDAP_OTHER;
1240 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1246 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1249 slapi_ch_free( (void **)&dn.bv_val );
1251 slapi_filter_free( filter, 1 );
1253 slapi_ch_free( (void **)&fstr.bv_val );
1255 slapi_ch_free( (void **)&an );
1257 slapi_int_connection_destroy( &c );
1262 #endif /* LDAP_SLAPI */