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