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