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