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