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