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