]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_ops.c
SLAPI plugins are no longer global; global SLAPI plugins should be
[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, (void **)&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, (void **)&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,     (void **)&argEntry );
751         slapi_pblock_get( pb, SLAPI_ADD_TARGET,    (void **)&dn );
752         slapi_pblock_get( pb, SLAPI_MODIFY_MODS,   (void **)&mods );
753         slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, (void **)&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
778         if ( argEntry != NULL ) {
779                 entry = slapi_entry_dup( argEntry );
780         } else {
781                 if ( mods == NULL || dn == NULL ) {
782                         rs.sr_err = LDAP_PARAM_ERROR;
783                         goto cleanup;
784                 }
785
786                 for ( i = 0; mods[i] != NULL; i++ ) {
787                         if ( (mods[i]->mod_op & LDAP_MOD_OP ) != LDAP_MOD_ADD ) {
788                                 rs.sr_err = LDAP_PARAM_ERROR;
789                                 goto cleanup;
790                         }
791                 }
792
793                 entry = slapi_int_ldapmod_to_entry( conn, dn, mods );
794                 if ( entry == NULL ) {
795                         rs.sr_err = LDAP_OTHER;
796                         goto cleanup;
797                 }
798         }
799
800         op = (Operation *)conn->c_pending_ops.stqh_first;
801         op->oq_add.rs_e = entry;
802
803         if ( op->o_bd->be_add != NULL ) {
804                 int repl_user = be_isupdate( op );
805                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
806                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
807
808                         if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
809                                 op->o_callback = &cb;
810
811                         if ( op->o_bd->be_add( op, &rs ) == LDAP_SUCCESS ) {
812                                 be_entry_release_w( op, entry );
813                                 entry = NULL;
814                         }
815                 } else {
816                         rs.sr_err = LDAP_REFERRAL;
817                 }
818         } else {
819                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
820         }
821
822 cleanup:
823         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
824
825         if ( entry != NULL ) {
826                 slapi_entry_free( entry );
827         }
828         if ( argEntry != NULL ) {
829                 slapi_pblock_set( pb, SLAPI_ADD_TARGET, NULL );
830         }
831
832         slapi_int_connection_destroy( &conn );
833
834         return 0;
835 #else
836         return -1;
837 #endif /* LDAP_SLAPI */
838 }
839
840 int
841 slapi_modrdn_internal_pb( Slapi_PBlock *pb )
842 {
843 #ifdef LDAP_SLAPI
844         struct berval           dn = BER_BVNULL;
845         struct berval           newrdn = BER_BVNULL;
846         struct berval           newsupdn = BER_BVNULL;
847         struct berval           newSuperiorPretty = BER_BVNULL;
848         struct berval           newSuperiorNormalized = BER_BVNULL;
849         Connection              *conn = NULL;
850         Operation               *op = NULL;
851         int                     manageDsaIt = SLAP_CONTROL_NONE;
852         int                     isCritical;
853
854         char                    *lnewrdn;
855         char                    *newsuperior;
856         int                     deloldrdn;
857         int                     operation_flags;
858
859         SlapReply               rs = { REP_RESULT };
860
861         if ( pb == NULL ) {
862                 return -1;
863         }
864
865         slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN,      (void **)&lnewrdn );
866         slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR, (void **)&newsuperior );
867         slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN,   (void **)&deloldrdn );
868         slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS,      (void **)&operation_flags );
869
870         rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_MODRDN, &conn );
871         if ( rs.sr_err != LDAP_SUCCESS ) {
872                 goto cleanup;
873         }
874
875         op = (Operation *)conn->c_pending_ops.stqh_first;
876
877         if ( op->o_req_dn.bv_len == 0 ) {
878                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
879                 goto cleanup;
880         }
881
882         newrdn.bv_val = lnewrdn;
883         newrdn.bv_len = strlen( lnewrdn );
884
885         rs.sr_err = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn, NULL );
886         if ( rs.sr_err != LDAP_SUCCESS ) {
887                 goto cleanup;
888         }
889
890         if ( rdn_validate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
891                 goto cleanup;
892         }
893
894         if ( newsuperior != NULL ) {
895                 newsupdn.bv_val = (char *)newsuperior;
896                 newsupdn.bv_len = strlen( newsuperior );
897
898                 rs.sr_err = dnPrettyNormal( NULL, &newsupdn, &newSuperiorPretty, &newSuperiorNormalized, NULL );
899                 if ( rs.sr_err != LDAP_SUCCESS )
900                         goto cleanup;
901
902                 op->oq_modrdn.rs_newSup = &newSuperiorPretty;
903                 op->oq_modrdn.rs_nnewSup = &newSuperiorNormalized;
904         } else {
905                 op->oq_modrdn.rs_newSup = NULL;
906                 op->oq_modrdn.rs_nnewSup = NULL;
907         }
908
909         op->oq_modrdn.rs_deleteoldrdn = deloldrdn;
910
911         if ( op->o_bd->be_modrdn != NULL ) {
912                 int repl_user = be_isupdate( op );
913                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
914                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
915
916                         if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
917                                 op->o_callback = &cb;
918
919                         op->o_bd->be_modrdn( op, &rs );
920                 } else {
921                         rs.sr_err = LDAP_REFERRAL;
922                 }
923         } else {
924                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
925         }
926
927 cleanup:
928         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
929
930         if ( op->oq_modrdn.rs_newrdn.bv_val != NULL )
931                 slapi_ch_free_string( &op->oq_modrdn.rs_newrdn.bv_val );
932         if ( op->oq_modrdn.rs_nnewrdn.bv_val != NULL )
933                 slapi_ch_free_string( &op->oq_modrdn.rs_nnewrdn.bv_val );
934         if ( newSuperiorPretty.bv_val != NULL )
935                 slapi_ch_free_string( &newSuperiorPretty.bv_val );
936         if ( newSuperiorNormalized.bv_val != NULL )
937                 slapi_ch_free_string( &newSuperiorNormalized.bv_val );
938
939         slapi_int_connection_destroy( &conn );
940
941         return 0;
942 #else
943         return -1;
944 #endif /* LDAP_SLAPI */
945 }
946
947 int slapi_modify_internal_pb( Slapi_PBlock *pb )
948 {
949 #ifdef LDAP_SLAPI
950         int                     i;
951         Connection              *conn = NULL;
952         Operation               *op = NULL;
953
954         struct berval dn = BER_BVNULL;
955
956         int                     manageDsaIt = SLAP_CONTROL_NONE;
957         int                     isCritical;
958         struct berval           *bv;
959         LDAPMod                 *pMod;
960
961         Modifications           *modlist = NULL;
962         Modifications           **modtail = &modlist;
963         Modifications           tmp;
964
965         LDAPMod                 **mods = NULL;
966         int                     operation_flags = 0;
967
968         SlapReply               rs = { REP_RESULT };
969
970         if ( pb == NULL ) {
971                 return -1;
972         }
973
974         slapi_pblock_get( pb, SLAPI_MODIFY_MODS,   (void **)&mods );
975         slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS, (void **)&operation_flags );
976
977         if ( mods == NULL || mods[0] == NULL ) {
978                 rs.sr_err = LDAP_PARAM_ERROR ;
979                 goto cleanup;
980         }
981
982         rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_MODIFY, &conn );
983         if ( rs.sr_err != LDAP_SUCCESS ) {
984                 goto cleanup;
985         }
986
987         for ( i = 0, pMod = mods[0];
988                 rs.sr_err == LDAP_SUCCESS && pMod != NULL; 
989                 pMod = mods[++i] )
990         {
991                 Modifications *mod;
992
993                 if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
994                         /*
995                          * attr values are in berval format
996                          * convert an array of pointers to bervals
997                          * to an array of bervals
998                          */
999                         rs.sr_err = bvptr2obj_copy( pMod->mod_bvalues, &bv );
1000                         if ( rs.sr_err != LDAP_SUCCESS )
1001                                 goto cleanup;
1002                         tmp.sml_type.bv_val = pMod->mod_type;
1003                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
1004                         tmp.sml_values = bv;
1005                         tmp.sml_nvalues = NULL;
1006
1007                         mod  = (Modifications *)ch_malloc( sizeof(Modifications) );
1008
1009                         mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
1010                         mod->sml_flags = 0;
1011                         mod->sml_next = NULL;
1012                         mod->sml_desc = NULL;
1013                         mod->sml_type = tmp.sml_type;
1014                         mod->sml_values = tmp.sml_values;
1015                         mod->sml_nvalues = tmp.sml_nvalues;
1016                 } else { 
1017                         rs.sr_err = values2obj_copy( pMod->mod_values, &bv );
1018                         if ( rs.sr_err != LDAP_SUCCESS )
1019                                 goto cleanup;
1020                         tmp.sml_type.bv_val = pMod->mod_type;
1021                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
1022                         tmp.sml_values = bv;
1023                         tmp.sml_nvalues = NULL;
1024
1025                         mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
1026
1027                         mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
1028                         mod->sml_flags = 0;
1029                         mod->sml_next = NULL;
1030                         mod->sml_desc = NULL;
1031                         mod->sml_type = tmp.sml_type;
1032                         mod->sml_values = tmp.sml_values;
1033                         mod->sml_nvalues = tmp.sml_nvalues;
1034                 }
1035                 *modtail = mod;
1036                 modtail = &mod->sml_next;
1037
1038                 switch( pMod->mod_op & LDAP_MOD_OP ) {
1039                 case LDAP_MOD_ADD:
1040                 if ( mod->sml_values == NULL ) {
1041                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1042                         goto cleanup;
1043                 }
1044
1045                 /* fall through */
1046                 case LDAP_MOD_DELETE:
1047                 case LDAP_MOD_REPLACE:
1048                 case LDAP_MOD_INCREMENT:
1049                 break;
1050
1051                 default:
1052                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1053                         goto cleanup;
1054                 }
1055         } 
1056         *modtail = NULL;
1057
1058         op = (Operation *)conn->c_pending_ops.stqh_first;
1059
1060         if ( op->o_req_ndn.bv_len == 0 ) {
1061                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1062                 goto cleanup;
1063         }
1064
1065         op->oq_modify.rs_modlist = modlist;
1066
1067         if ( op->o_bd->be_modify != NULL ) {
1068                 int repl_user = be_isupdate( op );
1069                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
1070                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
1071                         const char      *text = NULL;
1072                         char            textbuf[ SLAP_TEXT_BUFLEN ];
1073                         size_t          textlen = sizeof( textbuf );
1074                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
1075
1076                         rs.sr_err = slap_mods_check( modlist,
1077                                 &text, textbuf, textlen, NULL );
1078                         if ( rs.sr_err != LDAP_SUCCESS ) {
1079                                 goto cleanup;
1080                         }
1081
1082                         if ( !update ) {
1083                                 rs.sr_err = slap_mods_no_user_mod_check( op, modlist,
1084                                         &text, textbuf, textlen );
1085                                 if ( rs.sr_err != LDAP_SUCCESS ) {
1086                                         goto cleanup;
1087                                 }
1088                         }
1089
1090                         if ( !repl_user ) {
1091                                 rs.sr_err = slap_mods_opattrs( op, modlist,
1092                                                 modtail, &text, textbuf, 
1093                                                 textlen, 1 );
1094                                 if ( rs.sr_err != LDAP_SUCCESS ) {
1095                                         goto cleanup;
1096                                 }
1097                         }
1098
1099                         if ( operation_flags & SLAPI_OP_FLAG_LOG_CHANGE )
1100                                 op->o_callback = &cb;
1101
1102                         op->o_bd->be_modify( op, &rs );
1103                 } else {
1104                         rs.sr_err = LDAP_REFERRAL;
1105                 }
1106         } else {
1107                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1108         }
1109
1110 cleanup:
1111         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1112
1113         if ( modlist != NULL )
1114                 slap_mods_free( modlist );
1115
1116         slapi_int_connection_destroy( &conn );
1117
1118         return 0;
1119 #else
1120         return -1;
1121 #endif /* LDAP_SLAPI */
1122 }
1123
1124 #ifdef LDAP_SLAPI
1125 static int
1126 slapi_int_search_entry_callback( Slapi_Entry *entry, void *callback_data )
1127 {
1128         int nentries = 0, i = 0;
1129         Slapi_Entry **head = NULL, **tp;
1130         Slapi_PBlock *pb = (Slapi_PBlock *)callback_data;
1131
1132         entry = slapi_entry_dup( entry );
1133         if ( entry == NULL ) {
1134                 return 1;
1135         }
1136
1137         slapi_pblock_get( pb, SLAPI_NENTRIES, &nentries );
1138         slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &head );
1139         
1140         i = nentries + 1;
1141         if ( nentries == 0 ) {
1142                 tp = (Slapi_Entry **)slapi_ch_malloc( 2 * sizeof(Slapi_Entry *) );
1143                 if ( tp == NULL ) {
1144                         slapi_entry_free( entry );
1145                         return 1;
1146                 }
1147
1148                 tp[ 0 ] = entry;
1149         } else {
1150                 tp = (Slapi_Entry **)slapi_ch_realloc( (char *)head,
1151                                 sizeof(Slapi_Entry *) * ( i + 1 ) );
1152                 if ( tp == NULL ) {
1153                         slapi_entry_free( entry );
1154                         return 1;
1155                 }
1156                 tp[ i - 1 ] = entry;
1157         }
1158         tp[ i ] = NULL;
1159                   
1160         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, (void *)tp );
1161         slapi_pblock_set( pb, SLAPI_NENTRIES, (void *)i );
1162
1163         return LDAP_SUCCESS;
1164 }
1165 #endif /* LDAP_SLAPI */
1166
1167 int slapi_search_internal_pb( Slapi_PBlock *pb )
1168 {
1169 #ifdef LDAP_SLAPI
1170         return slapi_search_internal_callback_pb( pb,
1171                 (void *)pb,
1172                 NULL,
1173                 slapi_int_search_entry_callback,
1174                 NULL );
1175 #else
1176         return -1;
1177 #endif
1178 }
1179
1180 int slapi_search_internal_callback_pb( Slapi_PBlock *pb,
1181         void *callback_data,
1182         plugin_result_callback prc,
1183         plugin_search_entry_callback psec,
1184         plugin_referral_entry_callback prec )
1185 {
1186 #ifdef LDAP_SLAPI
1187         Connection              *conn = NULL;
1188         Operation               *op = NULL;
1189         struct berval           dn = BER_BVNULL;
1190         Filter                  *filter = NULL;
1191         struct berval           fstr = BER_BVNULL;
1192         AttributeName           *an = NULL;
1193         const char              *text = NULL;
1194
1195         int                     manageDsaIt = SLAP_CONTROL_NONE;
1196         int                     isCritical;
1197         int                     i;
1198
1199         int                     scope = LDAP_SCOPE_BASE;
1200         char                    *filStr = NULL;
1201         LDAPControl             **controls = NULL;
1202         char                    **attrs = NULL;
1203         char                    *uniqueid = NULL;
1204         int                     attrsonly = 0;
1205         int                     operation_flags = 0;
1206         int                     freeFilter = 0;
1207
1208         SlapReply               rs = { REP_RESULT };
1209
1210         if ( pb == NULL ) {
1211                 return -1;
1212         }
1213
1214         slapi_pblock_get( pb, SLAPI_SEARCH_SCOPE,     (void **)&scope );
1215         slapi_pblock_get( pb, SLAPI_SEARCH_FILTER,    (void **)&filter );
1216         slapi_pblock_get( pb, SLAPI_SEARCH_STRFILTER, (void **)&filStr );
1217         slapi_pblock_get( pb, SLAPI_SEARCH_ATTRS,     (void **)&attrs );
1218         slapi_pblock_get( pb, SLAPI_SEARCH_ATTRSONLY, (void **)&attrsonly );
1219         slapi_pblock_get( pb, SLAPI_REQCONTROLS,      (void **)&controls );
1220         slapi_pblock_get( pb, SLAPI_TARGET_UNIQUEID,  (void **)&uniqueid );
1221         slapi_pblock_get( pb, SLAPI_X_INTOP_FLAGS,    (void **)&operation_flags );
1222
1223         rs.sr_err = slapi_int_init_connection( pb, &rs, LDAP_REQ_SEARCH, &conn );
1224         if ( rs.sr_err != LDAP_SUCCESS ) {
1225                 goto cleanup;
1226         }
1227
1228         /* search callback and arguments */
1229         slapi_pblock_set( pb, SLAPI_X_INTOP_RESULT_CALLBACK,         (void *)prc );
1230         slapi_pblock_set( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK,   (void *)psec );
1231         slapi_pblock_set( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, (void *)prec );
1232         slapi_pblock_set( pb, SLAPI_X_INTOP_CALLBACK_DATA,           (void *)callback_data );
1233
1234         op = (Operation *)conn->c_pending_ops.stqh_first;
1235
1236         switch ( scope ) {
1237                 case LDAP_SCOPE_BASE:
1238                 case LDAP_SCOPE_ONELEVEL:
1239                 case LDAP_SCOPE_SUBTREE:
1240 #ifdef LDAP_SCOPE_SUBORDINATE
1241                 case LDAP_SCOPE_SUBORDINATE:
1242 #endif
1243                         break;
1244                 default:
1245                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1246                         goto cleanup;
1247         }
1248
1249         if ( filter == NULL ) {
1250                 if ( filStr == NULL ) {
1251                         rs.sr_err = LDAP_PARAM_ERROR;
1252                         goto cleanup;
1253                 }
1254
1255                 filter = slapi_str2filter( filStr );
1256                 if ( filter == NULL ) {
1257                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1258                         goto cleanup;
1259                 }
1260
1261                 freeFilter = 1;
1262         }
1263
1264         filter2bv( filter, &fstr );
1265
1266         for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
1267                 ; /* count the number of attributes */
1268         }
1269
1270         if ( i > 0 ) {
1271                 an = (AttributeName *)slapi_ch_calloc( (i + 1), sizeof(AttributeName) );
1272                 for (i = 0; attrs[i] != 0; i++) {
1273                         an[i].an_desc = NULL;
1274                         an[i].an_oc = NULL;
1275                         an[i].an_oc_exclude = 0;
1276                         an[i].an_name.bv_val = slapi_ch_strdup(attrs[i]);
1277                         an[i].an_name.bv_len = strlen(attrs[i]);
1278                         slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1279                 }
1280                 an[i].an_name.bv_val = NULL;
1281         }
1282
1283         rs.sr_type = REP_RESULT;
1284         rs.sr_err = LDAP_SUCCESS;
1285         rs.sr_entry = NULL; /* paranoia */
1286
1287         if ( scope == LDAP_SCOPE_BASE ) {
1288                 rs.sr_entry = NULL;
1289
1290                 if ( op->o_req_ndn.bv_len == 0 ) {
1291                         rs.sr_err = root_dse_info( conn, &rs.sr_entry, &rs.sr_text );
1292                 }
1293
1294                 if( rs.sr_err != LDAP_SUCCESS ) {
1295                         send_ldap_result( op, &rs );
1296                         goto cleanup;
1297                 } else if ( rs.sr_entry != NULL ) {
1298                         rs.sr_err = test_filter( op, rs.sr_entry, filter );
1299
1300                         if ( rs.sr_err == LDAP_COMPARE_TRUE ) {
1301                                 rs.sr_type = REP_SEARCH;
1302                                 rs.sr_err = LDAP_SUCCESS;
1303                                 rs.sr_attrs = an;
1304                                 rs.sr_operational_attrs = NULL;
1305                                 rs.sr_flags = REP_ENTRY_MODIFIABLE;
1306
1307                                 send_search_entry( op, &rs );
1308                         }
1309
1310                         entry_free( rs.sr_entry );
1311
1312                         rs.sr_type = REP_RESULT;
1313                         rs.sr_err = LDAP_SUCCESS;
1314
1315                         send_ldap_result( op, &rs );
1316
1317                         goto cleanup;
1318                 }
1319         }
1320
1321         op->oq_search.rs_scope = scope;
1322         op->oq_search.rs_deref = 0;
1323         op->oq_search.rs_slimit = SLAP_NO_LIMIT;
1324         op->oq_search.rs_tlimit = SLAP_NO_LIMIT;
1325         op->oq_search.rs_attrsonly = attrsonly;
1326         op->oq_search.rs_attrs = an;
1327         op->oq_search.rs_filter = filter;
1328         op->oq_search.rs_filterstr = fstr;
1329
1330         if ( BER_BVISEMPTY( &op->o_req_ndn ) &&
1331              !BER_BVISEMPTY( &default_search_nbase ) ) {
1332                 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
1333                 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
1334
1335                 ber_dupbv( &op->o_req_dn, &default_search_base );
1336                 ber_dupbv( &op->o_req_ndn, &default_search_nbase );
1337
1338                 rs.sr_err = slapi_int_pblock_get_backend( pb, op );
1339                 if ( rs.sr_err != LDAP_SUCCESS )
1340                         goto cleanup;
1341         }
1342
1343         if ( op->o_bd->be_search != NULL ) {
1344                 (*op->o_bd->be_search)( op, &rs );
1345         } else {
1346                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1347         }
1348
1349 cleanup:
1350         slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT,            (void *)rs.sr_err );
1351         slapi_pblock_set( pb, SLAPI_X_INTOP_RESULT_CALLBACK,         NULL );
1352         slapi_pblock_set( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK,   NULL );
1353         slapi_pblock_set( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, NULL );
1354         slapi_pblock_set( pb, SLAPI_X_INTOP_CALLBACK_DATA,           NULL );
1355
1356         if ( freeFilter && filter != NULL )
1357                 slapi_filter_free( filter, 1 );
1358         if ( fstr.bv_val )
1359                 slapi_ch_free( (void **)&fstr.bv_val );
1360         if ( an != NULL )
1361                 slapi_ch_free( (void **)&an );
1362
1363         slapi_int_connection_destroy( &conn );
1364
1365         return 0;
1366 #else
1367         return -1;
1368 #endif /* LDAP_SLAPI */
1369 }
1370
1371 /* Wrappers for old API */
1372
1373 void slapi_search_internal_set_pb( Slapi_PBlock *pb,
1374         const char *base,
1375         int scope,
1376         const char *filter,
1377         char **attrs,
1378         int attrsonly,
1379         LDAPControl **controls,
1380         const char *uniqueid,
1381         Slapi_ComponentId *plugin_identity,
1382         int operation_flags )
1383 {
1384 #ifdef LDAP_SLAPI
1385         slapi_pblock_set( pb, SLAPI_SEARCH_TARGET,    (void *)base );
1386         slapi_pblock_set( pb, SLAPI_SEARCH_SCOPE,     (void *)scope );
1387         slapi_pblock_set( pb, SLAPI_SEARCH_FILTER,     NULL );
1388         slapi_pblock_set( pb, SLAPI_SEARCH_STRFILTER, (void *)filter );
1389         slapi_pblock_set( pb, SLAPI_SEARCH_ATTRS,     (void *)attrs );
1390         slapi_pblock_set( pb, SLAPI_SEARCH_ATTRSONLY, (void *)attrsonly );
1391         slapi_pblock_set( pb, SLAPI_REQCONTROLS,      (void *)controls );
1392         slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID,  (void *)uniqueid );
1393         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY,  (void *)plugin_identity );
1394         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,    (void *)operation_flags );
1395 #endif /* LDAP_SLAPI */
1396 }
1397
1398 Slapi_PBlock *
1399 slapi_search_internal(
1400         char *ldn, 
1401         int scope, 
1402         char *filStr, 
1403         LDAPControl **controls, 
1404         char **attrs, 
1405         int attrsonly ) 
1406 {
1407 #ifdef LDAP_SLAPI
1408         Slapi_PBlock *pb;
1409
1410         pb = slapi_pblock_new();
1411         if ( pb == NULL ) {
1412                 return NULL;
1413         }
1414
1415         slapi_search_internal_set_pb( pb, ldn, scope, filStr, attrs, attrsonly,
1416                 controls, NULL, NULL, 0 );
1417
1418         slapi_search_internal_pb( pb );
1419
1420         return pb;
1421 #else
1422         return NULL;
1423 #endif /* LDAP_SLAPI */
1424 }
1425
1426 void slapi_modify_internal_set_pb( Slapi_PBlock *pb,
1427         const char *dn,
1428         LDAPMod **mods,
1429         LDAPControl **controls,
1430         const char *uniqueid,
1431         Slapi_ComponentId *plugin_identity,
1432         int operation_flags )
1433 {
1434 #ifdef LDAP_SLAPI
1435         slapi_pblock_set( pb, SLAPI_MODIFY_TARGET,   (void *)dn );
1436         slapi_pblock_set( pb, SLAPI_MODIFY_MODS,     (void *)mods );
1437         slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
1438         slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
1439         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1440         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)operation_flags );
1441 #endif /* LDAP_SLAPI */
1442 }
1443
1444 /* Function : slapi_modify_internal
1445  *
1446  * Description: Plugin functions call this routine to modify an entry 
1447  *                              in the backend directly
1448  * Return values : LDAP_SUCCESS
1449  *                 LDAP_PARAM_ERROR
1450  *                 LDAP_NO_MEMORY
1451  *                 LDAP_OTHER
1452  *                 LDAP_UNWILLING_TO_PERFORM
1453 */
1454 Slapi_PBlock *
1455 slapi_modify_internal(
1456         char *ldn,      
1457         LDAPMod **mods, 
1458         LDAPControl **controls, 
1459         int log_change )
1460 {
1461 #ifdef LDAP_SLAPI
1462         Slapi_PBlock *pb;
1463
1464         pb = slapi_pblock_new();
1465         if ( pb == NULL ) {
1466                 return NULL;
1467         }
1468
1469         slapi_modify_internal_set_pb( pb, ldn, mods, controls, NULL, NULL,
1470                 log_change ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1471
1472         slapi_modify_internal_pb( pb );
1473
1474         return pb;
1475 #else
1476         return NULL;
1477 #endif /* LDAP_SLAPI */
1478 }
1479
1480 int slapi_add_internal_set_pb( Slapi_PBlock *pb,
1481         const char *dn,
1482         LDAPMod **attrs,
1483         LDAPControl **controls,
1484         Slapi_ComponentId *plugin_identity,
1485         int operation_flags )
1486 {
1487 #ifdef LDAP_SLAPI
1488         slapi_pblock_set( pb, SLAPI_ADD_TARGET,      (void *)dn );
1489         slapi_pblock_set( pb, SLAPI_MODIFY_MODS,     (void *)attrs );
1490         slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
1491         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1492         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)operation_flags );
1493
1494         return 0;
1495 #else
1496         return -1;
1497 #endif /* LDAP_SLAPI */
1498 }
1499
1500 Slapi_PBlock *slapi_add_internal(
1501         char * dn,
1502         LDAPMod **attrs,
1503         LDAPControl **controls,
1504         int log_changes )
1505 {
1506 #ifdef LDAP_SLAPI
1507         Slapi_PBlock *pb;
1508
1509         pb = slapi_pblock_new();
1510         if ( pb == NULL )
1511                 return NULL;
1512
1513         slapi_add_internal_set_pb( pb, dn, attrs, controls, NULL,
1514                 log_changes ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1515         
1516         slapi_add_internal_pb( pb );
1517
1518         return pb;
1519 #else
1520         return NULL;
1521 #endif /* LDAP_SLAPI */
1522 }
1523
1524 void slapi_add_entry_internal_set_pb( Slapi_PBlock *pb,
1525         Slapi_Entry *e,
1526         LDAPControl **controls,
1527         Slapi_ComponentId *plugin_identity,
1528         int operation_flags )
1529 {
1530 #ifdef LDAP_SLAPI
1531         slapi_pblock_set( pb, SLAPI_ADD_ENTRY,       (void *)e );
1532         slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
1533         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1534         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)operation_flags );
1535 #endif /* LDAP_SLAPI */
1536 }
1537
1538 Slapi_PBlock * 
1539 slapi_add_entry_internal(
1540         Slapi_Entry *e, 
1541         LDAPControl **controls, 
1542         int log_changes )
1543 {
1544 #ifdef LDAP_SLAPI
1545         Slapi_PBlock *pb;
1546
1547         pb = slapi_pblock_new();
1548         if ( pb == NULL )
1549                 return NULL;
1550
1551         slapi_add_entry_internal_set_pb( pb, e, controls, NULL,
1552                 log_changes ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1553         
1554         slapi_add_internal_pb( pb );
1555
1556         return pb;
1557 #else
1558         return NULL;
1559 #endif /* LDAP_SLAPI */
1560 }
1561
1562 void slapi_rename_internal_set_pb( Slapi_PBlock *pb,
1563         const char *olddn,
1564         const char *newrdn,
1565         const char *newsuperior,
1566         int deloldrdn,
1567         LDAPControl **controls,
1568         const char *uniqueid,
1569         Slapi_ComponentId *plugin_identity,
1570         int operation_flags )
1571 {
1572 #ifdef LDAP_SLAPI
1573         slapi_pblock_set( pb, SLAPI_MODRDN_TARGET,      (void *)olddn );
1574         slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN,      (void *)newrdn );
1575         slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR, (void *)newsuperior );
1576         slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN,   (void *)deloldrdn );
1577         slapi_pblock_set( pb, SLAPI_REQCONTROLS,        (void *)controls );
1578         slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID,    (void *)uniqueid );
1579         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY,    (void *)plugin_identity );
1580         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,      (void *)operation_flags );
1581 #endif /* LDAP_SLAPI */
1582 }
1583
1584 /* Function : slapi_modrdn_internal
1585  *
1586  * Description : Plugin functions call this routine to modify the rdn 
1587  *                               of an entry in the backend directly
1588  * Return values : LDAP_SUCCESS
1589  *                 LDAP_PARAM_ERROR
1590  *                 LDAP_NO_MEMORY
1591  *                 LDAP_OTHER
1592  *                 LDAP_UNWILLING_TO_PERFORM
1593  *
1594  * NOTE: This function does not support the "newSuperior" option from LDAP V3.
1595  */
1596 Slapi_PBlock *
1597 slapi_modrdn_internal(
1598         char *olddn, 
1599         char *lnewrdn, 
1600         int deloldrdn, 
1601         LDAPControl **controls, 
1602         int log_change )
1603 {
1604 #ifdef LDAP_SLAPI
1605         Slapi_PBlock *pb;
1606
1607         pb = slapi_pblock_new();
1608         if ( pb == NULL ) {
1609                 return NULL;
1610         }
1611
1612         slapi_rename_internal_set_pb( pb, olddn, lnewrdn, NULL,
1613                 deloldrdn, controls, NULL, NULL,
1614                 log_change ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1615
1616         slapi_modrdn_internal_pb( pb );
1617
1618         return pb;
1619 #else
1620         return NULL;
1621 #endif /* LDAP_SLAPI */
1622 }
1623
1624 void slapi_delete_internal_set_pb( Slapi_PBlock *pb,
1625         const char *dn,
1626         LDAPControl **controls,
1627         const char *uniqueid,
1628         Slapi_ComponentId *plugin_identity,
1629         int operation_flags )
1630 {
1631 #ifdef LDAP_SLAPI
1632         slapi_pblock_set( pb, SLAPI_TARGET_DN,       (void *)dn );
1633         slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
1634         slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
1635         slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
1636         slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)operation_flags );
1637 #endif /* LDAP_SLAPI */
1638 }
1639
1640 /* Function : slapi_delete_internal
1641  *
1642  * Description : Plugin functions call this routine to delete an entry 
1643  *               in the backend directly
1644  * Return values : LDAP_SUCCESS
1645  *                 LDAP_PARAM_ERROR
1646  *                 LDAP_NO_MEMORY
1647  *                 LDAP_OTHER
1648  *                 LDAP_UNWILLING_TO_PERFORM
1649 */
1650 Slapi_PBlock *
1651 slapi_delete_internal(
1652         char *ldn, 
1653         LDAPControl **controls, 
1654         int log_change )
1655 {
1656 #ifdef LDAP_SLAPI
1657         Slapi_PBlock *pb;
1658
1659         pb = slapi_pblock_new();
1660         if ( pb == NULL )
1661                 return NULL;
1662
1663         slapi_delete_internal_set_pb( pb, ldn, controls, NULL, NULL,
1664                 log_change ? SLAPI_OP_FLAG_LOG_CHANGE : 0 );
1665
1666         slapi_delete_internal_pb( pb );
1667
1668         return pb;
1669 #else
1670         return NULL;
1671 #endif /* LDAP_SLAPI */
1672 }
1673