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