]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_ops.c
Fix replog sequencing - assign sequence numbers at beginning of operation,
[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-2003 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 slapiConnectionInit(
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 slapiConnectionDestroy( 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 : LDAPModToEntry 
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 Entry *
341 LDAPModToEntry(
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 = slapiConnectionInit( 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                         if ( log_change ) repstamp( op );
563                         if ( (*op->o_bd->be_delete)( op, &rs ) == 0 ) {
564                                 if ( log_change ) {
565                                         replog( op );
566                                 }
567                         } else {
568                                 rs.sr_err = LDAP_OTHER;
569                         }
570                 } else {
571                         rs.sr_err = LDAP_REFERRAL;
572                 }
573         } else {
574                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
575         }
576
577 cleanup:
578         if ( pPB != NULL ) {
579                 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
580         }
581         if ( dn.bv_val ) {
582                 slapi_ch_free( (void **)&dn.bv_val );
583         }
584         if ( pConn != NULL ) {
585                 pSavePB = pPB;
586         }
587
588         slapiConnectionDestroy( &pConn );
589
590         return (pSavePB);
591 #else
592         return NULL;
593 #endif /* LDAP_SLAPI */
594 }
595
596 #ifdef LDAP_SLAPI
597 static Slapi_PBlock * 
598 slapi_add_entry_internal_locked(
599         Slapi_Entry **e, 
600         LDAPControl **controls, 
601         int log_changes ) 
602 {
603         Connection              *pConn = NULL;
604         Operation               *op = NULL;
605         Slapi_PBlock            *pPB = NULL, *pSavePB = NULL;
606
607         int                     manageDsaIt = 0;
608         int                     isCritical;
609         SlapReply               rs = { REP_RESULT };
610
611         if ( *e == NULL ) {
612                 rs.sr_err = LDAP_PARAM_ERROR;
613                 goto cleanup;
614         }
615         
616         pConn = slapiConnectionInit( NULL, LDAP_REQ_ADD );
617         if ( pConn == NULL ) {
618                 rs.sr_err = LDAP_NO_MEMORY;
619                 goto cleanup;
620         }
621
622         if ( slapi_control_present( controls, LDAP_CONTROL_MANAGEDSAIT,
623                                 NULL, &isCritical ) ) {
624                 manageDsaIt = 1; 
625         }
626
627         op = (Operation *)pConn->c_pending_ops.stqh_first;
628         pPB = (Slapi_PBlock *)op->o_pb;
629         op->o_ctrls = controls;
630
631         op->o_bd = select_backend( &((*e)->e_nname), manageDsaIt, 0 );
632         if ( op->o_bd == NULL ) {
633                 rs.sr_err = LDAP_PARTIAL_RESULTS;
634                 goto cleanup;
635         }
636
637         op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
638         op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
639         op->oq_add.rs_e = *e;
640
641         if ( op->o_bd->be_add ) {
642                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
643                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
644                         if ( log_changes ) repstamp( op );
645                         if ( (*op->o_bd->be_add)( op, &rs ) == 0 ) {
646                                 if ( log_changes ) {
647                                         replog( op );
648                                 }
649                                 be_entry_release_w( op, *e );
650                                 *e = NULL;
651                         }
652                 } else {
653                         rs.sr_err = LDAP_REFERRAL;
654                 }
655         } else {
656                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
657         }
658
659 cleanup:
660
661         if ( pPB != NULL ) {
662                 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
663         }
664
665         if ( pConn != NULL ) {
666                 pSavePB = pPB;
667         }
668
669         slapiConnectionDestroy( &pConn );
670
671         return( pSavePB );
672 }
673 #endif /* LDAP_SLAPI */
674
675 Slapi_PBlock * 
676 slapi_add_entry_internal(
677         Slapi_Entry *e, 
678         LDAPControl **controls, 
679         int log_changes ) 
680 {
681 #ifdef LDAP_SLAPI
682         Slapi_PBlock *pb;
683         Slapi_Entry *entry;
684
685         /*
686          * We make a copy to avoid an entry that may be freed later
687          * by the caller being placed in the cache.
688          */
689         entry = slapi_entry_dup( e );
690         pb = slapi_add_entry_internal_locked( &entry, controls, log_changes );
691         if ( entry != NULL ) {
692                 slapi_entry_free( entry );
693         }
694         return pb;
695 #else
696         return NULL;
697 #endif
698 }
699
700 Slapi_PBlock *
701 slapi_add_internal(
702         char *dn, 
703         LDAPMod **mods, 
704         LDAPControl **controls, 
705         int log_changes  ) 
706 {
707 #ifdef LDAP_SLAPI
708         LDAPMod                 *pMod = NULL;
709         Slapi_PBlock            *pb = NULL;
710         Entry                   *pEntry = NULL;
711         int                     i, rc = LDAP_SUCCESS;
712
713         if ( mods == NULL || *mods == NULL || dn == NULL || *dn == '\0' ) {
714                 rc = LDAP_PARAM_ERROR ;
715         }
716
717         if ( rc == LDAP_SUCCESS ) {
718                 for ( i = 0, pMod = mods[0]; pMod != NULL; pMod = mods[++i] ) {
719                         if ( (pMod->mod_op & LDAP_MOD_OP ) != LDAP_MOD_ADD ) {
720                                 rc = LDAP_OTHER;
721                                 break;
722                         }
723                 }
724         }
725
726         if ( rc == LDAP_SUCCESS ) {
727                 pEntry = LDAPModToEntry( dn, mods );
728                 if ( pEntry == NULL ) {
729                         rc = LDAP_OTHER;
730                 }
731         }
732
733         if ( rc != LDAP_SUCCESS ) {
734                 pb = slapi_pblock_new();
735                 slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_RESULT, (void *)rc );
736         } else {
737                 pb = slapi_add_entry_internal_locked( &pEntry, controls, log_changes );
738         }
739
740         if ( pEntry != NULL ) {
741                 slapi_entry_free(pEntry);
742         }
743
744         return(pb);
745 #else
746         return NULL;
747 #endif /* LDAP_SLAPI */
748 }
749
750 /* Function : slapi_modrdn_internal
751  *
752  * Description : Plugin functions call this routine to modify the rdn 
753  *                               of an entry in the backend directly
754  * Return values : LDAP_SUCCESS
755  *                 LDAP_PARAM_ERROR
756  *                 LDAP_NO_MEMORY
757  *                 LDAP_OTHER
758  *                 LDAP_UNWILLING_TO_PERFORM
759  *
760  * NOTE: This function does not support the "newSuperior" option from LDAP V3.
761  */
762 Slapi_PBlock *
763 slapi_modrdn_internal(
764         char *olddn, 
765         char *lnewrdn, 
766         int deloldrdn, 
767         LDAPControl **controls, 
768         int log_change )
769 {
770 #ifdef LDAP_SLAPI
771         struct berval           dn = { 0, NULL };
772         struct berval           newrdn = { 0, NULL };
773         Connection              *pConn = NULL;
774         Operation               *op = NULL;
775         Slapi_PBlock            *pPB = NULL;
776         Slapi_PBlock            *pSavePB = NULL;
777         int                     manageDsaIt = 0;
778         int                     isCritical;
779         SlapReply               rs = { REP_RESULT };
780
781         pConn = slapiConnectionInit( NULL,  LDAP_REQ_MODRDN);
782         if ( pConn == NULL) {
783                 rs.sr_err = LDAP_NO_MEMORY;
784                 goto cleanup;
785         }
786
787         op = (Operation *)pConn->c_pending_ops.stqh_first;
788         pPB = (Slapi_PBlock *)op->o_pb;
789         op->o_ctrls = controls;
790
791         if ( slapi_control_present( controls, 
792                         SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
793                 manageDsaIt = 1;
794         }
795
796         op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
797         if ( op->o_bd == NULL ) {
798                 rs.sr_err =  LDAP_PARTIAL_RESULTS;
799                 goto cleanup;
800         }
801
802         op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
803         op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
804
805         dn.bv_val = slapi_ch_strdup( olddn );
806         dn.bv_len = strlen( olddn );
807
808         rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
809         if ( rs.sr_err != LDAP_SUCCESS ) {
810                 goto cleanup;
811         }
812
813         if ( op->o_req_dn.bv_len == 0 ) {
814                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
815                 goto cleanup;
816         }
817
818         newrdn.bv_val = slapi_ch_strdup( lnewrdn );
819         newrdn.bv_len = strlen( lnewrdn );
820
821         rs.sr_err = dnPrettyNormal( NULL, &newrdn, &op->oq_modrdn.rs_newrdn, &op->oq_modrdn.rs_nnewrdn, NULL );
822         if ( rs.sr_err != LDAP_SUCCESS ) {
823                 goto cleanup;
824         }
825
826         if ( rdnValidate( &op->oq_modrdn.rs_nnewrdn ) != LDAP_SUCCESS ) {
827                 goto cleanup;
828         }
829
830         op->oq_modrdn.rs_newSup = NULL;
831         op->oq_modrdn.rs_nnewSup = NULL;
832         op->oq_modrdn.rs_deleteoldrdn = deloldrdn;
833
834         if ( op->o_bd->be_modrdn ) {
835                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
836                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
837                         if ( log_change ) repstamp( op );
838                         if ( (*op->o_bd->be_modrdn)( op, &rs ) == 0 ) {
839                                 if ( log_change ) {
840                                         replog( op );
841                                 }
842                         } else {
843                                 rs.sr_err = LDAP_OTHER;
844                         }
845                 } else {
846                         rs.sr_err = LDAP_REFERRAL;
847                 }
848         } else {
849                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
850         }
851
852 cleanup:
853
854         if ( pPB != NULL ) {
855                 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
856         }
857         
858         if ( dn.bv_val )
859                 slapi_ch_free( (void **)&dn.bv_val );
860
861         if ( newrdn.bv_val )
862                 slapi_ch_free( (void **)&newrdn.bv_val );
863         if ( op->oq_modrdn.rs_newrdn.bv_val )
864                 slapi_ch_free( (void **)&op->oq_modrdn.rs_newrdn.bv_val );
865         if ( op->oq_modrdn.rs_nnewrdn.bv_val )
866                 slapi_ch_free( (void **)&op->oq_modrdn.rs_nnewrdn.bv_val );
867
868         if ( pConn != NULL ) {
869                 pSavePB = pPB;
870         }
871
872         slapiConnectionDestroy( &pConn );
873
874         return( pSavePB );
875 #else
876         return NULL;
877 #endif /* LDAP_SLAPI */
878 }
879
880 /* Function : slapi_modify_internal
881  *
882  * Description: Plugin functions call this routine to modify an entry 
883  *                              in the backend directly
884  * Return values : LDAP_SUCCESS
885  *                 LDAP_PARAM_ERROR
886  *                 LDAP_NO_MEMORY
887  *                 LDAP_OTHER
888  *                 LDAP_UNWILLING_TO_PERFORM
889 */
890 Slapi_PBlock *
891 slapi_modify_internal(
892         char *ldn,      
893         LDAPMod **mods, 
894         LDAPControl **controls, 
895         int log_change )
896 {
897 #ifdef LDAP_SLAPI
898         int                     i;
899         Connection              *pConn = NULL;
900         Operation               *op = NULL;
901         Slapi_PBlock            *pPB = NULL;
902         Slapi_PBlock            *pSavePB = NULL;
903
904         struct berval dn = { 0, NULL };
905
906         int                     manageDsaIt = 0;
907         int                     isCritical;
908         struct berval           *bv;
909         LDAPMod                 *pMod;
910
911         Modifications           *modlist = NULL;
912         Modifications           **modtail = &modlist;
913         Modifications           tmp;
914
915         SlapReply               rs = { REP_RESULT };
916
917         if ( mods == NULL || *mods == NULL || ldn == NULL ) {
918                 rs.sr_err = LDAP_PARAM_ERROR ;
919                 goto cleanup;
920         }
921
922         pConn = slapiConnectionInit( NULL,  LDAP_REQ_MODIFY );
923         if ( pConn == NULL ) {
924                 rs.sr_err = LDAP_NO_MEMORY;
925                 goto cleanup;
926         }
927
928         op = (Operation *)pConn->c_pending_ops.stqh_first;
929         pPB = (Slapi_PBlock *)op->o_pb;
930         op->o_ctrls = controls;
931
932         dn.bv_val = slapi_ch_strdup( ldn );
933         dn.bv_len = strlen( ldn );
934         rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
935         if ( rs.sr_err != LDAP_SUCCESS ) {
936                 goto cleanup;
937         }
938
939         if ( slapi_control_present( controls, 
940                         SLAPI_CONTROL_MANAGEDSAIT_OID, NULL, &isCritical ) ) {
941                 manageDsaIt = 1;
942         }
943
944         op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
945         if ( op->o_bd == NULL ) {
946                 rs.sr_err = LDAP_PARTIAL_RESULTS;
947                 goto cleanup;
948         }
949
950         op->o_dn = pConn->c_dn = op->o_bd->be_rootdn;
951         op->o_ndn = pConn->c_ndn = op->o_bd->be_rootndn;
952
953         for ( i = 0, pMod = mods[0];
954                 rs.sr_err == LDAP_SUCCESS && pMod != NULL; 
955                 pMod = mods[++i] )
956         {
957                 Modifications *mod;
958
959                 if ( (pMod->mod_op & LDAP_MOD_BVALUES) != 0 ) {
960                         /*
961                          * attr values are in berval format
962                          * convert an array of pointers to bervals
963                          * to an array of bervals
964                          */
965                         rs.sr_err = bvptr2obj_copy( pMod->mod_bvalues, &bv );
966                         if ( rs.sr_err != LDAP_SUCCESS )
967                                 goto cleanup;
968                         tmp.sml_type.bv_val = pMod->mod_type;
969                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
970                         tmp.sml_bvalues = bv;
971                         tmp.sml_nvalues = NULL;
972
973                         mod  = (Modifications *)ch_malloc( sizeof(Modifications) );
974
975                         mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
976                         mod->sml_next = NULL;
977                         mod->sml_desc = NULL;
978                         mod->sml_type = tmp.sml_type;
979                         mod->sml_bvalues = tmp.sml_bvalues;
980                         mod->sml_nvalues = tmp.sml_nvalues;
981                 } else { 
982                         rs.sr_err = values2obj_copy( pMod->mod_values, &bv );
983                         if ( rs.sr_err != LDAP_SUCCESS )
984                                 goto cleanup;
985                         tmp.sml_type.bv_val = pMod->mod_type;
986                         tmp.sml_type.bv_len = strlen( pMod->mod_type );
987                         tmp.sml_bvalues = bv;
988                         tmp.sml_nvalues = NULL;
989
990                         mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
991
992                         mod->sml_op = pMod->mod_op & LDAP_MOD_OP;
993                         mod->sml_next = NULL;
994                         mod->sml_desc = NULL;
995                         mod->sml_type = tmp.sml_type;
996                         mod->sml_bvalues = tmp.sml_bvalues;
997                         mod->sml_nvalues = tmp.sml_nvalues;
998                 }
999                 *modtail = mod;
1000                 modtail = &mod->sml_next;
1001
1002                 switch( pMod->mod_op & LDAP_MOD_OP ) {
1003                 case LDAP_MOD_ADD:
1004                 if ( mod->sml_bvalues == NULL ) {
1005                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1006                         goto cleanup;
1007                 }
1008
1009                 /* fall through */
1010                 case LDAP_MOD_DELETE:
1011                 case LDAP_MOD_REPLACE:
1012                 break;
1013
1014                 default:
1015                         rs.sr_err = LDAP_PROTOCOL_ERROR;
1016                         goto cleanup;
1017                 }
1018         } 
1019         *modtail = NULL;
1020
1021         if ( op->o_req_ndn.bv_len == 0 ) {
1022                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1023                 goto cleanup;
1024         }
1025
1026         op->oq_modify.rs_modlist = modlist;
1027
1028         if ( op->o_bd->be_modify ) {
1029                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
1030                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user ) {
1031                         int update = op->o_bd->be_update_ndn.bv_len;
1032                         const char *text = NULL;
1033                         char textbuf[SLAP_TEXT_BUFLEN];
1034                         size_t textlen = sizeof( textbuf );
1035
1036                         rs.sr_err = slap_mods_check( modlist, update,
1037                                         &text, textbuf, textlen, NULL );
1038                         if ( rs.sr_err != LDAP_SUCCESS ) {
1039                                 goto cleanup;
1040                         }
1041
1042                         if ( !repl_user ) {
1043                                 rs.sr_err = slap_mods_opattrs( op, modlist,
1044                                                 modtail, &text, textbuf, 
1045                                                 textlen );
1046                                 if ( rs.sr_err != LDAP_SUCCESS ) {
1047                                         goto cleanup;
1048                                 }
1049                         }
1050                         if ( log_change ) repstamp( op );
1051                         if ( (*op->o_bd->be_modify)( op, &rs ) == 0 ) {
1052                                 if ( log_change ) {
1053                                         replog( op );
1054                                 }
1055                         } else {
1056                                 rs.sr_err = LDAP_OTHER;
1057                         }
1058                 } else {
1059                         rs.sr_err = LDAP_REFERRAL;
1060                 }
1061         } else {
1062                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1063         }
1064
1065 cleanup:
1066
1067         if ( pPB != NULL ) 
1068                 slapi_pblock_set( pPB, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1069
1070         if ( dn.bv_val )
1071                 slapi_ch_free( (void **)&dn.bv_val );
1072
1073         if ( modlist != NULL )
1074                 slap_mods_free( modlist );
1075
1076         if ( pConn != NULL ) {
1077                 pSavePB = pPB;
1078         }
1079
1080         slapiConnectionDestroy( &pConn );
1081
1082         return ( pSavePB );
1083 #else
1084         return NULL;
1085 #endif /* LDAP_SLAPI */
1086 }
1087
1088 Slapi_PBlock *
1089 slapi_search_internal_bind(
1090         char *bindDN, 
1091         char *ldn, 
1092         int scope, 
1093         char *filStr, 
1094         LDAPControl **controls, 
1095         char **attrs, 
1096         int attrsonly ) 
1097 {       
1098 #ifdef LDAP_SLAPI
1099         Connection              *c;
1100         Operation               *op = NULL;
1101         Slapi_PBlock            *ptr = NULL;            
1102         Slapi_PBlock            *pSavePB = NULL;                
1103         struct berval           dn = { 0, NULL };
1104         Filter                  *filter=NULL;
1105         struct berval           fstr = { 0, NULL };
1106         AttributeName           *an = NULL;
1107         const char              *text = NULL;
1108
1109         int                     manageDsaIt = 0; 
1110         int                     isCritical;
1111         int                     i;
1112
1113         SlapReply               rs = { REP_RESULT };
1114
1115         c = slapiConnectionInit( NULL, LDAP_REQ_SEARCH );
1116         if ( c == NULL ) {
1117                 rs.sr_err = LDAP_NO_MEMORY;
1118                 goto cleanup;
1119         }
1120
1121         op = (Operation *)c->c_pending_ops.stqh_first;
1122         ptr = (Slapi_PBlock *)op->o_pb;
1123         op->o_ctrls = controls;
1124
1125         if ( ldn != NULL ) {
1126                 dn.bv_val = slapi_ch_strdup(ldn);
1127                 dn.bv_len = strlen(ldn);
1128         }
1129
1130         rs.sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, NULL );
1131         if ( rs.sr_err != LDAP_SUCCESS ) {
1132                 goto cleanup;
1133         }
1134
1135         if ( scope != LDAP_SCOPE_BASE && 
1136                         scope != LDAP_SCOPE_ONELEVEL && 
1137                         scope != LDAP_SCOPE_SUBTREE ) {
1138                 rs.sr_err = LDAP_PROTOCOL_ERROR;
1139                 goto cleanup;
1140         }
1141
1142         filter = slapi_str2filter(filStr);
1143         if ( filter == NULL ) {
1144                 rs.sr_err = LDAP_PROTOCOL_ERROR;
1145                 goto cleanup;
1146         }
1147
1148         filter2bv( filter, &fstr );
1149
1150         for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ ) {
1151                 ; /* count the number of attributes */
1152         }
1153
1154         if (i > 0) {
1155                 an = (AttributeName *)slapi_ch_calloc( (i + 1), sizeof(AttributeName) );
1156                 for (i = 0; attrs[i] != 0; i++) {
1157                         an[i].an_desc = NULL;
1158                         an[i].an_oc = NULL;
1159                         an[i].an_oc_exclude = 0;
1160                         an[i].an_name.bv_val = slapi_ch_strdup(attrs[i]);
1161                         an[i].an_name.bv_len = strlen(attrs[i]);
1162                         slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1163                 }
1164                 an[i].an_name.bv_val = NULL;
1165         }
1166
1167         memset( &rs, 0, sizeof(rs) );
1168         rs.sr_type = REP_RESULT;
1169         rs.sr_err = LDAP_SUCCESS;
1170         rs.sr_entry = NULL; /* paranoia */
1171
1172         if ( scope == LDAP_SCOPE_BASE ) {
1173                 rs.sr_entry = NULL;
1174
1175                 if ( op->o_req_ndn.bv_len == 0 ) {
1176                         rs.sr_err = root_dse_info( c, &rs.sr_entry, &rs.sr_text );
1177                 }
1178
1179                 if( rs.sr_err != LDAP_SUCCESS ) {
1180                         send_ldap_result( op, &rs );
1181                         goto cleanup;
1182                 } else if ( rs.sr_entry != NULL ) {
1183                         rs.sr_err = test_filter( op, rs.sr_entry, filter );
1184
1185                         if ( rs.sr_err == LDAP_COMPARE_TRUE ) {
1186                                 rs.sr_type = REP_SEARCH;
1187                                 rs.sr_err = LDAP_SUCCESS;
1188                                 rs.sr_attrs = an;
1189
1190                                 send_search_entry( op, &rs );
1191                         }
1192
1193                         entry_free( rs.sr_entry );
1194
1195                         rs.sr_type = REP_RESULT;
1196                         rs.sr_err = LDAP_SUCCESS;
1197
1198                         send_ldap_result( op, &rs );
1199
1200                         goto cleanup;
1201                 }
1202         }
1203
1204         if ( !op->o_req_ndn.bv_len && default_search_nbase.bv_len ) {
1205                 slapi_ch_free( (void **)&op->o_req_dn.bv_val );
1206                 slapi_ch_free( (void **)&op->o_req_ndn.bv_val );
1207
1208                 ber_dupbv( &op->o_req_dn, &default_search_base );
1209                 ber_dupbv( &op->o_req_ndn, &default_search_nbase );
1210         }
1211
1212         if ( slapi_control_present( controls,
1213                         LDAP_CONTROL_MANAGEDSAIT, NULL, &isCritical ) ) {
1214                 manageDsaIt = 1;
1215         }
1216
1217         op->o_bd = select_backend( &op->o_req_ndn, manageDsaIt, 0 );
1218         if ( op->o_bd == NULL ) {
1219                 if ( manageDsaIt == 1 ) {
1220                         rs.sr_err = LDAP_NO_SUCH_OBJECT;
1221                 } else {
1222                         rs.sr_err = LDAP_PARTIAL_RESULTS;
1223                 }
1224                 goto cleanup;
1225         } 
1226
1227         op->o_dn = c->c_dn = op->o_bd->be_rootdn;
1228         op->o_ndn = c->c_ndn = op->o_bd->be_rootndn;
1229
1230         op->oq_search.rs_scope = scope;
1231         op->oq_search.rs_deref = 0;
1232         op->oq_search.rs_slimit = LDAP_NO_LIMIT;
1233         op->oq_search.rs_tlimit = LDAP_NO_LIMIT;
1234         op->oq_search.rs_attrsonly = attrsonly;
1235         op->oq_search.rs_attrs = an;
1236         op->oq_search.rs_filter = filter;
1237         op->oq_search.rs_filterstr = fstr;
1238
1239         if ( op->o_bd->be_search ) {
1240                 if ( (*op->o_bd->be_search)( op, &rs ) != 0 ) {
1241                         rs.sr_err = LDAP_OTHER;
1242                 }
1243         } else {
1244                 rs.sr_err = LDAP_UNWILLING_TO_PERFORM;
1245         }
1246
1247 cleanup:
1248
1249         if ( ptr != NULL )
1250                 slapi_pblock_set( ptr, SLAPI_PLUGIN_INTOP_RESULT, (void *)rs.sr_err );
1251
1252         if ( dn.bv_val )
1253                 slapi_ch_free( (void **)&dn.bv_val );
1254         if ( filter )
1255                 slapi_filter_free( filter, 1 );
1256         if ( fstr.bv_val )
1257                 slapi_ch_free( (void **)&fstr.bv_val );
1258         if ( an != NULL )
1259                 slapi_ch_free( (void **)&an );
1260
1261         if ( c != NULL ) {
1262                 pSavePB = ptr;
1263         }
1264
1265         slapiConnectionDestroy( &c );
1266
1267         return( pSavePB );
1268 #else
1269         return NULL;
1270 #endif /* LDAP_SLAPI */
1271 }
1272
1273 Slapi_PBlock * 
1274 slapi_search_internal(
1275         char *base,
1276         int scope,
1277         char *filStr, 
1278         LDAPControl **controls,
1279         char **attrs,
1280         int attrsonly ) 
1281 {
1282 #ifdef LDAP_SLAPI
1283         return slapi_search_internal_bind( NULL, base, scope, filStr,
1284                         controls, attrs, attrsonly );
1285 #else
1286         return NULL;
1287 #endif /* LDAP_SLAPI */
1288 }
1289