]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_ops.c
Fix incorrect usage of slapi_ch_free()
[openldap] / servers / slapd / slapi / slapi_ops.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * (C) Copyright IBM Corp. 1997,2002
7  * Redistribution and use in source and binary forms are permitted
8  * provided that this notice is preserved and that due credit is
9  * given to IBM Corporation. This software is provided ``as is''
10  * without express or implied warranty.
11  */
12 /*
13  * Portions (C) Copyright PADL Software Pty Ltd. 2003
14  * Redistribution and use in source and binary forms are permitted
15  * provided that this notice is preserved and that due credit is 
16  * given to PADL Software Pty Ltd. This software is provided ``as is'' 
17  * without express or implied warranty.
18  */
19
20 #include "portable.h"
21 #include <slap.h>
22 #include <lber_pvt.h>
23 #include <slapi.h>
24
25 /*
26  * use a fake listener when faking a connection,
27  * so it can be used in ACLs
28  */
29 static struct slap_listener slap_unknown_listener = {
30         BER_BVC("unknown"),     /* FIXME: use a URI form? */
31         BER_BVC("UNKNOWN")
32 };
33
34 int bvptr2obj( struct berval **bvptr, struct berval **bvobj );
35
36 static void
37 internal_result_v3(
38         Operation       *op, 
39         SlapReply       *rs )
40 {
41 #ifdef notdef
42         /* XXX needs review after internal API change */
43         /* rs->sr_nentries appears to always be 0 */
44         if (op->o_tag == LDAP_REQ_SEARCH)
45                 slapi_pblock_set( (Slapi_PBlock *)op->o_pb,
46                         SLAPI_NENTRIES, (void *)rs->sr_nentries );
47 #endif
48
49         return;
50 }
51
52 static int
53 internal_search_entry(
54         Operation       *op,
55         SlapReply       *rs )
56 {
57         char *ent2str = NULL;
58         int nentries = 0, len = 0, i = 0;
59         Slapi_Entry **head = NULL, **tp;
60         
61         ent2str = slapi_entry2str( rs->sr_entry, &len );
62         if ( ent2str == NULL ) {
63                 return 1;
64         }
65
66         slapi_pblock_get( (Slapi_PBlock *)op->o_pb,
67                         SLAPI_NENTRIES, &nentries );
68         slapi_pblock_get( (Slapi_PBlock *)op->o_pb,
69                         SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &head );
70         
71         i = nentries + 1;
72         if ( nentries == 0 ) {
73                 tp = (Slapi_Entry **)slapi_ch_malloc( 2 * sizeof(Slapi_Entry *) );
74                 if ( tp == NULL ) {
75                         return 1;
76                 }
77
78                 tp[ 0 ] = (Slapi_Entry *)str2entry( ent2str );
79                 if ( tp[ 0 ] == NULL ) { 
80                         return 1;
81                 }
82
83         } else {
84                 tp = (Slapi_Entry **)slapi_ch_realloc( (char *)head,
85                                 sizeof(Slapi_Entry *) * ( i + 1 ) );
86                 if ( tp == NULL ) {
87                         return 1;
88                 }
89                 tp[ i - 1 ] = (Slapi_Entry *)str2entry( ent2str );
90                 if ( tp[ i - 1 ] == NULL ) { 
91                         return 1;
92                 }
93         }
94         tp[ i ] = NULL;
95                   
96         slapi_pblock_set( (Slapi_PBlock *)op->o_pb,
97                         SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, (void *)tp );
98         slapi_pblock_set( (Slapi_PBlock *)op->o_pb,
99                         SLAPI_NENTRIES, (void *)i );
100
101         return LDAP_SUCCESS;
102 }
103
104 static void
105 internal_result_ext(
106         Operation       *op,    
107         SlapReply       *sr )
108 {
109         return;
110 }
111
112 static int
113 internal_search_reference(
114         Operation       *op,    
115         SlapReply       *sr )
116 {
117         return LDAP_SUCCESS;
118 }
119
120 static Connection *
121 slapiConnectionInit(
122         char *DN, 
123         int OpType ) 
124
125         Connection *pConn, *c;
126         ber_len_t max = sockbuf_max_incoming;
127
128         pConn = (Connection *) slapi_ch_calloc(1, sizeof(Connection));
129         if (pConn == NULL) {
130                 return (Connection *)NULL;
131         }
132
133         LDAP_STAILQ_INIT( &pConn->c_pending_ops );
134
135         pConn->c_pending_ops.stqh_first =
136                 (Operation *) slapi_ch_calloc( 1, sizeof(Operation) );
137         if ( pConn->c_pending_ops.stqh_first == NULL ) { 
138                 slapi_ch_free( (void **)&pConn );
139                 return (Connection *)NULL;
140         }
141
142         pConn->c_pending_ops.stqh_first->o_pb = 
143                 (Slapi_PBlock *) slapi_pblock_new();
144         if ( pConn->c_pending_ops.stqh_first->o_pb == NULL ) {
145                 slapi_ch_free( (void **)&pConn->c_pending_ops.stqh_first );
146                 slapi_ch_free( (void **)&pConn );
147                 return (Connection *)NULL;
148         }
149
150         c = pConn;
151
152         /* operation object */
153         c->c_pending_ops.stqh_first->o_tag = OpType;
154         c->c_pending_ops.stqh_first->o_protocol = LDAP_VERSION3; 
155         c->c_pending_ops.stqh_first->o_authmech.bv_val = NULL; 
156         c->c_pending_ops.stqh_first->o_authmech.bv_len = 0; 
157         c->c_pending_ops.stqh_first->o_time = slap_get_time();
158         c->c_pending_ops.stqh_first->o_do_not_cache = 1;
159         c->c_pending_ops.stqh_first->o_threadctx = ldap_pvt_thread_pool_context();
160         c->c_pending_ops.stqh_first->o_tmpmemctx = NULL;
161         c->c_pending_ops.stqh_first->o_tmpmfuncs = &ch_mfuncs;
162         c->c_pending_ops.stqh_first->o_conn = c;
163
164         /* connection object */
165         c->c_authmech.bv_val = NULL;
166         c->c_authmech.bv_len = 0;
167         c->c_dn.bv_val = NULL;
168         c->c_dn.bv_len = 0;
169         c->c_ndn.bv_val = NULL;
170         c->c_ndn.bv_len = 0;
171         c->c_groups = NULL;
172
173         c->c_listener = &slap_unknown_listener;
174         ber_dupbv( &c->c_peer_domain, (struct berval *)&slap_unknown_bv );
175         ber_dupbv( &c->c_peer_name, (struct berval *)&slap_unknown_bv );
176
177         LDAP_STAILQ_INIT( &c->c_ops );
178
179         c->c_sasl_bind_mech.bv_val = NULL;
180         c->c_sasl_bind_mech.bv_len = 0;
181         c->c_sasl_context = NULL;
182         c->c_sasl_extra = NULL;
183
184         c->c_sb = ber_sockbuf_alloc( );
185
186         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
187
188         c->c_currentber = NULL;
189
190         /* should check status of thread calls */
191         ldap_pvt_thread_mutex_init( &c->c_mutex );
192         ldap_pvt_thread_mutex_init( &c->c_write_mutex );
193         ldap_pvt_thread_cond_init( &c->c_write_cv );
194
195         c->c_n_ops_received = 0;
196         c->c_n_ops_executing = 0;
197         c->c_n_ops_pending = 0;
198         c->c_n_ops_completed = 0;
199
200         c->c_n_get = 0;
201         c->c_n_read = 0;
202         c->c_n_write = 0;
203
204         c->c_protocol = LDAP_VERSION3; 
205
206         c->c_activitytime = c->c_starttime = slap_get_time();
207
208         c->c_connid = 0;
209
210         c->c_conn_state  = 0x01;        /* SLAP_C_ACTIVE */
211         c->c_struct_state = 0x02;       /* SLAP_C_USED */
212
213         c->c_ssf = c->c_transport_ssf = 0;
214         c->c_tls_ssf = 0;
215
216         backend_connection_init( c );
217
218         pConn->c_send_ldap_result = internal_result_v3;
219         pConn->c_send_search_entry = internal_search_entry;
220         pConn->c_send_ldap_extended = internal_result_ext;
221         pConn->c_send_search_reference = internal_search_reference;
222
223         return pConn;
224 }
225
226 static void slapiConnectionDestroy( Connection **pConn )
227 {
228         Connection *conn = *pConn;
229         Operation *op;
230
231         if ( pConn == NULL ) {
232                 return;
233         }
234
235         op = (Operation *)conn->c_pending_ops.stqh_first;
236
237         if ( op->o_req_dn.bv_val != NULL ) {
238                 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
239         }
240         if ( op->o_req_ndn.bv_val != NULL ) {
241                 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
242         }
243
244         if ( conn->c_sb != NULL ) {
245                 ber_sockbuf_free( conn->c_sb );
246         }
247         if ( op != NULL ) {
248                 slapi_ch_free( (void **)&op );
249         }
250         slapi_ch_free( (void **)pConn );
251 }
252
253 /*
254  * Function : values2obj
255  * Convert an array of strings into a BerVarray.
256  * the strings.
257  */
258 static int
259 values2obj(
260         char **ppValue,
261         BerVarray *bvobj)
262 {
263         int i;
264         BerVarray tmpberval;
265
266         if ( ppValue == NULL ) {
267                 *bvobj = NULL;
268                 return LDAP_SUCCESS;
269         }
270
271         for ( i = 0; ppValue[i] != NULL; i++ )
272                 ;
273
274         tmpberval = (BerVarray)slapi_ch_malloc( (i+1) * (sizeof(struct berval)) );
275         if ( tmpberval == NULL ) {
276                 return LDAP_NO_MEMORY;
277         }
278         for ( i = 0; ppValue[i] != NULL; i++ ) {
279                 tmpberval[i].bv_val = ppValue[i];
280                 tmpberval[i].bv_len = strlen( ppValue[i] );
281         }
282         tmpberval[i].bv_val = NULL;
283         tmpberval[i].bv_len = 0;
284
285         *bvobj = tmpberval;
286
287         return LDAP_SUCCESS;
288 }
289
290 static void
291 freeMods( Modifications *ml )
292 {
293         /*
294          * Free a modification list whose values have been 
295          * set with bvptr2obj() or values2obj() (ie. they
296          * do not own the pointer to the underlying values)
297          */
298         Modifications *next;
299
300         for ( ; ml != NULL; ml = next ) {
301                 next = ml->sml_next;
302
303                 slapi_ch_free( (void **)&ml->sml_bvalues );
304                 slapi_ch_free( (void **)&ml->sml_nvalues );
305                 slapi_ch_free( (void **)&ml );
306         }
307 }
308
309 /*
310  * Function : LDAPModToEntry 
311  * convert a dn plus an array of LDAPMod struct ptrs to an entry structure
312  * with a link list of the correspondent attributes.
313  * Return value : LDAP_SUCCESS
314  *                LDAP_NO_MEMORY
315  *                LDAP_OTHER
316 */
317 Entry *
318 LDAPModToEntry(
319         char *ldn, 
320         LDAPMod **mods )
321 {
322         struct berval           dn = { 0, NULL };
323         Entry                   *pEntry=NULL;
324         LDAPMod                 *pMod;
325         struct berval           *bv;
326         Operation               *op;
327
328         Modifications           *modlist = NULL;
329         Modifications           **modtail = &modlist;
330         Modifications           tmp;
331
332         int                     rc = LDAP_SUCCESS;
333         int                     i;
334
335         const char              *text = NULL;
336
337
338         op = (Operation *) slapi_ch_calloc(1, sizeof(Operation));
339         if ( pEntry == NULL) {
340                 rc = LDAP_NO_MEMORY;
341                 goto cleanup;
342         }  
343         op->o_tag = LDAP_REQ_ADD;
344
345         pEntry = (Entry *) ch_calloc( 1, sizeof(Entry) );
346         if ( pEntry == NULL) {
347                 rc = LDAP_NO_MEMORY;
348                 goto cleanup;
349         } 
350
351         dn.bv_val = slapi_ch_strdup(ldn);
352         dn.bv_len = strlen(ldn);
353
354         rc = dnPrettyNormal( NULL, &dn, &pEntry->e_name, &pEntry->e_nname, NULL );
355         if ( rc != LDAP_SUCCESS )
356                 goto cleanup;
357
358         if ( rc == LDAP_SUCCESS ) {
359                 for ( i=0, pMod=mods[0]; rc == LDAP_SUCCESS && pMod != NULL; pMod=mods[++i]) {
360                         Modifications *mod;
361                         if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
362                                 /* attr values are in berval format */
363                                 /* convert an array of pointers to bervals to an array of bervals */
364                                 rc = bvptr2obj(pMod->mod_bvalues, &bv);
365                                 if (rc != LDAP_SUCCESS) goto cleanup;
366                                 tmp.sml_type.bv_val = pMod->mod_type;
367                                 tmp.sml_type.bv_len = strlen( pMod->mod_type );
368                                 tmp.sml_bvalues = bv;
369                                 tmp.sml_nvalues = NULL;
370                 
371                                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
372
373                                 mod->sml_op = LDAP_MOD_ADD;
374                                 mod->sml_next = NULL;
375                                 mod->sml_desc = NULL;
376                                 mod->sml_type = tmp.sml_type;
377                                 mod->sml_bvalues = tmp.sml_bvalues;
378                                 mod->sml_nvalues = tmp.sml_nvalues;
379
380                                 *modtail = mod;
381                                 modtail = &mod->sml_next;
382
383                         } else {
384                                 /* attr values are in string format, need to be converted */
385                                 /* to an array of bervals */ 
386                                 if ( pMod->mod_values == NULL ) {
387                                         rc = LDAP_OTHER;
388                                 } else {
389                                         rc = values2obj( pMod->mod_values, &bv );
390                                         if (rc != LDAP_SUCCESS) goto cleanup;
391                                         tmp.sml_type.bv_val = pMod->mod_type;
392                                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
393                                         tmp.sml_bvalues = bv;
394                                         tmp.sml_nvalues = NULL;
395                 
396                                         mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
397
398                                         mod->sml_op = LDAP_MOD_ADD;
399                                         mod->sml_next = NULL;
400                                         mod->sml_desc = NULL;
401                                         mod->sml_type = tmp.sml_type;
402                                         mod->sml_bvalues = tmp.sml_bvalues;
403                                         mod->sml_nvalues = tmp.sml_nvalues;
404
405                                         *modtail = mod;
406                                         modtail = &mod->sml_next;
407                                 }
408                         }
409                 } /* for each LDAPMod */
410         }
411
412         op->o_bd = select_backend( &pEntry->e_nname, 0, 0 );
413         if ( op->o_bd == NULL ) {
414                 rc = LDAP_PARTIAL_RESULTS;
415         } else {
416                 int repl_user = be_isupdate( op->o_bd, &op->o_bd->be_rootdn );
417                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
418                         int update = op->o_bd->be_update_ndn.bv_len;
419                         char textbuf[SLAP_TEXT_BUFLEN];
420                         size_t textlen = sizeof textbuf;
421
422                         rc = slap_mods_check( modlist, update, &text, 
423                                         textbuf, textlen, NULL );
424                         if ( rc != LDAP_SUCCESS) {
425                                 goto cleanup;
426                         }
427
428                         if ( !repl_user ) {
429                                 rc = slap_mods_opattrs( op,
430                                                 modlist, modtail, &text, 
431                                                 textbuf, textlen );
432                                 if ( rc != LDAP_SUCCESS) {
433                                         goto cleanup;
434                                 }
435                         }
436
437                         /*
438                          * FIXME: slap_mods2entry is declared static 
439                          * in servers/slapd/add.c
440                          */
441                         rc = slap_mods2entry( modlist, &pEntry, repl_user,
442                                         &text, textbuf, textlen );
443                         if (rc != LDAP_SUCCESS) {
444                                 goto cleanup;
445                         }
446
447                 } else {
448                         rc = LDAP_REFERRAL;
449                 }
450         }
451
452 cleanup:
453
454         if ( dn.bv_val )
455                 slapi_ch_free( (void **)&dn.bv_val );
456         if ( op )
457                 slapi_ch_free( (void **)&op );
458         if ( modlist != NULL )
459                 freeMods( modlist );
460         if ( rc != LDAP_SUCCESS ) {
461                 if ( pEntry != NULL ) {
462                         slapi_entry_free( pEntry );
463                 }
464                 pEntry = NULL;
465         }
466
467         return( pEntry );
468 }
469
470 /* Function : slapi_delete_internal
471  *
472  * Description : Plugin functions call this routine to delete an entry 
473  *               in the backend directly
474  * Return values : LDAP_SUCCESS
475  *                 LDAP_PARAM_ERROR
476  *                 LDAP_NO_MEMORY
477  *                 LDAP_OTHER
478  *                 LDAP_UNWILLING_TO_PERFORM
479 */
480 Slapi_PBlock *
481 slapi_delete_internal(
482         char *ldn, 
483         LDAPControl **controls, 
484         int log_change )
485 {
486 #ifdef LDAP_SLAPI
487         Connection              *pConn = NULL;
488         Operation               *op = NULL;
489         Slapi_PBlock            *pPB = NULL;
490         Slapi_PBlock            *pSavePB = NULL;
491         SlapReply               rs = { REP_RESULT };
492         struct berval           dn = { 0, NULL };
493
494         int                     manageDsaIt = 0;
495         int                     isCritical;
496
497         if ( ldn == NULL ) {
498                 rs.sr_err = LDAP_PARAM_ERROR; 
499                 goto cleanup;
500         }
501
502         pConn = slapiConnectionInit( NULL, LDAP_REQ_DELETE );
503         if (pConn == NULL) {
504                 rs.sr_err = LDAP_NO_MEMORY;
505                 goto cleanup;
506         }
507
508         op = (Operation *)pConn->c_pending_ops.stqh_first;
509         pPB = (Slapi_PBlock *)op->o_pb;
510         op->o_ctrls = controls;
511
512         dn.bv_val = slapi_ch_strdup(ldn);
513         dn.bv_len = strlen(ldn);
514         rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
515         if ( rs.sr_err != LDAP_SUCCESS )
516                 goto cleanup;
517
518         if ( slapi_control_present( controls, 
519                         SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical) ) {
520                 manageDsaIt = 1; 
521         }
522
523         op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
524         if ( op->o_bd == NULL ) {
525                 rs.sr_err = LDAP_PARTIAL_RESULTS;
526                 goto cleanup;
527         }
528
529         op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
530         op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
531
532         if ( op->o_bd->be_delete ) {
533                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
534                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
535                         if ( (*op->o_bd->be_delete)( op, &rs ) == 0 ) {
536                                 if ( log_change ) {
537                                         replog( op );
538                                 }
539                         } else {
540                                 rs.sr_err = LDAP_OTHER;
541                         }
542                 } else {
543                         rs.sr_err = LDAP_REFERRAL;
544                 }
545         } else {
546                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
547         }
548
549 cleanup:
550         if ( pPB != NULL ) {
551                 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
552         }
553         if ( dn.bv_val ) {
554                 slapi_ch_free( (void **)&dn.bv_val );
555         }
556         if ( pConn != NULL ) {
557                 pSavePB = pPB;
558         }
559
560         slapiConnectionDestroy( &pConn );
561
562         return (pSavePB);
563 #else
564         return NULL;
565 #endif /* LDAP_SLAPI */
566 }
567
568 Slapi_PBlock * 
569 slapi_add_entry_internal(
570         Slapi_Entry *e, 
571         LDAPControl **controls, 
572         int log_changes ) 
573 {
574 #ifdef LDAP_SLAPI
575         Connection              *pConn = NULL;
576         Operation               *op = NULL;
577         Slapi_PBlock            *pPB = NULL, *pSavePB = NULL;
578
579         int                     manageDsaIt = 0;
580         int                     isCritical;
581         SlapReply               rs = { REP_RESULT };
582
583         if ( e == NULL ) {
584                 rs.sr_err = LDAP_PARAM_ERROR;
585                 goto cleanup;
586         }
587         
588         pConn = slapiConnectionInit( NULL, LDAP_REQ_ADD );
589         if ( pConn == NULL ) {
590                 rs.sr_err = LDAP_NO_MEMORY;
591                 goto cleanup;
592         }
593
594         if ( slapi_control_present( controls, LDAP_CONTROL_MANAGEDSAIT,
595                                 NULL, &isCritical ) ) {
596                 manageDsaIt = 1; 
597         }
598
599         op = (Operation *)pConn->c_pending_ops.stqh_first;
600         pPB = (Slapi_PBlock *)op->o_pb;
601         op->o_ctrls = controls;
602
603         op->o_bd = select_backend( &e->e_nname, manageDsaIt, 0 );
604         if ( op->o_bd == NULL ) {
605                 rs.sr_err = LDAP_PARTIAL_RESULTS;
606                 goto cleanup;
607         }
608
609         op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
610         op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
611         op->oq_add.rs_e = e;
612
613         if ( op->o_bd->be_add ) {
614                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
615                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ){
616                         if ( (*op->o_bd->be_add)( op, &rs ) == 0 ) {
617                                 if ( log_changes ) {
618                                         replog( op );
619                                 }
620                         }
621                 } else {
622                         rs.sr_err = LDAP_REFERRAL;
623                 }
624         } else {
625                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
626         }
627
628 cleanup:
629
630         if ( pPB != NULL ) {
631                 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
632         }
633
634         if ( pConn != NULL ) {
635                 pSavePB = pPB;
636         }
637
638         slapiConnectionDestroy( &pConn );
639
640         return( pSavePB );
641 #else
642         return NULL;
643 #endif /* LDAP_SLAPI */
644 }
645
646
647 Slapi_PBlock *
648 slapi_add_internal(
649         char *dn, 
650         LDAPMod **mods, 
651         LDAPControl **controls, 
652         int log_changes  ) 
653 {
654 #ifdef LDAP_SLAPI
655         LDAPMod                 *pMod = NULL;
656         Slapi_PBlock            *pb = NULL;
657         Entry                   *pEntry = NULL;
658         int                     i, rc = LDAP_SUCCESS;
659
660         if ( mods == NULL || *mods == NULL || dn == NULL || *dn == '\0' ) {
661                 rc = LDAP_PARAM_ERROR ;
662         }
663
664         if ( rc == LDAP_SUCCESS ) {
665                 for ( i = 0, pMod = mods[0]; pMod != NULL; pMod = mods[++i] ) {
666                         if ( (pMod->mod_op & ~LDAP_MOD_BVALUES) != LDAP_MOD_ADD ) {
667                                 rc = LDAP_OTHER;
668                                 break;
669                         }
670                 }
671         }
672
673         if ( rc == LDAP_SUCCESS ) {
674                 if((pEntry = LDAPModToEntry( dn, mods )) == NULL) {
675                         rc = LDAP_OTHER;
676                 }
677         }
678
679         if ( rc != LDAP_SUCCESS ) {
680                 pb = slapi_pblock_new();
681                 slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
682         } else {
683                 pb = slapi_add_entry_internal( pEntry, controls, log_changes );
684         }
685
686         if ( pEntry ) {
687                 slapi_entry_free(pEntry);
688         }
689
690         return(pb);
691 #else
692         return NULL;
693 #endif /* LDAP_SLAPI */
694 }
695
696 /* Function : slapi_modrdn_internal
697  *
698  * Description : Plugin functions call this routine to modify the rdn 
699  *                               of an entry in the backend directly
700  * Return values : LDAP_SUCCESS
701  *                 LDAP_PARAM_ERROR
702  *                 LDAP_NO_MEMORY
703  *                 LDAP_OTHER
704  *                 LDAP_UNWILLING_TO_PERFORM
705  *
706  * NOTE: This function does not support the "newSuperior" option from LDAP V3.
707  */
708 Slapi_PBlock *
709 slapi_modrdn_internal(
710         char *olddn, 
711         char *lnewrdn, 
712         int deloldrdn, 
713         LDAPControl **controls, 
714         int log_change )
715 {
716 #ifdef LDAP_SLAPI
717         struct berval           dn = { 0, NULL };
718         struct berval           newrdn = { 0, NULL };
719         Connection              *pConn = NULL;
720         Operation               *op = NULL;
721         Slapi_PBlock            *pPB = NULL;
722         Slapi_PBlock            *pSavePB = NULL;
723         int                     manageDsaIt = 0;
724         int                     isCritical;
725         SlapReply               rs = { REP_RESULT };
726
727         pConn = slapiConnectionInit( NULL,  LDAP_REQ_MODRDN);
728         if ( pConn == NULL) {
729                 rs.sr_err = LDAP_NO_MEMORY;
730                 goto cleanup;
731         }
732
733         op = (Operation *)pConn->c_pending_ops.stqh_first;
734         pPB = (Slapi_PBlock *)op->o_pb;
735         op->o_ctrls = controls;
736
737         if ( slapi_control_present( controls, 
738                         SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
739                 manageDsaIt = 1;
740         }
741
742         op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
743         if ( op->o_bd == NULL ) {
744                 rs.sr_err =  LDAP_PARTIAL_RESULTS;
745                 goto cleanup;
746         }
747
748         op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
749         op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
750
751         dn.bv_val = slapi_ch_strdup( olddn );
752         dn.bv_len = strlen( olddn );
753
754         rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
755         if ( rs.sr_err != LDAP_SUCCESS ) {
756                 goto cleanup;
757         }
758
759         if ( op->o_req_dn.bv_len == 0 ) {
760                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
761                 goto cleanup;
762         }
763
764         newrdn.bv_val = slapi_ch_strdup( lnewrdn );
765         newrdn.bv_len = strlen( lnewrdn );
766
767         rs.sr_err = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn, NULL );
768         if ( rs.sr_err != LDAP_SUCCESS ) {
769                 goto cleanup;
770         }
771
772         if ( rdnValidate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
773                 goto cleanup;
774         }
775
776         op->oq_modrdn.rs_newSup = NULL;
777         op->oq_modrdn.rs_nnewSup = NULL;
778         op->oq_modrdn.rs_deleteoldrdn = deloldrdn;
779
780         if ( op->o_bd->be_modrdn ) {
781                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
782                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
783                         if ( (*op->o_bd->be_modrdn)( op, &rs ) == 0 ) {
784                                 if ( log_change ) {
785                                         replog( op );
786                                 }
787                         } else {
788                                 rs.sr_err = LDAP_OTHER;
789                         }
790                 } else {
791                         rs.sr_err = LDAP_REFERRAL;
792                 }
793         } else {
794                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
795         }
796
797 cleanup:
798
799         if ( pPB != NULL ) {
800                 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
801         }
802         
803         if ( dn.bv_val )
804                 slapi_ch_free( (void **)&dn.bv_val );
805
806         if ( newrdn.bv_val )
807                 slapi_ch_free( (void **)&newrdn.bv_val );
808         if ( op->oq_modrdn.rs_newrdn.bv_val )
809                 slapi_ch_free( (void **)&op->oq_modrdn.rs_newrdn.bv_val );
810         if ( op->oq_modrdn.rs_nnewrdn.bv_val )
811                 slapi_ch_free( (void **)&op->oq_modrdn.rs_nnewrdn.bv_val );
812
813         if ( pConn != NULL ) {
814                 pSavePB = pPB;
815         }
816
817         slapiConnectionDestroy( &pConn );
818
819         return( pSavePB );
820 #else
821         return NULL;
822 #endif /* LDAP_SLAPI */
823 }
824
825 /* Function : slapi_modify_internal
826  *
827  * Description: Plugin functions call this routine to modify an entry 
828  *                              in the backend directly
829  * Return values : LDAP_SUCCESS
830  *                 LDAP_PARAM_ERROR
831  *                 LDAP_NO_MEMORY
832  *                 LDAP_OTHER
833  *                 LDAP_UNWILLING_TO_PERFORM
834 */
835 Slapi_PBlock *
836 slapi_modify_internal(
837         char *ldn,      
838         LDAPMod **mods, 
839         LDAPControl **controls, 
840         int log_change )
841 {
842 #ifdef LDAP_SLAPI
843         int                     i;
844         Connection              *pConn = NULL;
845         Operation               *op = NULL;
846         Slapi_PBlock            *pPB = NULL;
847         Slapi_PBlock            *pSavePB = NULL;
848
849         struct berval dn = { 0, NULL };
850
851         int                     manageDsaIt = 0;
852         int                     isCritical;
853         struct berval           *bv;
854         LDAPMod                 *pMod;
855
856         Modifications           *modlist = NULL;
857         Modifications           **modtail = &modlist;
858         Modifications           tmp;
859
860         SlapReply               rs = { REP_RESULT };
861
862         if ( mods == NULL || *mods == NULL || ldn == NULL ) {
863                 rs.sr_err = LDAP_PARAM_ERROR ;
864                 goto cleanup;
865         }
866
867         pConn = slapiConnectionInit( NULL,  LDAP_REQ_MODIFY );
868         if ( pConn == NULL ) {
869                 rs.sr_err = LDAP_NO_MEMORY;
870                 goto cleanup;
871         }
872
873         op = (Operation *)pConn->c_pending_ops.stqh_first;
874         pPB = (Slapi_PBlock *)op->o_pb;
875         op->o_ctrls = controls;
876
877         dn.bv_val = slapi_ch_strdup( ldn );
878         dn.bv_len = strlen( ldn );
879         rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
880         if ( rs.sr_err != LDAP_SUCCESS ) {
881                 goto cleanup;
882         }
883
884         if ( slapi_control_present( controls, 
885                         SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
886                 manageDsaIt = 1;
887         }
888
889         op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
890         if ( op->o_bd == NULL ) {
891                 rs.sr_err = LDAP_PARTIAL_RESULTS;
892                 goto cleanup;
893         }
894
895         op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
896         op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
897
898         for ( i = 0, pMod = mods[0];
899                 rs.sr_err == LDAP_SUCCESS && pMod != NULL; 
900                 pMod = mods[++i] )
901         {
902                 Modifications *mod;
903
904                 if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
905                         /*
906                          * attr values are in berval format
907                          * convert an array of pointers to bervals
908                          * to an array of bervals
909                          */
910                         rs.sr_err = bvptr2obj( pMod->mod_bvalues, &bv );
911                         if ( rs.sr_err != LDAP_SUCCESS )
912                                 goto cleanup;
913                         tmp.sml_type.bv_val = pMod->mod_type;
914                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
915                         tmp.sml_bvalues = bv;
916                         tmp.sml_nvalues = NULL;
917
918                         mod  = (Modifications *)ch_malloc( sizeof(Modifications) );
919
920                         mod->sml_op = pMod->mod_op;
921                         mod->sml_next = NULL;
922                         mod->sml_desc = NULL;
923                         mod->sml_type = tmp.sml_type;
924                         mod->sml_bvalues = tmp.sml_bvalues;
925                         mod->sml_nvalues = tmp.sml_nvalues;
926                 } else { 
927                         rs.sr_err = values2obj( pMod->mod_values, &bv );
928                         if ( rs.sr_err != LDAP_SUCCESS )
929                                 goto cleanup;
930                         tmp.sml_type.bv_val = pMod->mod_type;
931                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
932                         tmp.sml_bvalues = bv;
933                         tmp.sml_nvalues = NULL;
934
935                         mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
936
937                         mod->sml_op = pMod->mod_op;
938                         mod->sml_next = NULL;
939                         mod->sml_desc = NULL;
940                         mod->sml_type = tmp.sml_type;
941                         mod->sml_bvalues = tmp.sml_bvalues;
942                         mod->sml_nvalues = tmp.sml_nvalues;
943                 }
944                 *modtail = mod;
945                 modtail = &mod->sml_next;
946
947                 switch( pMod->mod_op ) {
948                 case LDAP_MOD_ADD:
949                 if ( mod->sml_bvalues == NULL ) {
950                         rs.sr_err = LDAP_PROTOCOL_ERROR;
951                         goto cleanup;
952                 }
953
954                 /* fall through */
955                 case LDAP_MOD_DELETE:
956                 case LDAP_MOD_REPLACE:
957                 break;
958
959                 default:
960                         rs.sr_err = LDAP_PROTOCOL_ERROR;
961                         goto cleanup;
962                 }
963         } 
964         *modtail = NULL;
965
966         if ( op->o_req_ndn.bv_len == 0 ) {
967                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
968                 goto cleanup;
969         }
970
971         op->oq_modify.rs_modlist = modlist;
972
973         if ( op->o_bd->be_modify ) {
974                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
975                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
976                         int update = op->o_bd->be_update_ndn.bv_len;
977                         const char *text = NULL;
978                         char textbuf[SLAP_TEXT_BUFLEN];
979                         size_t textlen = sizeof( textbuf );
980
981                         rs.sr_err = slap_mods_check( modlist, update,
982                                         &text, textbuf, textlen, NULL );
983                         if ( rs.sr_err != LDAP_SUCCESS ) {
984                                 goto cleanup;
985                         }
986
987                         if ( !repl_user ) {
988                                 rs.sr_err = slap_mods_opattrs( op, modlist,
989                                                 modtail, &text, textbuf, 
990                                                 textlen );
991                                 if ( rs.sr_err != LDAP_SUCCESS ) {
992                                         goto cleanup;
993                                 }
994                         }
995                         if ( (*op->o_bd->be_modify)( op, &rs ) == 0 ) {
996                                 if ( log_change ) {
997                                         replog( op );
998                                 }
999                         } else {
1000                                 rs.sr_err = LDAP_OTHER;
1001                         }
1002                 } else {
1003                         rs.sr_err = LDAP_REFERRAL;
1004                 }
1005         } else {
1006                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1007         }
1008
1009 cleanup:
1010
1011         if ( pPB != NULL ) 
1012                 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1013
1014         if ( dn.bv_val )
1015                 slapi_ch_free( (void **)&dn.bv_val );
1016
1017         if ( modlist != NULL )
1018                 freeMods( modlist );
1019
1020         if ( pConn != NULL ) {
1021                 pSavePB = pPB;
1022         }
1023
1024         slapiConnectionDestroy( &pConn );
1025
1026         return ( pSavePB );
1027 #else
1028         return NULL;
1029 #endif /* LDAP_SLAPI */
1030 }
1031
1032 Slapi_PBlock *
1033 slapi_search_internal_bind(
1034         char *bindDN, 
1035         char *ldn, 
1036         int scope, 
1037         char *filStr, 
1038         LDAPControl **controls, 
1039         char **attrs, 
1040         int attrsonly ) 
1041 {       
1042 #ifdef LDAP_SLAPI
1043         Connection              *c;
1044         Operation               *op = NULL;
1045         Slapi_PBlock            *ptr = NULL;            
1046         Slapi_PBlock            *pSavePB = NULL;                
1047         struct berval           dn = { 0, NULL };
1048         Filter                  *filter=NULL;
1049         struct berval           fstr = { 0, NULL };
1050         AttributeName           *an = NULL;
1051         const char              *text = NULL;
1052
1053         int                     manageDsaIt = 0; 
1054         int                     isCritical;
1055         int                     i;
1056
1057         SlapReply               rs = { REP_RESULT };
1058
1059         c = slapiConnectionInit( NULL, LDAP_REQ_SEARCH );
1060         if ( c == NULL ) {
1061                 rs.sr_err = LDAP_NO_MEMORY;
1062                 goto cleanup;
1063         }
1064
1065         op = (Operation *)c->c_pending_ops.stqh_first;
1066         ptr = (Slapi_PBlock *)op->o_pb;
1067         op->o_ctrls = controls;
1068
1069         dn.bv_val = slapi_ch_strdup(ldn);
1070         dn.bv_len = strlen(ldn);
1071
1072         rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
1073         if ( rs.sr_err != LDAP_SUCCESS ) {
1074                 goto cleanup;
1075         }
1076
1077         if ( scope != LDAP_SCOPE_BASE && 
1078                         scope != LDAP_SCOPE_ONELEVEL && 
1079                         scope != LDAP_SCOPE_SUBTREE ) {
1080                 rs.sr_err = LDAP_PROTOCOL_ERROR;
1081                 goto cleanup;
1082         }
1083
1084         filter = slapi_str2filter(filStr);
1085         if ( filter == NULL ) {
1086                 rs.sr_err = LDAP_PROTOCOL_ERROR;
1087                 goto cleanup;
1088         }
1089
1090         filter2bv( filter, &fstr );
1091
1092         for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
1093                 ; /* count the number of attributes */
1094         }
1095
1096         if (i > 0) {
1097                 an = (AttributeName *)slapi_ch_calloc( (i + 1), sizeof(AttributeName) );
1098                 for (i = 0; attrs[i] != 0; i++) {
1099                         an[i].an_desc = NULL;
1100                         an[i].an_oc = NULL;
1101                         an[i].an_name.bv_val = slapi_ch_strdup(attrs[i]);
1102                         an[i].an_name.bv_len = strlen(attrs[i]);
1103                         slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1104                 }
1105                 an[i].an_name.bv_val = NULL;
1106         }
1107
1108         memset( &rs, 0, sizeof(rs) );
1109         rs.sr_type = REP_RESULT;
1110         rs.sr_err = LDAP_SUCCESS;
1111         rs.sr_entry = NULL; /* paranoia */
1112
1113         if ( scope == LDAP_SCOPE_BASE ) {
1114                 rs.sr_entry = NULL;
1115
1116                 if ( op->o_req_ndn.bv_len == 0 ) {
1117                         rs.sr_err = root_dse_info( c, &rs.sr_entry, &rs.sr_text );
1118                 }
1119
1120                 if( rs.sr_err != LDAP_SUCCESS ) {
1121                         send_ldap_result( op, &rs );
1122                         goto cleanup;
1123                 } else if ( rs.sr_entry != NULL ) {
1124                         rs.sr_err = test_filter( op, rs.sr_entry, filter );
1125
1126                         if ( rs.sr_err == LDAP_COMPARE_TRUE ) {
1127                                 rs.sr_type = REP_SEARCH;
1128                                 rs.sr_err = LDAP_SUCCESS;
1129                                 rs.sr_attrs = an;
1130
1131                                 send_search_entry( op, &rs );
1132                         }
1133
1134                         entry_free( rs.sr_entry );
1135
1136                         rs.sr_type = REP_RESULT;
1137                         rs.sr_err = LDAP_SUCCESS;
1138
1139                         send_ldap_result( op, &rs );
1140
1141                         goto cleanup;
1142                 }
1143         }
1144
1145         if ( !op->o_req_ndn.bv_len && default_search_nbase.bv_len ) {
1146                 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
1147                 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
1148
1149                 ber_dupbv( &op->o_req_dn, &default_search_base );
1150                 ber_dupbv( &op->o_req_ndn, &default_search_nbase );
1151         }
1152
1153         if ( slapi_control_present( controls,
1154                         LDAP_CONTROL_MANAGEDSAIT, NULL, &isCritical ) ) {
1155                 manageDsaIt = 1;
1156         }
1157
1158         op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
1159         if ( op->o_bd == NULL ) {
1160                 if ( manageDsaIt == 1 ) {
1161                         rs.sr_err = LDAP_NO_SUCH_OBJECT;
1162                 } else {
1163                         rs.sr_err = LDAP_PARTIAL_RESULTS;
1164                 }
1165                 goto cleanup;
1166         } 
1167
1168         op->o_dn = c->c_dn = op->o_bd->be_rootdn;
1169         op->o_ndn = c->c_ndn = op->o_bd->be_rootndn;
1170
1171         op->oq_search.rs_scope = scope;
1172         op->oq_search.rs_deref = 0;
1173         op->oq_search.rs_slimit = LDAP_NO_LIMIT;
1174         op->oq_search.rs_tlimit = LDAP_NO_LIMIT;
1175         op->oq_search.rs_attrsonly = attrsonly;
1176         op->oq_search.rs_attrs = an;
1177         op->oq_search.rs_filter = filter;
1178         op->oq_search.rs_filterstr = fstr;
1179
1180         if ( op->o_bd->be_search ) {
1181                 if ( (*op->o_bd->be_search)( op, &rs ) != 0 ) {
1182                         rs.sr_err = LDAP_OTHER;
1183                 }
1184         } else {
1185                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1186         }
1187
1188 cleanup:
1189
1190         if ( ptr != NULL )
1191                 slapi_pblock_set( ptr, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1192
1193         if ( dn.bv_val )
1194                 slapi_ch_free( (void **)&dn.bv_val );
1195         if ( filter )
1196                 slapi_filter_free( filter, 1 );
1197         if ( fstr.bv_val )
1198                 slapi_ch_free( (void **)&fstr.bv_val );
1199         if ( an != NULL )
1200                 slapi_ch_free( (void **)&an );
1201
1202         if ( c != NULL ) {
1203                 pSavePB = ptr;
1204         }
1205
1206         slapiConnectionDestroy( &c );
1207
1208         return( pSavePB );
1209 #else
1210         return NULL;
1211 #endif /* LDAP_SLAPI */
1212 }
1213
1214 Slapi_PBlock * 
1215 slapi_search_internal(
1216         char *base,
1217         int scope,
1218         char *filStr, 
1219         LDAPControl **controls,
1220         char **attrs,
1221         int attrsonly ) 
1222 {
1223 #ifdef LDAP_SLAPI
1224         return slapi_search_internal_bind( NULL, base, scope, filStr,
1225                         controls, attrs, attrsonly );
1226 #else
1227         return NULL;
1228 #endif /* LDAP_SLAPI */
1229 }
1230