]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_ops.c
191b31b6df4c3b257dc4c1a04ae392459c31d749
[openldap] / servers / slapd / slapi / slapi_ops.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2002-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1997,2002-2003 IBM Corporation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
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:
20  *   Luke Howard
21  */
22
23 #include "portable.h"
24
25 #include <ac/string.h>
26 #include <ac/stdarg.h>
27 #include <ac/ctype.h>
28 #include <ac/unistd.h>
29
30 #include <slap.h>
31 #include <lber_pvt.h>
32 #include <slapi.h>
33
34 /*
35  * use a fake listener when faking a connection,
36  * so it can be used in ACLs
37  */
38 static struct slap_listener slap_unknown_listener = {
39         BER_BVC("unknown"),     /* FIXME: use a URI form? (e.g. slapi://) */
40         BER_BVC("UNKNOWN")
41 };
42
43 static void
44 slapi_int_send_ldap_result_shim(
45         Operation       *op, 
46         SlapReply       *rs )
47 {
48         LDAPControl             **controls = NULL;
49         size_t                  i;
50         plugin_result_callback  prc = NULL;
51         void                    *callback_data = NULL;
52
53         assert( op->o_pb != NULL );
54
55         slapi_pblock_get( op->o_pb, SLAPI_RESCONTROLS, (void **)&controls );
56         slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_RESULT_CALLBACK, (void **)&prc );
57         slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_CALLBACK_DATA, &callback_data );
58
59         assert( controls == NULL );
60
61         /* Copy these before they go out of scope */
62         if ( rs->sr_ctrls != NULL ) {
63                 for ( i = 0; rs->sr_ctrls[i] != NULL; i++ )
64                         ;
65
66                 controls = (LDAPControl **)slapi_ch_calloc( i + 1,
67                         sizeof(LDAPControl ));
68
69                 for ( i = 0; rs->sr_ctrls[i] != NULL; i++ )
70                         controls[i] = slapi_dup_control( rs->sr_ctrls[i] );
71
72                 controls[i] = NULL;
73         }
74
75         slapi_pblock_set( op->o_pb, SLAPI_RESCONTROLS, (void *)controls );
76         slapi_pblock_set( op->o_pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs->sr_err );
77
78         if ( prc != NULL ) {
79                 (*prc)( rs->sr_err, callback_data );
80         }
81
82         return;
83 }
84
85 static int
86 slapi_int_send_search_entry_shim(
87         Operation       *op,
88         SlapReply       *rs )
89 {
90         plugin_search_entry_callback    psec = NULL;
91         void                            *callback_data = NULL;
92
93         assert( op->o_pb != NULL );
94
95         slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK, &psec );
96         slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_CALLBACK_DATA, &callback_data );
97
98         if ( psec != NULL ) {
99                 return (*psec)( rs->sr_entry, callback_data );
100         }
101
102         return LDAP_SUCCESS;
103 }
104
105 static void
106 slapi_int_send_ldap_extended_shim(
107         Operation       *op,    
108         SlapReply       *rs )
109 {
110         assert( op->o_pb != NULL );
111
112         return;
113 }
114
115 static int
116 slapi_int_send_search_reference_shim(
117         Operation       *op,    
118         SlapReply       *rs )
119 {
120         int                             i, rc = LDAP_SUCCESS;
121         plugin_referral_entry_callback  prec = NULL;
122         void                            *callback_data = NULL;
123
124         assert( op->o_pb != NULL );
125
126         slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, &prec );
127         slapi_pblock_get( op->o_pb, SLAPI_X_INTOP_CALLBACK_DATA, &callback_data );
128
129         if ( prec != NULL ) {
130                 for ( i = 0; rs->sr_ref[i].bv_val != NULL; i++ ) {
131                         rc = (*prec)( rs->sr_ref[i].bv_val, callback_data );
132                         if ( rc != LDAP_SUCCESS ) {
133                                 break;
134                         }
135                 }
136         }
137
138         return rc;
139 }
140
141 static int
142 slapi_int_get_ctrls( Operation *op, SlapReply *rs, LDAPControl **controls )
143 {
144         LDAPControl **c;
145         int rc;
146
147         op->o_ctrls = controls;
148         if ( op->o_ctrls == NULL ) {
149                 return LDAP_SUCCESS;
150         }
151
152         for ( c = op->o_ctrls; *c != NULL; c++ ) {
153                 rc = slap_parse_ctrl( op, rs, *c, &rs->sr_text );
154                 if ( rc != LDAP_SUCCESS )
155                         break;
156         }
157
158         return rc;
159 }
160
161 /*
162  * To allow plugins to forward frontend requests to internal operations,
163  * the internal operation and connection structures should import as
164  * much state as practicable from the supplied parameter block.
165  */
166
167 /*
168  * Select the backend to be used for an internal operation, either
169  * from the operation target DN or from the parameter block.
170  */
171 static int
172 slapi_int_pblock_get_backend( Slapi_PBlock *pb, Operation *op )
173 {
174         int manageDsaIt = 0, isCritical;
175         LDAPControl **controls = NULL;
176         int rc;
177
178         slapi_pblock_get( pb, SLAPI_REQCONTROLS, (void **)&controls );
179
180         slapi_pblock_get( pb, SLAPI_MANAGEDSAIT, (void **)&manageDsaIt );
181         if ( manageDsaIt != 0 )
182                 manageDsaIt = SLAP_CONTROL_CRITICAL;
183         else if ( slapi_control_present( controls, SLAPI_CONTROL_MANAGEDSAIT_OID,
184                     NULL, &isCritical ))
185                 manageDsaIt = isCritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
186
187         /* let caller force a specific backend */
188         slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&op->o_bd );
189         if ( op->o_bd == NULL ) {
190                 op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 1 );
191                 if ( op->o_bd == NULL ) {
192                         if ( op->o_tag == LDAP_REQ_SEARCH && manageDsaIt > SLAP_CONTROL_NONE )
193                                 rc = LDAP_NO_SUCH_OBJECT;
194                         else
195                                 rc = LDAP_PARTIAL_RESULTS;
196                 }
197         }
198
199         if ( op->o_bd != NULL ) {
200                 rc = LDAP_SUCCESS;
201         }
202
203         return rc;
204 }
205
206 static int
207 slapi_int_pblock_get_connection( Slapi_PBlock *pb, Operation *op )
208 {
209         char *connDn = NULL;
210         Connection *conn = op->o_conn;
211
212         slapi_pblock_get( pb, SLAPI_X_CONN_SSF, (void **)&conn->c_ssf );
213         slapi_pblock_get( pb, SLAPI_X_CONN_SASL_CONTEXT, (void **)&conn->c_sasl_authctx );
214
215         if ( slapi_pblock_get( pb, SLAPI_CONN_DN, (void **)&connDn ) != 0 ||
216              connDn == NULL ) {
217                 /* default to operation DN */
218                 conn->c_ndn = op->o_ndn;
219                 conn->c_dn = op->o_ndn;
220         } else {
221                 /* NB: conn DN must be normalized */
222                 conn->c_ndn.bv_val = connDn;
223                 conn->c_ndn.bv_len = strlen( connDn );
224
225                 conn->c_dn = conn->c_ndn;
226         }
227
228         return LDAP_SUCCESS;
229 }
230
231 static int
232 slapi_int_pblock_get_operation( Slapi_PBlock *pb, Operation *op, SlapReply *rs )
233 {
234         int isRoot = 0;
235         int isUpdateDn = 0;
236         char *requestorDn = NULL;
237         struct berval targetDn = BER_BVNULL;
238         LDAPControl **controls;
239         int rc;
240
241         /* All internal operations must specify a target DN */
242         if ( slapi_pblock_get( pb, SLAPI_TARGET_DN, (void **)&targetDn.bv_val ) != 0 ||
243              targetDn.bv_val == NULL ) {
244                 return LDAP_PARAM_ERROR; 
245         }
246
247         targetDn.bv_len = strlen( targetDn.bv_val );
248
249         rc = dnPrettyNormal( NULL, &targetDn, &op->o_req_dn, &op->o_req_ndn, NULL );
250         if ( rc != LDAP_SUCCESS ) {
251                 return rc;
252         }
253
254         rc = slapi_int_pblock_get_backend( pb, op );
255         if ( rc != LDAP_SUCCESS ) {
256                 return rc;
257         }
258
259         slapi_pblock_get( pb, SLAPI_REQUESTOR_ISROOT, (void **)&isRoot );
260         slapi_pblock_get( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void **)&isUpdateDn );
261         /* NB: requestor DN must be normalized */
262         slapi_pblock_get( pb, SLAPI_REQUESTOR_DN, (void **)&requestorDn );
263
264         /* Default authorization identity for internal operations is root DN */
265         if ( isRoot || requestorDn == NULL ) {
266                 op->o_dn = op->o_bd->be_rootdn;
267                 op->o_ndn = op->o_bd->be_rootndn;
268         } else {
269                 op->o_ndn.bv_val = requestorDn;
270                 op->o_ndn.bv_len = strlen( requestorDn );
271                 op->o_dn = op->o_ndn;
272         }
273
274         rc = slapi_int_pblock_get_connection( pb, op );
275         if ( rc != LDAP_SUCCESS ) {
276                 return rc;
277         }
278
279         slapi_pblock_get( pb, SLAPI_REQCONTROLS, (void **)&controls );
280         rc = slapi_int_get_ctrls( op, rs, controls );
281         if ( rc != LDAP_SUCCESS ) {
282                 return rs->sr_err;
283         }
284
285         return LDAP_SUCCESS;
286 }
287
288 int
289 slapi_int_init_connection( Slapi_PBlock *pb,
290         SlapReply *rs,
291         int OpType,
292         Connection **pConn )
293 {
294         Connection *conn;
295         Operation *op;
296         ber_len_t max = sockbuf_max_incoming;
297         int rc;
298
299         conn = (Connection *) slapi_ch_calloc(1, sizeof(Connection));
300
301         LDAP_STAILQ_INIT( &conn->c_pending_ops );
302
303         op = (Operation *) slapi_ch_calloc( 1, OPERATION_BUFFER_SIZE );
304         op->o_hdr = (Opheader *)(op + 1);
305         op->o_controls = (void **)(op->o_hdr + 1);
306         op->o_pb = pb;
307         op->o_extensions = NULL;
308
309         conn->c_pending_ops.stqh_first = op;
310
311         /* connection object authorization information */
312         conn->c_authtype = LDAP_AUTH_NONE;
313         conn->c_authmech.bv_val = NULL;
314         conn->c_authmech.bv_len = 0;
315         conn->c_dn.bv_val = NULL;
316         conn->c_dn.bv_len = 0;
317         conn->c_ndn.bv_val = NULL;
318         conn->c_ndn.bv_len = 0;
319
320         conn->c_listener = &slap_unknown_listener;
321         ber_dupbv( &conn->c_peer_domain, (struct berval *)&slap_unknown_bv );
322         ber_dupbv( &conn->c_peer_name, (struct berval *)&slap_unknown_bv );
323
324         LDAP_STAILQ_INIT( &conn->c_ops );
325
326         conn->c_sasl_bind_mech.bv_val = NULL;
327         conn->c_sasl_bind_mech.bv_len = 0;
328         conn->c_sasl_authctx = NULL;
329         conn->c_sasl_sockctx = NULL;
330         conn->c_sasl_extra = NULL;
331
332         conn->c_sb = ber_sockbuf_alloc( );
333
334         ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
335
336         conn->c_currentber = NULL;
337
338         /* should check status of thread calls */
339         ldap_pvt_thread_mutex_init( &conn->c_mutex );
340         ldap_pvt_thread_mutex_init( &conn->c_write_mutex );
341         ldap_pvt_thread_cond_init( &conn->c_write_cv );
342
343         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
344
345         conn->c_n_ops_received = 0;
346         conn->c_n_ops_executing = 0;
347         conn->c_n_ops_pending = 0;
348         conn->c_n_ops_completed = 0;
349
350         conn->c_n_get = 0;
351         conn->c_n_read = 0;
352         conn->c_n_write = 0;
353
354         conn->c_protocol = LDAP_VERSION3; 
355
356         conn->c_activitytime = conn->c_starttime = slap_get_time();
357
358         /*
359          * A real connection ID is required, because syncrepl associates
360          * pending CSNs with unique ( connection, operation ) tuples.
361          * Setting a fake connection ID will cause slap_get_commit_csn()
362          * to return a stale value.
363          */
364         connection_assign_nextid( conn );
365
366         conn->c_conn_state  = 0x01;     /* SLAP_C_ACTIVE */
367         conn->c_struct_state = 0x02;    /* SLAP_C_USED */
368
369         conn->c_ssf = conn->c_transport_ssf = 0;
370         conn->c_tls_ssf = 0;
371
372         backend_connection_init( conn );
373
374         conn->c_send_ldap_result = slapi_int_send_ldap_result_shim;
375         conn->c_send_search_entry = slapi_int_send_search_entry_shim;
376         conn->c_send_ldap_extended = slapi_int_send_ldap_extended_shim;
377         conn->c_send_search_reference = slapi_int_send_search_reference_shim;
378
379         /* operation object */
380         op->o_tag = OpType;
381         op->o_protocol = LDAP_VERSION3; 
382         op->o_authmech.bv_val = NULL; 
383         op->o_authmech.bv_len = 0; 
384         op->o_time = slap_get_time();
385         op->o_do_not_cache = 1;
386         op->o_threadctx = ldap_pvt_thread_pool_context();
387         op->o_tmpmemctx = NULL;
388         op->o_tmpmfuncs = &ch_mfuncs;
389         op->o_conn = conn;
390         op->o_connid = conn->c_connid;
391
392         rc = slapi_int_pblock_get_operation( pb, op, rs );
393
394         slapi_pblock_set( op->o_pb, SLAPI_OPERATION, op );
395         slapi_pblock_set( op->o_pb, SLAPI_CONNECTION, conn );
396
397         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
398
399         if ( rc != LDAP_SUCCESS ) {
400                 slapi_int_connection_destroy( &conn );
401                 return rc;
402         }
403
404         *pConn = conn;
405
406         return LDAP_SUCCESS;
407 }
408
409 void slapi_int_connection_destroy( Connection **pConn )
410 {
411         Connection *conn = *pConn;
412         Operation *op;
413
414         if ( conn == NULL ) {
415                 return;
416         }
417
418         op = (Operation *)conn->c_pending_ops.stqh_first;
419
420         slap_graduate_commit_csn( op );
421
422         if ( op->o_req_dn.bv_val != NULL ) {
423                 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
424         }
425         if ( op->o_req_ndn.bv_val != NULL ) {
426                 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
427         }
428
429         if ( conn->c_sb != NULL ) {
430                 ber_sockbuf_free( conn->c_sb );
431         }
432
433         slapi_pblock_set( op->o_pb, SLAPI_OPERATION, NULL );
434         slapi_pblock_set( op->o_pb, SLAPI_CONNECTION, NULL );
435
436         if ( op != NULL ) {
437                 slapi_ch_free( (void **)&op );
438         }
439
440         slapi_ch_free( (void **)pConn );
441 }
442
443 /*
444  * Function : values2obj
445  * Convert an array of strings into a BerVarray.
446  * the strings.
447  */
448 static int
449 values2obj_copy(
450         char **ppValue,
451         BerVarray *bvobj )
452 {
453         int i;
454         BerVarray tmpberval;
455
456         if ( ppValue == NULL ) {
457                 *bvobj = NULL;
458                 return LDAP_SUCCESS;
459         }
460
461         for ( i = 0; ppValue[i] != NULL; i++ )
462                 ; /* EMPTY */
463
464         tmpberval = (BerVarray)slapi_ch_malloc( (i+1) * (sizeof(struct berval)) );
465         if ( tmpberval == NULL ) {
466                 return LDAP_NO_MEMORY;
467         }
468         for ( i = 0; ppValue[i] != NULL; i++ ) {
469                 size_t len = strlen( ppValue[i] );
470
471                 tmpberval[i].bv_val = slapi_ch_malloc( len + 1 );
472                 AC_MEMCPY( tmpberval[i].bv_val, ppValue[i], len + 1 );
473                 tmpberval[i].bv_len = len;
474         }
475         tmpberval[i].bv_val = NULL;
476         tmpberval[i].bv_len = 0;
477
478         *bvobj = tmpberval;
479
480         return LDAP_SUCCESS;
481 }
482
483 static int
484 bvptr2obj_copy(
485         struct berval   **bvptr, 
486         BerVarray       *bvobj )
487 {
488         int             i;
489         BerVarray       tmpberval;
490
491         if ( bvptr == NULL ) {
492                 *bvobj = NULL;
493                 return LDAP_SUCCESS;
494         }
495
496         for ( i = 0; bvptr[i] != NULL; i++ )
497                 ; /* EMPTY */
498
499         tmpberval = (BerVarray)slapi_ch_malloc( (i + 1) * sizeof(struct berval));
500         if ( tmpberval == NULL ) {
501                 return LDAP_NO_MEMORY;
502         } 
503
504         for ( i = 0; bvptr[i] != NULL; i++ ) {
505                 tmpberval[i].bv_val = slapi_ch_malloc( bvptr[i]->bv_len );
506                 tmpberval[i].bv_len = bvptr[i]->bv_len;
507                 AC_MEMCPY( tmpberval[i].bv_val, bvptr[i]->bv_val, bvptr[i]->bv_len );
508         }
509
510         tmpberval[i].bv_val = NULL;
511         tmpberval[i].bv_len = 0;
512
513         *bvobj = tmpberval;
514
515         return LDAP_SUCCESS;
516 }
517
518 /*
519  * Function : slapi_int_ldapmod_to_entry 
520  * convert a dn plus an array of LDAPMod struct ptrs to an entry structure
521  * with a link list of the correspondent attributes.
522  * Return value : LDAP_SUCCESS
523  *                LDAP_NO_MEMORY
524  *                LDAP_OTHER
525 */
526 static Entry *
527 slapi_int_ldapmod_to_entry(
528         Connection *pConn,
529         char *ldn, 
530         LDAPMod **mods )
531 {
532         struct berval           dn = BER_BVNULL;
533         Entry                   *pEntry=NULL;
534         LDAPMod                 *pMod;
535         struct berval           *bv;
536         Operation               *op;
537
538         Modifications           *modlist = NULL;
539         Modifications           **modtail = &modlist;
540         Modifications           tmp;
541
542         int                     rc = LDAP_SUCCESS;
543         int                     i;
544
545         const char              *text = NULL;
546
547         op = (Operation *)pConn->c_pending_ops.stqh_first;
548
549         pEntry = (Entry *) ch_calloc( 1, sizeof(Entry) );
550         if ( pEntry == NULL) {
551                 rc = LDAP_NO_MEMORY;
552                 goto cleanup;
553         } 
554
555         dn.bv_val = ldn;
556         dn.bv_len = strlen( ldn );
557
558         rc = dnPrettyNormal( NULL, &dn, &pEntry->e_name, &pEntry->e_nname, NULL );
559         if ( rc != LDAP_SUCCESS ) {
560                 goto cleanup;
561         }
562
563         if ( rc == LDAP_SUCCESS ) {
564                 for ( i = 0, pMod = mods[0]; rc == LDAP_SUCCESS && pMod != NULL; pMod = mods[++i]) {
565                         Modifications *mod;
566
567                         if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
568                                 /*
569                                  * Convert an array of pointers to bervals to
570                                  * an array of bervals. Note that we need to copy the
571                                  * values too, as the slap_mods_check() will free the
572                                  * original values after prettying; the modifications
573                                  * being passed in may not have been allocated on the
574                                  * heap.
575                                  */
576                                 rc = bvptr2obj_copy( pMod->mod_bvalues, &bv );
577                                 if ( rc != LDAP_SUCCESS ) goto cleanup;
578                                 tmp.sml_type.bv_val = pMod->mod_type;
579                                 tmp.sml_type.bv_len = strlen( pMod->mod_type );
580                                 tmp.sml_values = bv;
581                                 tmp.sml_nvalues = NULL;
582                 
583                                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
584
585                                 mod->sml_op = LDAP_MOD_ADD;
586                                 mod->sml_flags = 0;
587                                 mod->sml_next = NULL;
588                                 mod->sml_desc = NULL;
589                                 mod->sml_type = tmp.sml_type;
590                                 mod->sml_values = tmp.sml_values;
591                                 mod->sml_nvalues = tmp.sml_nvalues;
592
593                                 *modtail = mod;
594                                 modtail = &mod->sml_next;
595
596                         } else {
597                                 /* attr values are in string format, need to be converted */
598                                 /* to an array of bervals */ 
599                                 if ( pMod->mod_values == NULL ) {
600                                         rc = LDAP_OTHER;
601                                 } else {
602                                         rc = values2obj_copy( pMod->mod_values, &bv );
603                                         if ( rc != LDAP_SUCCESS ) goto cleanup;
604                                         tmp.sml_type.bv_val = pMod->mod_type;
605                                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
606                                         tmp.sml_values = bv;
607                                         tmp.sml_nvalues = NULL;
608                 
609                                         mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
610
611                                         mod->sml_op = LDAP_MOD_ADD;
612                                         mod->sml_flags = 0;
613                                         mod->sml_next = NULL;
614                                         mod->sml_desc = NULL;
615                                         mod->sml_type = tmp.sml_type;
616                                         mod->sml_values = tmp.sml_values;
617                                         mod->sml_nvalues = tmp.sml_nvalues;
618
619                                         *modtail = mod;
620                                         modtail = &mod->sml_next;
621                                 }
622                         }
623                 } /* for each LDAPMod */
624         }
625
626         op->o_bd = select_backend( &pEntry->e_nname, 0, 0 );
627         if ( op->o_bd == NULL ) {
628                 rc = LDAP_PARTIAL_RESULTS;
629         } else {
630                 int repl_user = be_isupdate_dn( op->o_bd, &op->o_bd->be_rootdn );
631                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
632                         int     update = !BER_BVISNULL( &op->o_bd->be_update_ndn );
633                         char    textbuf[ SLAP_TEXT_BUFLEN ];
634                         size_t  textlen = sizeof( textbuf );
635
636                         rc = slap_mods_check( modlist, &text, 
637                                 textbuf, textlen, NULL );
638
639                         if ( rc != LDAP_SUCCESS) {
640                                 goto cleanup;
641                         }
642
643                         if ( !update ) {
644                                 rc = slap_mods_no_user_mod_check( op, modlist,
645                                         &text, textbuf, textlen );
646                                 if ( rc != LDAP_SUCCESS) {
647                                         goto cleanup;
648                                 }
649                         }
650
651                         if ( !repl_user ) {
652                                 rc = slap_mods_opattrs( op, modlist, modtail,
653                                         &text, textbuf, textlen, 1 );
654                                 if ( rc != LDAP_SUCCESS) {
655                                         goto cleanup;
656                                 }
657                         }
658
659                         rc = slap_mods2entry( modlist, &pEntry, repl_user,
660                                               0, &text, textbuf, textlen );
661                         if (rc != LDAP_SUCCESS) {
662                                 goto cleanup;
663                         }
664
665                 } else {
666                         rc = LDAP_REFERRAL;
667                 }
668         }
669
670 cleanup:;
671         if ( modlist != NULL )
672                 slap_mods_free( modlist );
673         if ( rc != LDAP_SUCCESS ) {
674                 if ( pEntry != NULL ) {
675                         slapi_entry_free( pEntry );
676                 }
677                 pEntry = NULL;
678         }
679
680         return( pEntry );
681 }
682
683 int
684 slapi_delete_internal_pb( Slapi_PBlock *pb )
685 {
686 #ifdef LDAP_SLAPI
687         Connection              *conn = NULL;
688         Operation               *op = NULL;
689         int                     operation_flags = 0;
690
691         SlapReply               rs = { REP_RESULT };
692
693         if ( pb == NULL ) {
694                 return -1;
695         }
696
697         slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
698
699         rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_DELETE, &conn );
700         if ( rs.sr_err != LDAP_SUCCESS ) {
701                 goto cleanup;
702         }
703
704         op = conn->c_pending_ops.stqh_first;
705
706         if ( op->o_bd->be_delete != NULL ) {
707                 int repl_user = be_isupdate( op );
708                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
709                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
710                         if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
711                                 op->o_callback = &cb;
712
713                         op->o_bd->be_delete( op, &rs );
714                 } else {
715                         rs.sr_err = LDAP_REFERRAL;
716                 }
717         } else {
718                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
719         }
720
721 cleanup:
722         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
723         slapi_int_connection_destroy( &conn );
724
725         return 0;
726 #else
727         return -1;
728 #endif /* LDAP_SLAPI */
729 }
730
731 int
732 slapi_add_internal_pb( Slapi_PBlock *pb )
733 {
734 #ifdef LDAP_SLAPI
735         Connection              *conn = NULL;
736         Slapi_Entry             *entry = NULL;
737         Slapi_Entry             *argEntry = NULL;
738         char                    *dn = NULL;
739         LDAPMod                 **mods = NULL;
740         int                     operation_flags = 0;
741         Operation               *op = NULL;
742         int                     i;
743
744         SlapReply               rs = { REP_RESULT };
745
746         if ( pb == NULL ) {
747                 return -1;
748         }
749
750         slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &argEntry );
751         slapi_pblock_get( pb, SLAPI_ADD_TARGET, &dn );
752         slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
753         slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
754
755         if ( argEntry != NULL ) {
756                 /*
757                  * The caller can specify a new entry, or a target DN and set
758                  * of modifications, but not both.
759                  */
760                 if ( dn != NULL ) {
761                         rs.sr_err = LDAP_PARAM_ERROR;
762                         goto cleanup;
763                 }
764
765                 /*
766                  * Set the DN here so that slapi_int_init_backend() will select
767                  * correct backend.
768                  */
769                 dn = slapi_entry_get_dn( argEntry );
770                 slapi_pblock_set( pb, SLAPI_ADD_TARGET, &entry->e_nname.bv_val );
771         }
772
773         rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_ADD, &conn );
774         if ( rs.sr_err != LDAP_SUCCESS )
775                 goto cleanup;
776
777         if ( argEntry != NULL ) {
778                 entry = slapi_entry_dup( argEntry );
779         } else {
780                 if ( mods == NULL || dn == NULL ) {
781                         rs.sr_err = LDAP_PARAM_ERROR;
782                         goto cleanup;
783                 }
784
785                 for ( i = 0; mods[i] != NULL; i++ ) {
786                         if ( (mods[i]->mod_op & LDAP_MOD_OP ) != LDAP_MOD_ADD ) {
787                                 rs.sr_err = LDAP_PARAM_ERROR;
788                                 goto cleanup;
789                         }
790                 }
791
792                 entry = slapi_int_ldapmod_to_entry( conn, dn, mods );
793                 if ( entry == NULL ) {
794                         rs.sr_err = LDAP_OTHER;
795                         goto cleanup;
796                 }
797         }
798
799         op = (Operation *)conn->c_pending_ops.stqh_first;
800         op->oq_add.rs_e = entry;
801
802         if ( op->o_bd->be_add != NULL ) {
803                 int repl_user = be_isupdate( op );
804                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
805                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
806
807                         if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
808                                 op->o_callback = &cb;
809
810                         if ( op->o_bd->be_add( op, &rs ) == LDAP_SUCCESS ) {
811                                 be_entry_release_w( op, entry );
812                                 entry = NULL;
813                         }
814                 } else {
815                         rs.sr_err = LDAP_REFERRAL;
816                 }
817         } else {
818                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
819         }
820
821 cleanup:
822         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
823
824         if ( entry != NULL ) {
825                 slapi_entry_free( entry );
826         }
827         if ( argEntry != NULL ) {
828                 slapi_pblock_set( pb, SLAPI_ADD_TARGET, NULL );
829         }
830
831         slapi_int_connection_destroy( &conn );
832
833         return 0;
834 #else
835         return -1;
836 #endif /* LDAP_SLAPI */
837 }
838
839 int
840 slapi_modrdn_internal_pb( Slapi_PBlock *pb )
841 {
842 #ifdef LDAP_SLAPI
843         struct berval           dn = BER_BVNULL;
844         struct berval           newrdn = BER_BVNULL;
845         struct berval           newsupdn = BER_BVNULL;
846         struct berval           newSuperiorPretty = BER_BVNULL;
847         struct berval           newSuperiorNormalized = BER_BVNULL;
848         Connection              *conn = NULL;
849         Operation               *op = NULL;
850         int                     manageDsaIt = SLAP_CONTROL_NONE;
851         int                     isCritical;
852
853         char                    *lnewrdn;
854         char                    *newsuperior;
855         int                     deloldrdn;
856         int                     operation_flags;
857
858         SlapReply               rs = { REP_RESULT };
859
860         if ( pb == NULL ) {
861                 return -1;
862         }
863
864         slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &lnewrdn );
865         slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR, &newsuperior );
866         slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN, &deloldrdn );
867         slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
868
869         rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_MODRDN, &conn );
870         if ( rs.sr_err != LDAP_SUCCESS ) {
871                 goto cleanup;
872         }
873
874         op = (Operation *)conn->c_pending_ops.stqh_first;
875
876         if ( op->o_req_dn.bv_len == 0 ) {
877                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
878                 goto cleanup;
879         }
880
881         newrdn.bv_val = lnewrdn;
882         newrdn.bv_len = strlen( lnewrdn );
883
884         rs.sr_err = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn, NULL );
885         if ( rs.sr_err != LDAP_SUCCESS ) {
886                 goto cleanup;
887         }
888
889         if ( rdn_validate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
890                 goto cleanup;
891         }
892
893         if ( newsuperior != NULL ) {
894                 newsupdn.bv_val = (char *)newsuperior;
895                 newsupdn.bv_len = strlen( newsuperior );
896
897                 rs.sr_err = dnPrettyNormal( NULL, &newsupdn, &newSuperiorPretty, &newSuperiorNormalized, NULL );
898                 if ( rs.sr_err != LDAP_SUCCESS )
899                         goto cleanup;
900
901                 op->oq_modrdn.rs_newSup = &newSuperiorPretty;
902                 op->oq_modrdn.rs_nnewSup = &newSuperiorNormalized;
903         } else {
904                 op->oq_modrdn.rs_newSup = NULL;
905                 op->oq_modrdn.rs_nnewSup = NULL;
906         }
907
908         op->oq_modrdn.rs_deleteoldrdn = deloldrdn;
909
910         if ( op->o_bd->be_modrdn != NULL ) {
911                 int repl_user = be_isupdate( op );
912                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
913                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
914
915                         if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
916                                 op->o_callback = &cb;
917
918                         op->o_bd->be_modrdn( op, &rs );
919                 } else {
920                         rs.sr_err = LDAP_REFERRAL;
921                 }
922         } else {
923                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
924         }
925
926 cleanup:
927         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
928
929         if ( op->oq_modrdn.rs_newrdn.bv_val != NULL )
930                 slapi_ch_free_string( &op->oq_modrdn.rs_newrdn.bv_val );
931         if ( op->oq_modrdn.rs_nnewrdn.bv_val != NULL )
932                 slapi_ch_free_string( &op->oq_modrdn.rs_nnewrdn.bv_val );
933         if ( newSuperiorPretty.bv_val != NULL )
934                 slapi_ch_free_string( &newSuperiorPretty.bv_val );
935         if ( newSuperiorNormalized.bv_val != NULL )
936                 slapi_ch_free_string( &newSuperiorNormalized.bv_val );
937
938         slapi_int_connection_destroy( &conn );
939
940         return 0;
941 #else
942         return -1;
943 #endif /* LDAP_SLAPI */
944 }
945
946 int slapi_modify_internal_pb( Slapi_PBlock *pb )
947 {
948 #ifdef LDAP_SLAPI
949         int                     i;
950         Connection              *conn = NULL;
951         Operation               *op = NULL;
952
953         struct berval dn = BER_BVNULL;
954
955         int                     manageDsaIt = SLAP_CONTROL_NONE;
956         int                     isCritical;
957         struct berval           *bv;
958         LDAPMod                 *pMod;
959
960         Modifications           *modlist = NULL;
961         Modifications           **modtail = &modlist;
962         Modifications           tmp;
963
964         LDAPMod                 **mods = NULL;
965         int                     operation_flags = 0;
966
967         SlapReply               rs = { REP_RESULT };
968
969         if ( pb == NULL ) {
970                 return -1;
971         }
972
973         slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
974         slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
975
976         if ( mods == NULL || mods[0] == NULL ) {
977                 rs.sr_err = LDAP_PARAM_ERROR ;
978                 goto cleanup;
979         }
980
981         rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_MODIFY, &conn );
982         if ( rs.sr_err != LDAP_SUCCESS ) {
983                 goto cleanup;
984         }
985
986         for ( i = 0, pMod = mods[0];
987                 rs.sr_err == LDAP_SUCCESS && pMod != NULL; 
988                 pMod = mods[++i] )
989         {
990                 Modifications *mod;
991
992                 if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
993                         /*
994                          * attr values are in berval format
995                          * convert an array of pointers to bervals
996                          * to an array of bervals
997                          */
998                         rs.sr_err = bvptr2obj_copy( pMod->mod_bvalues, &bv );
999                         if ( rs.sr_err != LDAP_SUCCESS )
1000                                 goto cleanup;
1001                         tmp.sml_type.bv_val = pMod->mod_type;
1002                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
1003                         tmp.sml_values = bv;
1004                         tmp.sml_nvalues = NULL;
1005
1006                         mod  = (Modifications *)ch_malloc( sizeof(Modifications) );
1007
1008                         mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
1009                         mod->sml_flags = 0;
1010                         mod->sml_next = NULL;
1011                         mod->sml_desc = NULL;
1012                         mod->sml_type = tmp.sml_type;
1013                         mod->sml_values = tmp.sml_values;
1014                         mod->sml_nvalues = tmp.sml_nvalues;
1015                 } else { 
1016                         rs.sr_err = values2obj_copy( pMod->mod_values, &bv );
1017                         if ( rs.sr_err != LDAP_SUCCESS )
1018                                 goto cleanup;
1019                         tmp.sml_type.bv_val = pMod->mod_type;
1020                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
1021                         tmp.sml_values = bv;
1022                         tmp.sml_nvalues = NULL;
1023
1024                         mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
1025
1026                         mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
1027                         mod->sml_flags = 0;
1028                         mod->sml_next = NULL;
1029                         mod->sml_desc = NULL;
1030                         mod->sml_type = tmp.sml_type;
1031                         mod->sml_values = tmp.sml_values;
1032                         mod->sml_nvalues = tmp.sml_nvalues;
1033                 }
1034                 *modtail = mod;
1035                 modtail = &mod->sml_next;
1036
1037                 switch( pMod->mod_op & LDAP_MOD_OP ) {
1038                 case LDAP_MOD_ADD:
1039                 if ( mod->sml_values == NULL ) {
1040                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1041                         goto cleanup;
1042                 }
1043
1044                 /* fall through */
1045                 case LDAP_MOD_DELETE:
1046                 case LDAP_MOD_REPLACE:
1047                 case LDAP_MOD_INCREMENT:
1048                 break;
1049
1050                 default:
1051                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1052                         goto cleanup;
1053                 }
1054         } 
1055         *modtail = NULL;
1056
1057         op = (Operation *)conn->c_pending_ops.stqh_first;
1058
1059         if ( op->o_req_ndn.bv_len == 0 ) {
1060                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1061                 goto cleanup;
1062         }
1063
1064         op->oq_modify.rs_modlist = modlist;
1065
1066         if ( op->o_bd->be_modify != NULL ) {
1067                 int repl_user = be_isupdate( op );
1068                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
1069                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
1070                         const char      *text = NULL;
1071                         char            textbuf[ SLAP_TEXT_BUFLEN ];
1072                         size_t          textlen = sizeof( textbuf );
1073                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
1074
1075                         rs.sr_err = slap_mods_check( modlist,
1076                                 &text, textbuf, textlen, NULL );
1077                         if ( rs.sr_err != LDAP_SUCCESS ) {
1078                                 goto cleanup;
1079                         }
1080
1081                         if ( !update ) {
1082                                 rs.sr_err = slap_mods_no_user_mod_check( op, modlist,
1083                                         &text, textbuf, textlen );
1084                                 if ( rs.sr_err != LDAP_SUCCESS ) {
1085                                         goto cleanup;
1086                                 }
1087                         }
1088
1089                         if ( !repl_user ) {
1090                                 rs.sr_err = slap_mods_opattrs( op, modlist,
1091                                                 modtail, &text, textbuf, 
1092                                                 textlen, 1 );
1093                                 if ( rs.sr_err != LDAP_SUCCESS ) {
1094                                         goto cleanup;
1095                                 }
1096                         }
1097
1098                         if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
1099                                 op->o_callback = &cb;
1100
1101                         op->o_bd->be_modify( op, &rs );
1102                 } else {
1103                         rs.sr_err = LDAP_REFERRAL;
1104                 }
1105         } else {
1106                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1107         }
1108
1109 cleanup:
1110         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1111
1112         if ( modlist != NULL )
1113                 slap_mods_free( modlist );
1114
1115         slapi_int_connection_destroy( &conn );
1116
1117         return 0;
1118 #else
1119         return -1;
1120 #endif /* LDAP_SLAPI */
1121 }
1122
1123 #ifdef LDAP_SLAPI
1124 static int
1125 slapi_int_search_entry_callback( Slapi_Entry *entry, void *callback_data )
1126 {
1127         int nentries = 0, i = 0;
1128         Slapi_Entry **head = NULL, **tp;
1129         Slapi_PBlock *pb = (Slapi_PBlock *)callback_data;
1130
1131         entry = slapi_entry_dup( entry );
1132         if ( entry == NULL ) {
1133                 return 1;
1134         }
1135
1136         slapi_pblock_get( pb, SLAPI_NENTRIES, &nentries );
1137         slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &head );
1138         
1139         i = nentries + 1;
1140         if ( nentries == 0 ) {
1141                 tp = (Slapi_Entry **)slapi_ch_malloc( 2 * sizeof(Slapi_Entry *) );
1142                 if ( tp == NULL ) {
1143                         slapi_entry_free( entry );
1144                         return 1;
1145                 }
1146
1147                 tp[ 0 ] = entry;
1148         } else {
1149                 tp = (Slapi_Entry **)slapi_ch_realloc( (char *)head,
1150                                 sizeof(Slapi_Entry *) * ( i + 1 ) );
1151                 if ( tp == NULL ) {
1152                         slapi_entry_free( entry );
1153                         return 1;
1154                 }
1155                 tp[ i - 1 ] = entry;
1156         }
1157         tp[ i ] = NULL;
1158                   
1159         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, (void *)tp );
1160         slapi_pblock_set( pb, SLAPI_NENTRIES, (void *)i );
1161
1162         return LDAP_SUCCESS;
1163 }
1164 #endif /* LDAP_SLAPI */
1165
1166 int slapi_search_internal_pb( Slapi_PBlock *pb )
1167 {
1168 #ifdef LDAP_SLAPI
1169         return slapi_search_internal_callback_pb( pb,
1170                 (void *)pb,
1171                 NULL,
1172                 slapi_int_search_entry_callback,
1173                 NULL );
1174 #else
1175         return -1;
1176 #endif
1177 }
1178
1179 int slapi_search_internal_callback_pb( Slapi_PBlock *pb,
1180         void *callback_data,
1181         plugin_result_callback prc,
1182         plugin_search_entry_callback psec,
1183         plugin_referral_entry_callback prec )
1184 {
1185 #ifdef LDAP_SLAPI
1186         Connection              *conn = NULL;
1187         Operation               *op = NULL;
1188         struct berval           dn = BER_BVNULL;
1189         Filter                  *filter = NULL;
1190         struct berval           fstr = BER_BVNULL;
1191         AttributeName           *an = NULL;
1192         const char              *text = NULL;
1193
1194         int                     manageDsaIt = SLAP_CONTROL_NONE;
1195         int                     isCritical;
1196         int                     i;
1197
1198         int                     scope = LDAP_SCOPE_BASE;
1199         char                    *filStr = NULL;
1200         LDAPControl             **controls = NULL;
1201         char                    **attrs = NULL;
1202         char                    *uniqueid = NULL;
1203         int                     attrsonly = 0;
1204         int                     operation_flags = 0;
1205         int                     freeFilter = 0;
1206
1207         SlapReply               rs = { REP_RESULT };
1208
1209         if ( pb == NULL ) {
1210                 return -1;
1211         }
1212
1213         slapi_pblock_get( pb, SLAPI_SEARCH_SCOPE, &scope );
1214         slapi_pblock_get( pb, SLAPI_SEARCH_FILTER, &filter );
1215         slapi_pblock_get( pb, SLAPI_SEARCH_STRFILTER, &filStr );
1216         slapi_pblock_get( pb, SLAPI_SEARCH_ATTRS, &attrs );
1217         slapi_pblock_get( pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly );
1218         slapi_pblock_get( pb, SLAPI_REQCONTROLS, &controls );
1219         slapi_pblock_get( pb, SLAPI_TARGET_UNIQUEID, &uniqueid );
1220         slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, &operation_flags );
1221
1222         rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_SEARCH, &conn );
1223         if ( rs.sr_err != LDAP_SUCCESS )
1224                 goto cleanup;
1225
1226         /* search callback and arguments */
1227         slapi_pblock_set( pb, SLAPI_X_INTOP_RESULT_CALLBACK, prc );
1228         slapi_pblock_set( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK, psec );
1229         slapi_pblock_set( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, prec );
1230         slapi_pblock_set( pb, SLAPI_X_INTOP_CALLBACK_DATA, callback_data );
1231
1232         op = (Operation *)conn->c_pending_ops.stqh_first;
1233
1234         switch ( scope ) {
1235                 case LDAP_SCOPE_BASE:
1236                 case LDAP_SCOPE_ONELEVEL:
1237                 case LDAP_SCOPE_SUBTREE:
1238 #ifdef LDAP_SCOPE_SUBORDINATE
1239                 case LDAP_SCOPE_SUBORDINATE:
1240 #endif
1241                         break;
1242                 default:
1243                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1244                         goto cleanup;
1245         }
1246
1247         if ( filter == NULL ) {
1248                 if ( filStr == NULL ) {
1249                         rs.sr_err = LDAP_PARAM_ERROR;
1250                         goto cleanup;
1251                 }
1252
1253                 filter = slapi_str2filter( filStr );
1254                 if ( filter == NULL ) {
1255                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1256                         goto cleanup;
1257                 }
1258
1259                 freeFilter = 1;
1260         }
1261
1262         filter2bv( filter, &fstr );
1263
1264         for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
1265                 ; /* count the number of attributes */
1266         }
1267
1268         if ( i > 0 ) {
1269                 an = (AttributeName *)slapi_ch_calloc( (i + 1), sizeof(AttributeName) );
1270                 for (i = 0; attrs[i] != 0; i++) {
1271                         an[i].an_desc = NULL;
1272                         an[i].an_oc = NULL;
1273                         an[i].an_oc_exclude = 0;
1274                         an[i].an_name.bv_val = slapi_ch_strdup(attrs[i]);
1275                         an[i].an_name.bv_len = strlen(attrs[i]);
1276                         slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1277                 }
1278                 an[i].an_name.bv_val = NULL;
1279         }
1280
1281         rs.sr_type = REP_RESULT;
1282         rs.sr_err = LDAP_SUCCESS;
1283         rs.sr_entry = NULL; /* paranoia */
1284
1285         if ( scope == LDAP_SCOPE_BASE ) {
1286                 rs.sr_entry = NULL;
1287
1288                 if ( op->o_req_ndn.bv_len == 0 ) {
1289                         rs.sr_err = root_dse_info( conn, &rs.sr_entry, &rs.sr_text );
1290                 }
1291
1292                 if( rs.sr_err != LDAP_SUCCESS ) {
1293                         send_ldap_result( op, &rs );
1294                         goto cleanup;
1295                 } else if ( rs.sr_entry != NULL ) {
1296                         rs.sr_err = test_filter( op, rs.sr_entry, filter );
1297
1298                         if ( rs.sr_err == LDAP_COMPARE_TRUE ) {
1299                                 rs.sr_type = REP_SEARCH;
1300                                 rs.sr_err = LDAP_SUCCESS;
1301                                 rs.sr_attrs = an;
1302                                 rs.sr_operational_attrs = NULL;
1303                                 rs.sr_flags = REP_ENTRY_MODIFIABLE;
1304
1305                                 send_search_entry( op, &rs );
1306                         }
1307
1308                         entry_free( rs.sr_entry );
1309
1310                         rs.sr_type = REP_RESULT;
1311                         rs.sr_err = LDAP_SUCCESS;
1312
1313                         send_ldap_result( op, &rs );
1314
1315                         goto cleanup;
1316                 }
1317         }
1318
1319         op->oq_search.rs_scope = scope;
1320         op->oq_search.rs_deref = 0;
1321         op->oq_search.rs_slimit = SLAP_NO_LIMIT;
1322         op->oq_search.rs_tlimit = SLAP_NO_LIMIT;
1323         op->oq_search.rs_attrsonly = attrsonly;
1324         op->oq_search.rs_attrs = an;
1325         op->oq_search.rs_filter = filter;
1326         op->oq_search.rs_filterstr = fstr;
1327
1328         if ( BER_BVISEMPTY( &op->o_req_ndn ) &&
1329              !BER_BVISEMPTY( &default_search_nbase ) ) {
1330                 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
1331                 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
1332
1333                 ber_dupbv( &op->o_req_dn, &default_search_base );
1334                 ber_dupbv( &op->o_req_ndn, &default_search_nbase );
1335
1336                 rs.sr_err = slapi_int_pblock_get_backend( pb, op );
1337                 if ( rs.sr_err != LDAP_SUCCESS )
1338                         goto cleanup;
1339         }
1340
1341         if ( op->o_bd->be_search != NULL ) {
1342                 (*op->o_bd->be_search)( op, &rs );
1343         } else {
1344                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1345         }
1346
1347 cleanup:
1348         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1349         slapi_pblock_set( pb, SLAPI_X_INTOP_RESULT_CALLBACK, NULL );
1350         slapi_pblock_set( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK, NULL );
1351         slapi_pblock_set( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, NULL );
1352         slapi_pblock_set( pb, SLAPI_X_INTOP_CALLBACK_DATA, NULL );
1353
1354         if ( freeFilter && filter != NULL )
1355                 slapi_filter_free( filter, 1 );
1356         if ( fstr.bv_val )
1357                 slapi_ch_free( (void **)&fstr.bv_val );
1358         if ( an != NULL )
1359                 slapi_ch_free( (void **)&an );
1360
1361         slapi_int_connection_destroy( &conn );
1362
1363         return 0;
1364 #else
1365         return -1;
1366 #endif /* LDAP_SLAPI */
1367 }
1368
1369 /* Wrappers for old API */
1370
1371 void slapi_search_internal_set_pb( Slapi_PBlock *pb,
1372         const char *base,
1373         int scope,
1374         const char *filter,
1375         char **attrs,
1376         int attrsonly,
1377         LDAPControl **controls,
1378         const char *uniqueid,
1379         Slapi_ComponentId *plugin_identity,
1380         int operation_flags )
1381 {
1382 #ifdef LDAP_SLAPI
1383         slapi_pblock_set( pb, SLAPI_SEARCH_TARGET, (void *)base );
1384         slapi_pblock_set( pb, SLAPI_SEARCH_SCOPE, (void *)scope );
1385         slapi_pblock_set( pb, SLAPI_SEARCH_FILTER, NULL );
1386         slapi_pblock_set( pb, SLAPI_SEARCH_STRFILTER, (void *)filter );
1387         slapi_pblock_set( pb, SLAPI_SEARCH_ATTRS, (void *)attrs );
1388         slapi_pblock_set( pb, SLAPI_SEARCH_ATTRSONLY, (void *)attrsonly );
1389         slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
1390         slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
1391         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1392         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
1393 #endif /* LDAP_SLAPI */
1394 }
1395
1396 Slapi_PBlock *
1397 slapi_search_internal(
1398         char *ldn, 
1399         int scope, 
1400         char *filStr, 
1401         LDAPControl **controls, 
1402         char **attrs, 
1403         int attrsonly ) 
1404 {
1405 #ifdef LDAP_SLAPI
1406         Slapi_PBlock *pb;
1407
1408         pb = slapi_pblock_new();
1409         if ( pb == NULL ) {
1410                 return NULL;
1411         }
1412
1413         slapi_search_internal_set_pb( pb, ldn, scope, filStr, attrs, attrsonly,
1414                 controls, NULL, NULL, 0 );
1415
1416         slapi_search_internal_pb( pb );
1417
1418         return pb;
1419 #else
1420         return NULL;
1421 #endif /* LDAP_SLAPI */
1422 }
1423
1424 void slapi_modify_internal_set_pb( Slapi_PBlock *pb,
1425         const char *dn,
1426         LDAPMod **mods,
1427         LDAPControl **controls,
1428         const char *uniqueid,
1429         Slapi_ComponentId *plugin_identity,
1430         int operation_flags )
1431 {
1432 #ifdef LDAP_SLAPI
1433         slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn );
1434         slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)mods );
1435         slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
1436         slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
1437         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1438         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
1439 #endif /* LDAP_SLAPI */
1440 }
1441
1442 /* Function : slapi_modify_internal
1443  *
1444  * Description: Plugin functions call this routine to modify an entry 
1445  *                              in the backend directly
1446  * Return values : LDAP_SUCCESS
1447  *                 LDAP_PARAM_ERROR
1448  *                 LDAP_NO_MEMORY
1449  *                 LDAP_OTHER
1450  *                 LDAP_UNWILLING_TO_PERFORM
1451 */
1452 Slapi_PBlock *
1453 slapi_modify_internal(
1454         char *ldn,      
1455         LDAPMod **mods, 
1456         LDAPControl **controls, 
1457         int log_change )
1458 {
1459 #ifdef LDAP_SLAPI
1460         Slapi_PBlock *pb;
1461
1462         pb = slapi_pblock_new();
1463         if ( pb == NULL ) {
1464                 return NULL;
1465         }
1466
1467         slapi_modify_internal_set_pb( pb, ldn, mods, controls, NULL, NULL,
1468                 log_change ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1469
1470         slapi_modify_internal_pb( pb );
1471
1472         return pb;
1473 #else
1474         return NULL;
1475 #endif /* LDAP_SLAPI */
1476 }
1477
1478 int slapi_add_internal_set_pb( Slapi_PBlock *pb,
1479         const char *dn,
1480         LDAPMod **attrs,
1481         LDAPControl **controls,
1482         Slapi_ComponentId *plugin_identity,
1483         int operation_flags )
1484 {
1485 #ifdef LDAP_SLAPI
1486         slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn );
1487         slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)attrs );
1488         slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
1489         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1490         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
1491
1492         return 0;
1493 #else
1494         return -1;
1495 #endif /* LDAP_SLAPI */
1496 }
1497
1498 Slapi_PBlock *slapi_add_internal(
1499         char * dn,
1500         LDAPMod **attrs,
1501         LDAPControl **controls,
1502         int log_changes )
1503 {
1504 #ifdef LDAP_SLAPI
1505         Slapi_PBlock *pb;
1506
1507         pb = slapi_pblock_new();
1508         if ( pb == NULL )
1509                 return NULL;
1510
1511         slapi_add_internal_set_pb( pb, dn, attrs, controls, NULL,
1512                 log_changes ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1513         
1514         slapi_add_internal_pb( pb );
1515
1516         return pb;
1517 #else
1518         return NULL;
1519 #endif /* LDAP_SLAPI */
1520 }
1521
1522 void slapi_add_entry_internal_set_pb( Slapi_PBlock *pb,
1523         Slapi_Entry *e,
1524         LDAPControl **controls,
1525         Slapi_ComponentId *plugin_identity,
1526         int operation_flags )
1527 {
1528 #ifdef LDAP_SLAPI
1529         slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
1530         slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
1531         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1532         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
1533 #endif /* LDAP_SLAPI */
1534 }
1535
1536 Slapi_PBlock * 
1537 slapi_add_entry_internal(
1538         Slapi_Entry *e, 
1539         LDAPControl **controls, 
1540         int log_changes )
1541 {
1542 #ifdef LDAP_SLAPI
1543         Slapi_PBlock *pb;
1544
1545         pb = slapi_pblock_new();
1546         if ( pb == NULL )
1547                 return NULL;
1548
1549         slapi_add_entry_internal_set_pb( pb, e, controls, NULL,
1550                 log_changes ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1551         
1552         slapi_add_internal_pb( pb );
1553
1554         return pb;
1555 #else
1556         return NULL;
1557 #endif /* LDAP_SLAPI */
1558 }
1559
1560 void slapi_rename_internal_set_pb( Slapi_PBlock *pb,
1561         const char *olddn,
1562         const char *newrdn,
1563         const char *newsuperior,
1564         int deloldrdn,
1565         LDAPControl **controls,
1566         const char *uniqueid,
1567         Slapi_ComponentId *plugin_identity,
1568         int operation_flags )
1569 {
1570 #ifdef LDAP_SLAPI
1571         slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)olddn );
1572         slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn );
1573         slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR, (void *)newsuperior );
1574         slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
1575         slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
1576         slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
1577         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1578         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
1579 #endif /* LDAP_SLAPI */
1580 }
1581
1582 /* Function : slapi_modrdn_internal
1583  *
1584  * Description : Plugin functions call this routine to modify the rdn 
1585  *                               of an entry in the backend directly
1586  * Return values : LDAP_SUCCESS
1587  *                 LDAP_PARAM_ERROR
1588  *                 LDAP_NO_MEMORY
1589  *                 LDAP_OTHER
1590  *                 LDAP_UNWILLING_TO_PERFORM
1591  *
1592  * NOTE: This function does not support the "newSuperior" option from LDAP V3.
1593  */
1594 Slapi_PBlock *
1595 slapi_modrdn_internal(
1596         char *olddn, 
1597         char *lnewrdn, 
1598         int deloldrdn, 
1599         LDAPControl **controls, 
1600         int log_change )
1601 {
1602 #ifdef LDAP_SLAPI
1603         Slapi_PBlock *pb;
1604
1605         pb = slapi_pblock_new();
1606         if ( pb == NULL ) {
1607                 return NULL;
1608         }
1609
1610         slapi_rename_internal_set_pb( pb, olddn, lnewrdn, NULL,
1611                 deloldrdn, controls, NULL, NULL,
1612                 log_change ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1613
1614         slapi_modrdn_internal_pb( pb );
1615
1616         return pb;
1617 #else
1618         return NULL;
1619 #endif /* LDAP_SLAPI */
1620 }
1621
1622 void slapi_delete_internal_set_pb( Slapi_PBlock *pb,
1623         const char *dn,
1624         LDAPControl **controls,
1625         const char *uniqueid,
1626         Slapi_ComponentId *plugin_identity,
1627         int operation_flags )
1628 {
1629 #ifdef LDAP_SLAPI
1630         slapi_pblock_set( pb, SLAPI_TARGET_DN, (void *)dn );
1631         slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)controls );
1632         slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
1633         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1634         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS, (void *)operation_flags );
1635 #endif /* LDAP_SLAPI */
1636 }
1637
1638 /* Function : slapi_delete_internal
1639  *
1640  * Description : Plugin functions call this routine to delete an entry 
1641  *               in the backend directly
1642  * Return values : LDAP_SUCCESS
1643  *                 LDAP_PARAM_ERROR
1644  *                 LDAP_NO_MEMORY
1645  *                 LDAP_OTHER
1646  *                 LDAP_UNWILLING_TO_PERFORM
1647 */
1648 Slapi_PBlock *
1649 slapi_delete_internal(
1650         char *ldn, 
1651         LDAPControl **controls, 
1652         int log_change )
1653 {
1654 #ifdef LDAP_SLAPI
1655         Slapi_PBlock *pb;
1656
1657         pb = slapi_pblock_new();
1658         if ( pb == NULL )
1659                 return NULL;
1660
1661         slapi_delete_internal_set_pb( pb, ldn, controls, NULL, NULL,
1662                 log_change ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1663
1664         slapi_delete_internal_pb( pb );
1665
1666         return pb;
1667 #else
1668         return NULL;
1669 #endif /* LDAP_SLAPI */
1670 }
1671