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