]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
7fb2c42eddb1f826c133b5a1f4db4b8eac2a9d7e
[openldap] / servers / slapd / backend.c
1 /* backend.c - routines for dealing with back-end databases */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
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 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27
28 #include "portable.h"
29
30 #include <stdio.h>
31
32 #include <ac/string.h>
33 #include <ac/socket.h>
34 #include <sys/stat.h>
35
36 #include "slap.h"
37 #include "lutil.h"
38 #include "lber_pvt.h"
39
40 #include "ldap_rq.h"
41
42 #ifdef LDAP_SLAPI
43 #include "slapi/slapi.h"
44
45 static void init_group_pblock( Operation *op, Entry *target,
46         Entry *e, struct berval *op_ndn, AttributeDescription *group_at );
47 static int call_group_preop_plugins( Operation *op );
48 static void call_group_postop_plugins( Operation *op );
49 #endif /* LDAP_SLAPI */
50
51 /*
52  * If a module is configured as dynamic, its header should not
53  * get included into slapd. While this is a general rule and does
54  * not have much of an effect in UNIX, this rule should be adhered
55  * to for Windows, where dynamic object code should not be implicitly
56  * imported into slapd without appropriate __declspec(dllimport) directives.
57  */
58
59 int                     nBackendInfo = 0;
60 BackendInfo             *backendInfo = NULL;
61
62 int                     nBackendDB = 0; 
63 BackendDB               *backendDB = NULL;
64
65 ldap_pvt_thread_pool_t  syncrepl_pool;
66 int                     syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
67
68 int backend_init(void)
69 {
70         int rc = -1;
71
72         ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
73
74         if((nBackendInfo != 0) || (backendInfo != NULL)) {
75                 /* already initialized */
76                 Debug( LDAP_DEBUG_ANY,
77                         "backend_init: already initialized.\n", 0, 0, 0 );
78                 return -1;
79         }
80
81         for( ;
82                 slap_binfo[nBackendInfo].bi_type != NULL;
83                 nBackendInfo++ )
84         {
85                 assert( slap_binfo[nBackendInfo].bi_init );
86
87                 rc = slap_binfo[nBackendInfo].bi_init( &slap_binfo[nBackendInfo] );
88
89                 if(rc != 0) {
90                         Debug( LDAP_DEBUG_ANY,
91                                 "backend_init: initialized for type \"%s\"\n",
92                                 slap_binfo[nBackendInfo].bi_type, 0, 0 );
93                         /* destroy those we've already inited */
94                         for( nBackendInfo--;
95                                 nBackendInfo >= 0 ;
96                                 nBackendInfo-- )
97                         { 
98                                 if ( slap_binfo[nBackendInfo].bi_destroy ) {
99                                         slap_binfo[nBackendInfo].bi_destroy(
100                                                 &slap_binfo[nBackendInfo] );
101                                 }
102                         }
103                         return rc;
104                 }
105         }
106
107         if ( nBackendInfo > 0) {
108                 backendInfo = slap_binfo;
109                 return 0;
110         }
111
112 #ifdef SLAPD_MODULES    
113         return 0;
114 #else
115
116         Debug( LDAP_DEBUG_ANY,
117                 "backend_init: failed\n",
118                 0, 0, 0 );
119
120         return rc;
121 #endif /* SLAPD_MODULES */
122 }
123
124 int backend_add(BackendInfo *aBackendInfo)
125 {
126         int rc = 0;
127
128         if ( aBackendInfo->bi_init == NULL ) {
129                 Debug( LDAP_DEBUG_ANY, "backend_add: "
130                         "backend type \"%s\" does not have the (mandatory)init function\n",
131                         aBackendInfo->bi_type, 0, 0 );
132                 return -1;
133         }
134
135    if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
136                 Debug( LDAP_DEBUG_ANY,
137                         "backend_add:  initialization for type \"%s\" failed\n",
138                         aBackendInfo->bi_type, 0, 0 );
139                 return rc;
140    }
141
142         /* now add the backend type to the Backend Info List */
143         {
144                 BackendInfo *newBackendInfo = 0;
145
146                 /* if backendInfo == slap_binfo no deallocation of old backendInfo */
147                 if (backendInfo == slap_binfo) {
148                         newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
149                         AC_MEMCPY(newBackendInfo, backendInfo,
150                                 sizeof(BackendInfo) * nBackendInfo);
151                 } else {
152                         newBackendInfo = ch_realloc(backendInfo,
153                                 sizeof(BackendInfo) * (nBackendInfo + 1));
154                 }
155
156                 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
157                         sizeof(BackendInfo));
158                 backendInfo = newBackendInfo;
159                 nBackendInfo++;
160                 return 0;
161         }
162 }
163
164 /* startup a specific backend database */
165 int backend_startup_one(Backend *be)
166 {
167         int             rc = 0;
168         BackendInfo     *bi = be->bd_info;
169
170         assert(be);
171
172         be->be_pending_csn_list = (struct be_pcl *)
173                 ch_calloc( 1, sizeof( struct be_pcl ));
174
175         LDAP_TAILQ_INIT( be->be_pending_csn_list );
176
177         /* back-relay takes care of itself; so may do other */
178         if ( be->be_controls == NULL ) {
179                 if ( overlay_is_over( be ) ) {
180                         bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
181                 }
182
183                 if ( bi->bi_controls ) {
184                         be->be_controls = ldap_charray_dup( bi->bi_controls );
185                 }
186         }
187
188         Debug( LDAP_DEBUG_TRACE,
189                 "backend_startup: starting \"%s\"\n",
190                 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
191                 0, 0 );
192         if ( be->bd_info->bi_db_open ) {
193                 rc = be->bd_info->bi_db_open( be );
194                 if ( rc != 0 ) {
195                         Debug( LDAP_DEBUG_ANY,
196                                 "backend_startup: bi_db_open failed! (%d)\n",
197                                 rc, 0, 0 );
198                 }
199         }
200
201         /* back-relay takes care of itself; so may do other */
202         bi = be->bd_info;
203         if ( overlay_is_over( be ) ) {
204                 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
205         }
206
207         if ( bi->bi_controls ) {
208                 if ( be->be_controls == NULL ) {
209                         be->be_controls = ldap_charray_dup( bi->bi_controls );
210
211                 } else {
212                         int     i;
213
214                         /* maybe not efficient, but it's startup and few dozens of controls... */
215                         for ( i = 0; bi->bi_controls[ i ]; i++ ) {
216                                 if ( !ldap_charray_inlist( be->be_controls, bi->bi_controls[ i ] ) ) {
217                                         rc = ldap_charray_add( &be->be_controls, bi->bi_controls[ i ] );
218                                         if ( rc != 0 ) {
219                                                 break;
220                                         }
221                                 }
222                         }
223                 }
224         }
225
226         return rc;
227 }
228
229 int backend_startup(Backend *be)
230 {
231         int i;
232         int rc = 0;
233
234         if( ! ( nBackendDB > 0 ) ) {
235                 /* no databases */
236                 Debug( LDAP_DEBUG_ANY,
237                         "backend_startup: %d databases to startup.\n",
238                         nBackendDB, 0, 0 );
239                 return 1;
240         }
241
242         if(be != NULL) {
243                 if ( be->bd_info->bi_open ) {
244                         rc = be->bd_info->bi_open( be->bd_info );
245                         if ( rc != 0 ) {
246                                 Debug( LDAP_DEBUG_ANY,
247                                         "backend_startup: bi_open failed!\n",
248                                         0, 0, 0 );
249
250                                 return rc;
251                         }
252                 }
253
254                 return backend_startup_one( be );
255         }
256
257         /* open frontend, if required */
258         if ( frontendDB->bd_info->bi_db_open ) {
259                 rc = frontendDB->bd_info->bi_db_open( frontendDB );
260                 if ( rc != 0 ) {
261                         Debug( LDAP_DEBUG_ANY,
262                                 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
263                                 rc, 0, 0 );
264                         return rc;
265                 }
266         }
267
268         /* open each backend type */
269         for( i = 0; i < nBackendInfo; i++ ) {
270                 if( backendInfo[i].bi_nDB == 0) {
271                         /* no database of this type, don't open */
272                         continue;
273                 }
274
275                 if( backendInfo[i].bi_open ) {
276                         rc = backendInfo[i].bi_open(
277                                 &backendInfo[i] );
278                         if ( rc != 0 ) {
279                                 Debug( LDAP_DEBUG_ANY,
280                                         "backend_startup: bi_open %d failed!\n",
281                                         i, 0, 0 );
282                                 return rc;
283                         }
284                 }
285         }
286
287         ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
288         LDAP_STAILQ_INIT( &slapd_rq.task_list );
289         LDAP_STAILQ_INIT( &slapd_rq.run_list );
290
291         /* open each backend database */
292         for( i = 0; i < nBackendDB; i++ ) {
293                 if ( backendDB[i].be_suffix == NULL ) {
294                         Debug( LDAP_DEBUG_ANY,
295                                 "backend_startup: warning, database %d (%s) "
296                                 "has no suffix\n",
297                                 i, backendDB[i].bd_info->bi_type, 0 );
298                 }
299                 /* append global access controls */
300                 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
301
302                 rc = backend_startup_one( &backendDB[i] );
303
304                 if ( rc ) return rc;
305
306
307                 if ( backendDB[i].be_syncinfo ) {
308                         syncinfo_t *si;
309
310                         if ( !( backendDB[i].be_search && backendDB[i].be_add &&
311                                 backendDB[i].be_modify && backendDB[i].be_delete )) {
312                                 Debug( LDAP_DEBUG_ANY,
313                                         "backend_startup: database(%d) does not support "
314                                         "operations required for syncrepl", i, 0, 0 );
315                                 continue;
316                         }
317
318                         {
319                                 si = backendDB[i].be_syncinfo;
320                                 si->si_be = &backendDB[i];
321                                 init_syncrepl( si );
322                                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
323                                 ldap_pvt_runqueue_insert( &slapd_rq,
324                                                 si->si_interval, do_syncrepl, (void *) si );
325                                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
326                         }
327                 }
328         }
329
330         return rc;
331 }
332
333 int backend_num( Backend *be )
334 {
335         int i;
336
337         if( be == NULL ) return -1;
338
339         for( i = 0; i < nBackendDB; i++ ) {
340                 if( be == &backendDB[i] ) return i;
341         }
342         return -1;
343 }
344
345 int backend_shutdown( Backend *be )
346 {
347         int i;
348         int rc = 0;
349
350         if( be != NULL ) {
351                 /* shutdown a specific backend database */
352
353                 if ( be->bd_info->bi_nDB == 0 ) {
354                         /* no database of this type, we never opened it */
355                         return 0;
356                 }
357
358                 if ( be->bd_info->bi_db_close ) {
359                         be->bd_info->bi_db_close( be );
360                 }
361
362                 if( be->bd_info->bi_close ) {
363                         be->bd_info->bi_close( be->bd_info );
364                 }
365
366                 return 0;
367         }
368
369         /* close each backend database */
370         for( i = 0; i < nBackendDB; i++ ) {
371                 if ( backendDB[i].bd_info->bi_db_close ) {
372                         backendDB[i].bd_info->bi_db_close(
373                                 &backendDB[i] );
374                 }
375
376                 if(rc != 0) {
377                         Debug( LDAP_DEBUG_ANY,
378                                 "backend_close: bi_db_close %s failed!\n",
379                                 backendDB[i].be_type, 0, 0 );
380                 }
381         }
382
383         /* close each backend type */
384         for( i = 0; i < nBackendInfo; i++ ) {
385                 if( backendInfo[i].bi_nDB == 0 ) {
386                         /* no database of this type */
387                         continue;
388                 }
389
390                 if( backendInfo[i].bi_close ) {
391                         backendInfo[i].bi_close(
392                                 &backendInfo[i] );
393                 }
394         }
395
396         /* close frontend, if required */
397         if ( frontendDB->bd_info->bi_db_close ) {
398                 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
399                 if ( rc != 0 ) {
400                         Debug( LDAP_DEBUG_ANY,
401                                 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
402                                 rc, 0, 0 );
403                 }
404         }
405
406         return 0;
407 }
408
409 int backend_destroy(void)
410 {
411         int i;
412         BackendDB *bd;
413         struct slap_csn_entry *csne;
414
415         ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
416
417         /* destroy each backend database */
418         for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
419
420                 if ( bd->be_syncinfo ) {
421                         syncinfo_free( bd->be_syncinfo );
422                 }
423
424                 if ( bd->be_pending_csn_list ) {
425                         csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
426                         while ( csne ) {
427                                 struct slap_csn_entry *tmp_csne = csne;
428
429                                 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
430                                 ch_free( csne->ce_csn.bv_val );
431                                 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
432                                 ch_free( tmp_csne );
433                         }
434                 }
435                 
436                 if ( bd->bd_info->bi_db_destroy ) {
437                         bd->bd_info->bi_db_destroy( bd );
438                 }
439                 ber_bvarray_free( bd->be_suffix );
440                 ber_bvarray_free( bd->be_nsuffix );
441                 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
442                         free( bd->be_rootdn.bv_val );
443                 }
444                 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
445                         free( bd->be_rootndn.bv_val );
446                 }
447                 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
448                         free( bd->be_rootpw.bv_val );
449                 }
450                 acl_destroy( bd->be_acl, frontendDB->be_acl );
451                 if ( bd->be_controls ) {
452                         ldap_charray_free( bd->be_controls );
453                 }
454         }
455         free( backendDB );
456
457         /* destroy each backend type */
458         for( i = 0; i < nBackendInfo; i++ ) {
459                 if( backendInfo[i].bi_destroy ) {
460                         backendInfo[i].bi_destroy(
461                                 &backendInfo[i] );
462                 }
463         }
464
465 #ifdef SLAPD_MODULES
466         if (backendInfo != slap_binfo) {
467            free(backendInfo);
468         }
469 #endif /* SLAPD_MODULES */
470
471         nBackendInfo = 0;
472         backendInfo = NULL;
473
474         /* destroy frontend database */
475         bd = frontendDB;
476         if ( bd ) {
477                 if ( bd->bd_info->bi_db_destroy ) {
478                         bd->bd_info->bi_db_destroy( bd );
479                 }
480                 ber_bvarray_free( bd->be_suffix );
481                 ber_bvarray_free( bd->be_nsuffix );
482                 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
483                         free( bd->be_rootdn.bv_val );
484                 }
485                 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
486                         free( bd->be_rootndn.bv_val );
487                 }
488                 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
489                         free( bd->be_rootpw.bv_val );
490                 }
491                 acl_destroy( bd->be_acl, frontendDB->be_acl );
492         }
493
494         return 0;
495 }
496
497 BackendInfo* backend_info(const char *type)
498 {
499         int i;
500
501         /* search for the backend type */
502         for( i = 0; i < nBackendInfo; i++ ) {
503                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
504                         return &backendInfo[i];
505                 }
506         }
507
508         return NULL;
509 }
510
511
512 BackendDB *
513 backend_db_init(
514     const char  *type )
515 {
516         Backend *be;
517         BackendInfo *bi = backend_info(type);
518         int     rc = 0;
519
520         if( bi == NULL ) {
521                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
522                 return NULL;
523         }
524
525         be = backendDB;
526
527         backendDB = (BackendDB *) ch_realloc(
528                         (char *) backendDB,
529                     (nBackendDB + 1) * sizeof(Backend) );
530
531         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
532
533         /* did realloc move our table? if so, fix up dependent pointers */
534         if ( be != backendDB ) {
535                 int i;
536                 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
537                         be->be_pcl_mutexp = &be->be_pcl_mutex;
538                 }
539         }
540
541         be = &backends[nbackends++];
542
543         be->bd_info = bi;
544
545         be->be_def_limit = frontendDB->be_def_limit;
546         be->be_dfltaccess = frontendDB->be_dfltaccess;
547
548         be->be_restrictops = frontendDB->be_restrictops;
549         be->be_requires = frontendDB->be_requires;
550         be->be_ssf_set = frontendDB->be_ssf_set;
551
552         be->be_pcl_mutexp = &be->be_pcl_mutex;
553         ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
554
555         /* assign a default depth limit for alias deref */
556         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
557
558         if(bi->bi_db_init) {
559                 rc = bi->bi_db_init( be );
560         }
561
562         if(rc != 0) {
563                 fprintf( stderr, "database init failed (%s)\n", type );
564                 nbackends--;
565                 return NULL;
566         }
567
568         bi->bi_nDB++;
569         return( be );
570 }
571
572 void
573 be_db_close( void )
574 {
575         int     i;
576
577         for ( i = 0; i < nbackends; i++ ) {
578                 if ( backends[i].bd_info->bi_db_close ) {
579                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
580                 }
581         }
582
583         if ( frontendDB->bd_info->bi_db_close ) {
584                 (*frontendDB->bd_info->bi_db_close)( frontendDB );
585         }
586 }
587
588 Backend *
589 select_backend(
590         struct berval * dn,
591         int manageDSAit,
592         int noSubs )
593 {
594         int             i, j;
595         ber_len_t       len, dnlen = dn->bv_len;
596         Backend         *be = NULL;
597
598         for ( i = 0; i < nbackends; i++ ) {
599                 if ( backends[i].be_nsuffix == NULL ) {
600                         continue;
601                 }
602
603                 for ( j = 0; !BER_BVISNULL( &backends[i].be_nsuffix[j] ); j++ )
604                 {
605                         if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
606                                 && noSubs )
607                         {
608                                 continue;
609                         }
610
611                         len = backends[i].be_nsuffix[j].bv_len;
612
613                         if ( len > dnlen ) {
614                                 /* suffix is longer than DN */
615                                 continue;
616                         }
617                         
618                         /*
619                          * input DN is normalized, so the separator check
620                          * need not look at escaping
621                          */
622                         if ( len && len < dnlen &&
623                                 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
624                         {
625                                 continue;
626                         }
627
628                         if ( strcmp( backends[i].be_nsuffix[j].bv_val,
629                                 &dn->bv_val[dnlen-len] ) == 0 )
630                         {
631                                 if( be == NULL ) {
632                                         be = &backends[i];
633
634                                         if( manageDSAit && len == dnlen &&
635                                                 !SLAP_GLUE_SUBORDINATE( be ) ) {
636                                                 continue;
637                                         }
638                                 } else {
639                                         be = &backends[i];
640                                 }
641                                 return be;
642                         }
643                 }
644         }
645
646         return be;
647 }
648
649 int
650 be_issuffix(
651     Backend *be,
652     struct berval *bvsuffix )
653 {
654         int     i;
655
656         if ( be->be_nsuffix == NULL ) {
657                 return 0;
658         }
659
660         for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
661                 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
662                         return 1;
663                 }
664         }
665
666         return 0;
667 }
668
669 int
670 be_isroot_dn( Backend *be, struct berval *ndn )
671 {
672         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
673                 return 0;
674         }
675
676         return dn_match( &be->be_rootndn, ndn );
677 }
678
679 int
680 be_slurp_update( Operation *op )
681 {
682         return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
683                 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
684 }
685
686 int
687 be_shadow_update( Operation *op )
688 {
689         return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
690                 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
691 }
692
693 int
694 be_isupdate_dn( Backend *be, struct berval *ndn )
695 {
696         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
697                 return 0;
698         }
699
700         return dn_match( &be->be_update_ndn, ndn );
701 }
702
703 struct berval *
704 be_root_dn( Backend *be )
705 {
706         return &be->be_rootdn;
707 }
708
709 int
710 be_isroot( Operation *op )
711 {
712         return be_isroot_dn( op->o_bd, &op->o_ndn );
713 }
714
715 int
716 be_isroot_pw( Operation *op )
717 {
718         int result;
719
720         if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
721                 return 0;
722         }
723
724         if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
725                 return 0;
726         }
727
728 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
729         ldap_pvt_thread_mutex_lock( &passwd_mutex );
730 #ifdef SLAPD_SPASSWD
731         lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
732 #endif
733 #endif
734
735         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
736
737 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
738 #ifdef SLAPD_SPASSWD
739         lutil_passwd_sasl_conn = NULL;
740 #endif
741         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
742 #endif
743
744         return result == 0;
745 }
746
747 int
748 be_entry_release_rw(
749         Operation *op,
750         Entry *e,
751         int rw )
752 {
753         if ( op->o_bd->be_release ) {
754                 /* free and release entry from backend */
755                 return op->o_bd->be_release( op, e, rw );
756         } else {
757                 /* free entry */
758                 entry_free( e );
759                 return 0;
760         }
761 }
762
763 int
764 backend_unbind( Operation *op, SlapReply *rs )
765 {
766         int             i;
767
768         for ( i = 0; i < nbackends; i++ ) {
769 #if defined( LDAP_SLAPI )
770                 if ( op->o_pb ) {
771                         int rc;
772                         if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
773                         slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
774                         rc = slapi_int_call_plugins( &backends[i],
775                                 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
776                         if ( rc < 0 ) {
777                                 /*
778                                  * A preoperation plugin failure will abort the
779                                  * entire operation.
780                                  */
781                                 Debug(LDAP_DEBUG_TRACE,
782                                         "do_bind: Unbind preoperation plugin failed\n",
783                                         0, 0, 0);
784                                 return 0;
785                         }
786                 }
787 #endif /* defined( LDAP_SLAPI ) */
788
789                 if ( backends[i].be_unbind ) {
790                         op->o_bd = &backends[i];
791                         (*backends[i].be_unbind)( op, rs );
792                 }
793
794 #if defined( LDAP_SLAPI )
795                 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
796                         SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
797                 {
798                         Debug(LDAP_DEBUG_TRACE,
799                                 "do_unbind: Unbind postoperation plugins failed\n",
800                                 0, 0, 0);
801                 }
802 #endif /* defined( LDAP_SLAPI ) */
803         }
804
805         return 0;
806 }
807
808 int
809 backend_connection_init(
810         Connection   *conn )
811 {
812         int     i;
813
814         for ( i = 0; i < nbackends; i++ ) {
815                 if ( backends[i].be_connection_init ) {
816                         (*backends[i].be_connection_init)( &backends[i], conn);
817                 }
818         }
819
820         return 0;
821 }
822
823 int
824 backend_connection_destroy(
825         Connection   *conn )
826 {
827         int     i;
828
829         for ( i = 0; i < nbackends; i++ ) {
830                 if ( backends[i].be_connection_destroy ) {
831                         (*backends[i].be_connection_destroy)( &backends[i], conn);
832                 }
833         }
834
835         return 0;
836 }
837
838 static int
839 backend_check_controls(
840         Operation *op,
841         SlapReply *rs )
842 {
843         LDAPControl **ctrls = op->o_ctrls;
844         rs->sr_err = LDAP_SUCCESS;
845
846         if( ctrls ) {
847                 for( ; *ctrls != NULL ; ctrls++ ) {
848                         /* KLUDGE: ldctl_iscritical munged by controls.c:get_ctrls()
849                          * to ensure this check is enabled/disabled appropriately.
850                          */
851                         if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
852                                 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
853                         {
854                                 /* Per RFC 2251 (and LDAPBIS discussions), if the control
855                                  * is recognized and appropriate for the operation (which
856                                  * we've already verified), then the server should make
857                                  * use of the control when performing the operation.
858                                  * 
859                                  * Here we find that operation extended by the control
860                                  * is not unavailable in a particular context, hence the
861                                  * return of unwillingToPerform.
862                                  */
863                                 rs->sr_text = "control unavailable in context";
864                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
865                                 break;
866                         }
867                 }
868         }
869
870         return rs->sr_err;
871 }
872
873 int
874 backend_check_restrictions(
875         Operation *op,
876         SlapReply *rs,
877         struct berval *opdata )
878 {
879         slap_mask_t restrictops;
880         slap_mask_t requires;
881         slap_mask_t opflag;
882         slap_mask_t exopflag = 0;
883         slap_ssf_set_t *ssf;
884         int updateop = 0;
885         int starttls = 0;
886         int session = 0;
887
888         if( op->o_bd ) {
889                 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
890                         return rs->sr_err;
891                 }
892
893                 restrictops = op->o_bd->be_restrictops;
894                 requires = op->o_bd->be_requires;
895                 ssf = &op->o_bd->be_ssf_set;
896
897         } else {
898                 restrictops = frontendDB->be_restrictops;
899                 requires = frontendDB->be_requires;
900                 ssf = &frontendDB->be_ssf_set;
901         }
902
903         switch( op->o_tag ) {
904         case LDAP_REQ_ADD:
905                 opflag = SLAP_RESTRICT_OP_ADD;
906                 updateop++;
907                 break;
908         case LDAP_REQ_BIND:
909                 opflag = SLAP_RESTRICT_OP_BIND;
910                 session++;
911                 break;
912         case LDAP_REQ_COMPARE:
913                 opflag = SLAP_RESTRICT_OP_COMPARE;
914                 break;
915         case LDAP_REQ_DELETE:
916                 updateop++;
917                 opflag = SLAP_RESTRICT_OP_DELETE;
918                 break;
919         case LDAP_REQ_EXTENDED:
920                 opflag = SLAP_RESTRICT_OP_EXTENDED;
921
922                 if( !opdata ) {
923                         /* treat unspecified as a modify */
924                         opflag = SLAP_RESTRICT_OP_MODIFY;
925                         updateop++;
926                         break;
927                 }
928
929                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
930                         session++;
931                         starttls++;
932                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
933                         break;
934                 }
935
936                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
937                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
938                         break;
939                 }
940
941                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
942                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
943                         break;
944                 }
945
946                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
947                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
948                         updateop++;
949                         break;
950                 }
951
952                 /* treat everything else as a modify */
953                 opflag = SLAP_RESTRICT_OP_MODIFY;
954                 updateop++;
955                 break;
956
957         case LDAP_REQ_MODIFY:
958                 updateop++;
959                 opflag = SLAP_RESTRICT_OP_MODIFY;
960                 break;
961         case LDAP_REQ_RENAME:
962                 updateop++;
963                 opflag = SLAP_RESTRICT_OP_RENAME;
964                 break;
965         case LDAP_REQ_SEARCH:
966                 opflag = SLAP_RESTRICT_OP_SEARCH;
967                 break;
968         case LDAP_REQ_UNBIND:
969                 session++;
970                 opflag = 0;
971                 break;
972         default:
973                 rs->sr_text = "restrict operations internal error";
974                 rs->sr_err = LDAP_OTHER;
975                 return rs->sr_err;
976         }
977
978         if ( !starttls ) {
979                 /* these checks don't apply to StartTLS */
980
981                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
982                 if( op->o_transport_ssf < ssf->sss_transport ) {
983                         rs->sr_text = op->o_transport_ssf
984                                 ? "stronger transport confidentiality required"
985                                 : "transport confidentiality required";
986                         return rs->sr_err;
987                 }
988
989                 if( op->o_tls_ssf < ssf->sss_tls ) {
990                         rs->sr_text = op->o_tls_ssf
991                                 ? "stronger TLS confidentiality required"
992                                 : "TLS confidentiality required";
993                         return rs->sr_err;
994                 }
995
996
997                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
998                         /* simple bind specific check */
999                         if( op->o_ssf < ssf->sss_simple_bind ) {
1000                                 rs->sr_text = op->o_ssf
1001                                         ? "stronger confidentiality required"
1002                                         : "confidentiality required";
1003                                 return rs->sr_err;
1004                         }
1005                 }
1006
1007                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1008                         /* these checks don't apply to SASL bind */
1009
1010                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
1011                                 rs->sr_text = op->o_sasl_ssf
1012                                         ? "stronger SASL confidentiality required"
1013                                         : "SASL confidentiality required";
1014                                 return rs->sr_err;
1015                         }
1016
1017                         if( op->o_ssf < ssf->sss_ssf ) {
1018                                 rs->sr_text = op->o_ssf
1019                                         ? "stronger confidentiality required"
1020                                         : "confidentiality required";
1021                                 return rs->sr_err;
1022                         }
1023                 }
1024
1025                 if( updateop ) {
1026                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
1027                                 rs->sr_text = op->o_transport_ssf
1028                                         ? "stronger transport confidentiality required for update"
1029                                         : "transport confidentiality required for update";
1030                                 return rs->sr_err;
1031                         }
1032
1033                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
1034                                 rs->sr_text = op->o_tls_ssf
1035                                         ? "stronger TLS confidentiality required for update"
1036                                         : "TLS confidentiality required for update";
1037                                 return rs->sr_err;
1038                         }
1039
1040                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1041                                 rs->sr_text = op->o_sasl_ssf
1042                                         ? "stronger SASL confidentiality required for update"
1043                                         : "SASL confidentiality required for update";
1044                                 return rs->sr_err;
1045                         }
1046
1047                         if( op->o_ssf < ssf->sss_update_ssf ) {
1048                                 rs->sr_text = op->o_ssf
1049                                         ? "stronger confidentiality required for update"
1050                                         : "confidentiality required for update";
1051                                 return rs->sr_err;
1052                         }
1053
1054                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1055                                 BER_BVISEMPTY( &op->o_ndn ) )
1056                         {
1057                                 rs->sr_text = "modifications require authentication";
1058                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1059                                 return rs->sr_err;
1060                         }
1061
1062 #ifdef SLAP_X_LISTENER_MOD
1063                         if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn ) ? S_IWUSR : S_IWOTH ) ) ) {
1064                                 /* no "w" mode means readonly */
1065                                 rs->sr_text = "modifications not allowed on this listener";
1066                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1067                                 return rs->sr_err;
1068                         }
1069 #endif /* SLAP_X_LISTENER_MOD */
1070                 }
1071         }
1072
1073         if ( !session ) {
1074                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1075
1076                 if( requires & SLAP_REQUIRE_STRONG ) {
1077                         /* should check mechanism */
1078                         if( ( op->o_transport_ssf < ssf->sss_transport
1079                                 && op->o_authtype == LDAP_AUTH_SIMPLE )
1080                                 || BER_BVISEMPTY( &op->o_dn ) )
1081                         {
1082                                 rs->sr_text = "strong(er) authentication required";
1083                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1084                                 return rs->sr_err;
1085                         }
1086                 }
1087
1088                 if( requires & SLAP_REQUIRE_SASL ) {
1089                         if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1090                                 rs->sr_text = "SASL authentication required";
1091                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1092                                 return rs->sr_err;
1093                         }
1094                 }
1095                         
1096                 if( requires & SLAP_REQUIRE_AUTHC ) {
1097                         if( BER_BVISEMPTY( &op->o_dn ) ) {
1098                                 rs->sr_text = "authentication required";
1099                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1100                                 return rs->sr_err;
1101                         }
1102                 }
1103
1104                 if( requires & SLAP_REQUIRE_BIND ) {
1105                         int version;
1106                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1107                         version = op->o_conn->c_protocol;
1108                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1109
1110                         if( !version ) {
1111                                 /* no bind has occurred */
1112                                 rs->sr_text = "BIND required";
1113                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1114                                 return rs->sr_err;
1115                         }
1116                 }
1117
1118                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1119                         if( op->o_protocol < LDAP_VERSION3 ) {
1120                                 /* no bind has occurred */
1121                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1122                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1123                                 return rs->sr_err;
1124                         }
1125                 }
1126
1127 #ifdef SLAP_X_LISTENER_MOD
1128                 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1129                         if ( op->o_conn->c_listener &&
1130                                 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1131                 {
1132                                 /* no "x" mode means bind required */
1133                                 rs->sr_text = "bind required on this listener";
1134                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1135                                 return rs->sr_err;
1136                         }
1137                 }
1138
1139                 if ( !starttls && !updateop ) {
1140                         if ( op->o_conn->c_listener &&
1141                                 !( op->o_conn->c_listener->sl_perms &
1142                                         ( !BER_BVISEMPTY( &op->o_dn ) ? S_IRUSR : S_IROTH )))
1143                         {
1144                                 /* no "r" mode means no read */
1145                                 rs->sr_text = "read not allowed on this listener";
1146                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1147                                 return rs->sr_err;
1148                         }
1149                 }
1150 #endif /* SLAP_X_LISTENER_MOD */
1151
1152         }
1153
1154         if( ( restrictops & opflag )
1155                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1156                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1157                         rs->sr_text = "read operations restricted";
1158                 } else if ( restrictops & exopflag ) {
1159                         rs->sr_text = "extended operation restricted";
1160                 } else {
1161                         rs->sr_text = "operation restricted";
1162                 }
1163                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1164                 return rs->sr_err;
1165         }
1166
1167         rs->sr_err = LDAP_SUCCESS;
1168         return rs->sr_err;
1169 }
1170
1171 int backend_check_referrals( Operation *op, SlapReply *rs )
1172 {
1173         rs->sr_err = LDAP_SUCCESS;
1174
1175         if( op->o_bd->be_chk_referrals ) {
1176                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1177
1178                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1179                         send_ldap_result( op, rs );
1180                 }
1181         }
1182
1183         return rs->sr_err;
1184 }
1185
1186 int
1187 be_entry_get_rw(
1188         Operation *op,
1189         struct berval *ndn,
1190         ObjectClass *oc,
1191         AttributeDescription *at,
1192         int rw,
1193         Entry **e )
1194 {
1195         int rc;
1196
1197         *e = NULL;
1198
1199         if (op->o_bd == NULL) {
1200                 rc = LDAP_NO_SUCH_OBJECT;
1201         } else if ( op->o_bd->be_fetch ) {
1202                 rc = ( op->o_bd->be_fetch )( op, ndn,
1203                         oc, at, rw, e );
1204         } else {
1205                 rc = LDAP_UNWILLING_TO_PERFORM;
1206         }
1207         return rc;
1208 }
1209
1210 int 
1211 backend_group(
1212         Operation *op,
1213         Entry   *target,
1214         struct berval *gr_ndn,
1215         struct berval *op_ndn,
1216         ObjectClass *group_oc,
1217         AttributeDescription *group_at )
1218 {
1219         Entry *e;
1220         Attribute *a;
1221         int rc;
1222         GroupAssertion *g;
1223         Backend *be = op->o_bd;
1224
1225         if ( op->o_abandon ) return SLAPD_ABANDON;
1226
1227         op->o_bd = select_backend( gr_ndn, 0, 0 );
1228
1229         for ( g = op->o_groups; g; g = g->ga_next ) {
1230                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1231                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1232                 {
1233                         continue;
1234                 }
1235                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1236                         break;
1237                 }
1238         }
1239
1240         if ( g ) {
1241                 rc = g->ga_res;
1242                 goto done;
1243         }
1244
1245         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1246                 e = target;
1247                 rc = 0;
1248         } else {
1249                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1250         }
1251         if ( e ) {
1252 #ifdef LDAP_SLAPI
1253                 if ( op->o_pb != NULL ) {
1254                         init_group_pblock( op, target, e, op_ndn, group_at );
1255
1256                         rc = call_group_preop_plugins( op );
1257                         if ( rc == LDAP_SUCCESS ) {
1258                                 goto done;
1259                         }
1260                 }
1261 #endif /* LDAP_SLAPI */
1262
1263                 a = attr_find( e->e_attrs, group_at );
1264                 if ( a ) {
1265                         /* If the attribute is a subtype of labeledURI, treat this as
1266                          * a dynamic group ala groupOfURLs
1267                          */
1268                         if (is_at_subtype( group_at->ad_type,
1269                                 slap_schema.si_ad_labeledURI->ad_type ) )
1270                         {
1271                                 int i;
1272                                 LDAPURLDesc *ludp;
1273                                 struct berval bv, nbase;
1274                                 Filter *filter;
1275                                 Entry *user;
1276                                 Backend *b2 = op->o_bd;
1277
1278                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1279                                         user = target;
1280                                 } else {
1281                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1282                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1283                                 }
1284                                 
1285                                 if ( rc == 0 ) {
1286                                         rc = 1;
1287                                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1288                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1289                                                         LDAP_URL_SUCCESS )
1290                                                 {
1291                                                         continue;
1292                                                 }
1293                                                 BER_BVZERO( &nbase );
1294                                                 /* host part must be empty */
1295                                                 /* attrs and extensions parts must be empty */
1296                                                 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1297                                                         ludp->lud_attrs || ludp->lud_exts )
1298                                                 {
1299                                                         goto loopit;
1300                                                 }
1301                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1302                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1303                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1304                                                 {
1305                                                         goto loopit;
1306                                                 }
1307                                                 switch ( ludp->lud_scope ) {
1308                                                 case LDAP_SCOPE_BASE:
1309                                                         if ( !dn_match( &nbase, op_ndn ) ) {
1310                                                                 goto loopit;
1311                                                         }
1312                                                         break;
1313                                                 case LDAP_SCOPE_ONELEVEL:
1314                                                         dnParent( op_ndn, &bv );
1315                                                         if ( !dn_match( &nbase, &bv ) ) {
1316                                                                 goto loopit;
1317                                                         }
1318                                                         break;
1319                                                 case LDAP_SCOPE_SUBTREE:
1320                                                         if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1321                                                                 goto loopit;
1322                                                         }
1323                                                         break;
1324 #ifdef LDAP_SCOPE_SUBORDINATE
1325                                                 case LDAP_SCOPE_SUBORDINATE:
1326                                                         if ( dn_match( &nbase, op_ndn ) ||
1327                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1328                                                         {
1329                                                                 goto loopit;
1330                                                         }
1331 #endif
1332                                                 }
1333                                                 filter = str2filter_x( op, ludp->lud_filter );
1334                                                 if ( filter ) {
1335                                                         if ( test_filter( NULL, user, filter ) ==
1336                                                                 LDAP_COMPARE_TRUE )
1337                                                         {
1338                                                                 rc = 0;
1339                                                         }
1340                                                         filter_free_x( op, filter );
1341                                                 }
1342 loopit:
1343                                                 ldap_free_urldesc( ludp );
1344                                                 if ( !BER_BVISNULL( &nbase ) ) {
1345                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1346                                                 }
1347                                                 if ( rc == 0 ) break;
1348                                         }
1349                                         if ( user != target ) {
1350                                                 be_entry_release_r( op, user );
1351                                         }
1352                                 }
1353                                 op->o_bd = b2;
1354                         } else {
1355                                 rc = value_find_ex( group_at,
1356                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1357                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1358                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1359                         }
1360                 } else {
1361                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1362                 }
1363                 if (e != target ) {
1364                         be_entry_release_r( op, e );
1365                 }
1366         } else {
1367                 rc = LDAP_NO_SUCH_OBJECT;
1368         }
1369
1370 #ifdef LDAP_SLAPI
1371         if ( op->o_pb ) call_group_postop_plugins( op );
1372 #endif /* LDAP_SLAPI */
1373
1374         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1375                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1376                         op->o_tmpmemctx );
1377                 g->ga_be = op->o_bd;
1378                 g->ga_oc = group_oc;
1379                 g->ga_at = group_at;
1380                 g->ga_res = rc;
1381                 g->ga_len = gr_ndn->bv_len;
1382                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1383                 g->ga_next = op->o_groups;
1384                 op->o_groups = g;
1385         }
1386 done:
1387         op->o_bd = be;
1388         return rc;
1389 }
1390
1391 #ifdef LDAP_SLAPI
1392 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1393 {
1394         BerVarray v;
1395         int rc;
1396         BerVarray *vals = (BerVarray *)c->cac_private;
1397         Operation *op = NULL;
1398         int i, j;
1399
1400         slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1401         if ( op == NULL ) {
1402                 return 1;
1403         }
1404
1405         if ( op->o_conn && access_allowed( op,
1406                 e, a->a_desc, NULL, ACL_AUTH,
1407                 &c->cac_acl_state ) == 0 ) {
1408                 return 1;
1409         }
1410
1411         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1412                         
1413         v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1414                 op->o_tmpmemctx );
1415         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1416                 if ( op->o_conn && access_allowed( op,
1417                         e, a->a_desc,
1418                         &a->a_nvals[i],
1419                         ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1420                         continue;
1421                 }
1422                 ber_dupbv_x( &v[j],
1423                         &a->a_nvals[i], op->o_tmpmemctx );
1424                 if ( !BER_BVISNULL( &v[j] ) ) {
1425                         j++;
1426                 }
1427         }
1428
1429         if ( j == 0 ) {
1430                 op->o_tmpfree( v, op->o_tmpmemctx );
1431                 *vals = NULL;
1432                 rc = 1;
1433         } else {
1434                 BER_BVZERO( &v[j] );
1435                 *vals = v;
1436                 rc = 0;
1437         }
1438
1439         return rc;
1440 }
1441 #endif /* LDAP_SLAPI */
1442
1443 int 
1444 backend_attribute(
1445         Operation *op,
1446         Entry   *target,
1447         struct berval   *edn,
1448         AttributeDescription *entry_at,
1449         BerVarray *vals,
1450         slap_access_t access )
1451 {
1452         Entry                   *e = NULL;
1453         Attribute               *a = NULL;
1454         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1455         AccessControlState      acl_state = ACL_STATE_INIT;
1456         Backend                 *be = op->o_bd;
1457
1458         op->o_bd = select_backend( edn, 0, 0 );
1459
1460         if ( target && dn_match( &target->e_nname, edn ) ) {
1461                 e = target;
1462
1463         } else {
1464                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1465         } 
1466
1467         if ( e ) {
1468                 a = attr_find( e->e_attrs, entry_at );
1469                 if ( a == NULL ) {
1470                         SlapReply       rs = { 0 };
1471                         AttributeName   anlist[ 2 ];
1472
1473                         anlist[ 0 ].an_name = entry_at->ad_cname;
1474                         anlist[ 0 ].an_desc = entry_at;
1475                         BER_BVZERO( &anlist[ 1 ].an_name );
1476                         rs.sr_attrs = anlist;
1477                         
1478                         rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1479
1480                         rc = backend_operational( op, &rs );
1481
1482                         if ( rc == LDAP_SUCCESS && rs.sr_operational_attrs ) {
1483                                 freeattr = 1;
1484                                 a = rs.sr_operational_attrs;
1485                         }
1486                 }
1487
1488                 if ( a ) {
1489                         BerVarray v;
1490
1491                         if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1492                                 e, entry_at, NULL, access,
1493                                 &acl_state ) == 0 ) {
1494                                 rc = LDAP_INSUFFICIENT_ACCESS;
1495                                 goto freeit;
1496                         }
1497
1498                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1499                                 ;
1500                         
1501                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1502                                 op->o_tmpmemctx );
1503                         for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1504                         {
1505                                 if ( op->o_conn && access > ACL_NONE && 
1506                                                 access_allowed( op, e,
1507                                                         entry_at,
1508                                                         &a->a_nvals[i],
1509                                                         access,
1510                                                         &acl_state ) == 0 )
1511                                 {
1512                                         continue;
1513                                 }
1514                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1515                                                 op->o_tmpmemctx );
1516                                 if ( !BER_BVISNULL( &v[j] ) ) {
1517                                         j++;
1518                                 }
1519                         }
1520                         if ( j == 0 ) {
1521                                 op->o_tmpfree( v, op->o_tmpmemctx );
1522                                 *vals = NULL;
1523                                 rc = LDAP_INSUFFICIENT_ACCESS;
1524
1525                         } else {
1526                                 BER_BVZERO( &v[j] );
1527                                 *vals = v;
1528                                 rc = LDAP_SUCCESS;
1529                         }
1530                 }
1531 #ifdef LDAP_SLAPI
1532                 else if ( op->o_pb ) {
1533                         /* try any computed attributes */
1534                         computed_attr_context   ctx;
1535
1536                         slapi_int_pblock_set_operation( op->o_pb, op );
1537
1538                         ctx.cac_pb = op->o_pb;
1539                         ctx.cac_attrs = NULL;
1540                         ctx.cac_userattrs = 0;
1541                         ctx.cac_opattrs = 0;
1542                         ctx.cac_acl_state = acl_state;
1543                         ctx.cac_private = (void *)vals;
1544
1545                         rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1546                         if ( rc == 1 ) {
1547                                 rc = LDAP_INSUFFICIENT_ACCESS;
1548
1549                         } else {
1550                                 rc = LDAP_SUCCESS;
1551                         }
1552                 }
1553 #endif /* LDAP_SLAPI */
1554 freeit:         if ( e != target ) {
1555                         be_entry_release_r( op, e );
1556                 }
1557                 if ( freeattr ) {
1558                         attr_free( a );
1559                 }
1560         }
1561
1562         op->o_bd = be;
1563         return rc;
1564 }
1565
1566 int backend_operational(
1567         Operation *op,
1568         SlapReply *rs )
1569 {
1570         Attribute       **ap;
1571         int             rc = 0;
1572         BackendDB       *be_orig;
1573
1574         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1575                 /* just count them */ ;
1576
1577         /*
1578          * If operational attributes (allegedly) are required, 
1579          * and the backend supports specific operational attributes, 
1580          * add them to the attribute list
1581          */
1582         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1583                 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1584         {
1585                 *ap = slap_operational_entryDN( rs->sr_entry );
1586                 ap = &(*ap)->a_next;
1587         }
1588
1589         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1590                 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1591         {
1592                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1593                 ap = &(*ap)->a_next;
1594         }
1595
1596         /* Let the overlays have a chance at this */
1597         be_orig = op->o_bd;
1598         if ( SLAP_ISOVERLAY( be_orig ))
1599                 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1600
1601         if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1602                 op->o_bd && op->o_bd->be_operational != NULL )
1603         {
1604                 Attribute       *a;
1605                 
1606                 a = rs->sr_operational_attrs;
1607                 rs->sr_operational_attrs = NULL;
1608                 rc = op->o_bd->be_operational( op, rs );
1609                 *ap = rs->sr_operational_attrs;
1610                 if ( a != NULL ) {
1611                         rs->sr_operational_attrs = a;
1612                 }
1613
1614                 for ( ; *ap; ap = &(*ap)->a_next )
1615                         /* just count them */ ;
1616         }
1617         op->o_bd = be_orig;
1618
1619         return rc;
1620 }
1621
1622 #ifdef LDAP_SLAPI
1623 static void init_group_pblock( Operation *op, Entry *target,
1624         Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1625 {
1626         slapi_int_pblock_set_operation( op->o_pb, op );
1627
1628         slapi_pblock_set( op->o_pb,
1629                 SLAPI_X_GROUP_ENTRY, (void *)e );
1630         slapi_pblock_set( op->o_pb,
1631                 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1632         slapi_pblock_set( op->o_pb,
1633                 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1634         slapi_pblock_set( op->o_pb,
1635                 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1636 }
1637
1638 static int call_group_preop_plugins( Operation *op )
1639 {
1640         int rc;
1641
1642         rc = slapi_int_call_plugins( op->o_bd,
1643                 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1644         if ( rc < 0 ) {
1645                 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1646                         (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1647                 {
1648                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1649                 }
1650         } else {
1651                 rc = LDAP_SUCCESS;
1652         }
1653
1654         return rc;
1655 }
1656
1657 static void call_group_postop_plugins( Operation *op )
1658 {
1659         (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1660 }
1661 #endif /* LDAP_SLAPI */
1662