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