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