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