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