]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Suck in changes from HEAD
[openldap] / servers / slapd / backend.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* backend.c - routines for dealing with back-end databases */
7
8
9 #include "portable.h"
10
11 #include <stdio.h>
12
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include <sys/stat.h>
17
18 #include "slap.h"
19 #include "lutil.h"
20 #include "lber_pvt.h"
21
22 /*
23  * If a module is configured as dynamic, its header should not
24  * get included into slapd. While this is a general rule and does
25  * not have much of an effect in UNIX, this rule should be adhered
26  * to for Windows, where dynamic object code should not be implicitly
27  * imported into slapd without appropriate __declspec(dllimport) directives.
28  */
29
30 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
31 #include "back-bdb/external.h"
32 #endif
33 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
34 #include "back-dnssrv/external.h"
35 #endif
36 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
37 #include "back-ldap/external.h"
38 #endif
39 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
40 #include "back-ldbm/external.h"
41 #endif
42 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
43 #include "back-meta/external.h"
44 #endif
45 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
46 #include "back-monitor/external.h"
47 #endif
48 #if defined(SLAPD_NULL) && !defined(SLAPD_NULL_DYNAMIC)
49 #include "back-null/external.h"
50 #endif
51 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
52 #include "back-passwd/external.h"
53 #endif
54 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
55 #include "back-perl/external.h"
56 #endif
57 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
58 #include "back-shell/external.h"
59 #endif
60 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
61 #include "back-tcl/external.h"
62 #endif
63 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
64 #include "back-sql/external.h"
65 #endif
66 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
67 #include "private/external.h"
68 #endif
69
70 static BackendInfo binfo[] = {
71 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
72         {"bdb", bdb_initialize},
73 #endif
74 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
75         {"dnssrv",      dnssrv_back_initialize},
76 #endif
77 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
78         {"ldap",        ldap_back_initialize},
79 #endif
80 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
81         {"ldbm",        ldbm_back_initialize},
82 #endif
83 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
84         {"meta",        meta_back_initialize},
85 #endif
86 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
87         {"monitor",     monitor_back_initialize},
88 #endif
89 #if defined(SLAPD_NULL) && !defined(SLAPD_NULL_DYNAMIC)
90         {"null",        null_back_initialize},
91 #endif
92 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
93         {"passwd",      passwd_back_initialize},
94 #endif
95 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
96         {"perl",        perl_back_initialize},
97 #endif
98 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
99         {"shell",       shell_back_initialize},
100 #endif
101 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
102         {"tcl",         tcl_back_initialize},
103 #endif
104 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
105         {"sql",         sql_back_initialize},
106 #endif
107         /* for any private backend */
108 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
109         {"private",     private_back_initialize},
110 #endif
111         {NULL}
112 };
113
114 int                     nBackendInfo = 0;
115 BackendInfo     *backendInfo = NULL;
116
117 int                     nBackendDB = 0; 
118 BackendDB       *backendDB = NULL;
119
120 int backend_init(void)
121 {
122         int rc = -1;
123
124         if((nBackendInfo != 0) || (backendInfo != NULL)) {
125                 /* already initialized */
126 #ifdef NEW_LOGGING
127                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
128                            "backend_init:  backend already initialized\n" ));
129 #else
130                 Debug( LDAP_DEBUG_ANY,
131                         "backend_init: already initialized.\n", 0, 0, 0 );
132 #endif
133                 return -1;
134         }
135
136         for( ;
137                 binfo[nBackendInfo].bi_type != NULL;
138                 nBackendInfo++ )
139         {
140                 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
141
142                 if(rc != 0) {
143 #ifdef NEW_LOGGING
144                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
145                                 "backend_init:  initialized for type \"%s\"\n",
146                                 binfo[nBackendInfo].bi_type ));
147 #else
148                         Debug( LDAP_DEBUG_ANY,
149                                 "backend_init: initialized for type \"%s\"\n",
150                                 binfo[nBackendInfo].bi_type, 0, 0 );
151 #endif
152                         /* destroy those we've already inited */
153                         for( nBackendInfo--;
154                                 nBackendInfo >= 0 ;
155                                 nBackendInfo-- )
156                         { 
157                                 if ( binfo[nBackendInfo].bi_destroy ) {
158                                         binfo[nBackendInfo].bi_destroy(
159                                                 &binfo[nBackendInfo] );
160                                 }
161                         }
162                         return rc;
163                 }
164         }
165
166         if ( nBackendInfo > 0) {
167                 backendInfo = binfo;
168                 return 0;
169         }
170
171 #ifdef SLAPD_MODULES    
172         return 0;
173 #else
174
175 #ifdef NEW_LOGGING
176         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
177                 "backend_init: failed\n" ));
178 #else
179         Debug( LDAP_DEBUG_ANY,
180                 "backend_init: failed\n",
181                 0, 0, 0 );
182 #endif
183
184         return rc;
185 #endif /* SLAPD_MODULES */
186 }
187
188 int backend_add(BackendInfo *aBackendInfo)
189 {
190    int rc = 0;
191
192    if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
193 #ifdef NEW_LOGGING
194        LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
195                   "backend_add:  initialization for type \"%s\" failed\n",
196                   aBackendInfo->bi_type ));
197 #else
198       Debug( LDAP_DEBUG_ANY,
199              "backend_add: initialization for type \"%s\" failed\n",
200              aBackendInfo->bi_type, 0, 0 );
201 #endif
202       return rc;
203    }
204
205    /* now add the backend type to the Backend Info List */
206    {
207       BackendInfo *newBackendInfo = 0;
208
209       /* if backendInfo == binfo no deallocation of old backendInfo */
210       if (backendInfo == binfo) {
211          newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
212          AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) * 
213                 nBackendInfo);
214       } else {
215          newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) * 
216                                      (nBackendInfo + 1));
217       }
218       AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo, 
219              sizeof(BackendInfo));
220       backendInfo = newBackendInfo;
221       nBackendInfo++;
222
223       return 0;
224    }        
225 }
226
227 int backend_startup(Backend *be)
228 {
229         int i;
230         int rc = 0;
231
232         if( ! ( nBackendDB > 0 ) ) {
233                 /* no databases */
234 #ifdef NEW_LOGGING
235                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
236                            "backend_startup: %d databases to startup. \n",
237                            nBackendDB ));
238 #else
239                 Debug( LDAP_DEBUG_ANY,
240                         "backend_startup: %d databases to startup.\n",
241                         nBackendDB, 0, 0 );
242 #endif
243                 return 1;
244         }
245
246         if(be != NULL) {
247                 /* startup a specific backend database */
248 #ifdef NEW_LOGGING
249                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
250                            "backend_startup:  starting \"%s\"\n",
251                            be->be_suffix[0].bv_val ));
252 #else
253                 Debug( LDAP_DEBUG_TRACE,
254                         "backend_startup: starting \"%s\"\n",
255                         be->be_suffix[0].bv_val, 0, 0 );
256 #endif
257
258                 if ( be->bd_info->bi_open ) {
259                         rc = be->bd_info->bi_open( be->bd_info );
260                 }
261
262                 if(rc != 0) {
263 #ifdef NEW_LOGGING
264                         LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
265                                    "backend_startup: bi_open failed!\n" ));
266 #else
267                         Debug( LDAP_DEBUG_ANY,
268                                 "backend_startup: bi_open failed!\n",
269                                 0, 0, 0 );
270 #endif
271
272                         return rc;
273                 }
274
275                 if ( be->bd_info->bi_db_open ) {
276                         rc = be->bd_info->bi_db_open( be );
277                 }
278
279                 if(rc != 0) {
280 #ifdef NEW_LOGGING
281                         LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
282                                 "backend_startup: bi_db_open failed! (%d)\n", rc ));
283 #else
284                         Debug( LDAP_DEBUG_ANY,
285                                 "backend_startup: bi_db_open failed! (%d)\n",
286                                 rc, 0, 0 );
287 #endif
288                         return rc;
289                 }
290
291                 return rc;
292         }
293
294         /* open each backend type */
295         for( i = 0; i < nBackendInfo; i++ ) {
296                 if( backendInfo[i].bi_nDB == 0) {
297                         /* no database of this type, don't open */
298                         continue;
299                 }
300
301                 if( backendInfo[i].bi_open ) {
302                         rc = backendInfo[i].bi_open(
303                                 &backendInfo[i] );
304                 }
305
306                 if(rc != 0) {
307 #ifdef NEW_LOGGING
308                         LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
309                                    "backend_startup: bi_open %d failed!\n", i ));
310 #else
311                         Debug( LDAP_DEBUG_ANY,
312                                 "backend_startup: bi_open %d failed!\n",
313                                 i, 0, 0 );
314 #endif
315                         return rc;
316                 }
317         }
318
319         /* open each backend database */
320         for( i = 0; i < nBackendDB; i++ ) {
321                 /* append global access controls */
322                 acl_append( &backendDB[i].be_acl, global_acl );
323
324                 if ( backendDB[i].bd_info->bi_db_open ) {
325                         rc = backendDB[i].bd_info->bi_db_open(
326                                 &backendDB[i] );
327                 }
328
329                 if(rc != 0) {
330 #ifdef NEW_LOGGING
331                         LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
332                                 "backend_startup: bi_db_open(%d) failed! (%d)\n",
333                                 i, rc ));
334 #else
335                         Debug( LDAP_DEBUG_ANY,
336                                 "backend_startup: bi_db_open(%d) failed! (%d)\n",
337                                 i, rc, 0 );
338 #endif
339                         return rc;
340                 }
341         }
342
343         return rc;
344 }
345
346 int backend_num( Backend *be )
347 {
348         int i;
349
350         if( be == NULL ) return -1;
351
352         for( i = 0; i < nBackendDB; i++ ) {
353                 if( be == &backendDB[i] ) return i;
354         }
355         return -1;
356 }
357
358 int backend_shutdown( Backend *be )
359 {
360         int i;
361         int rc = 0;
362
363         if( be != NULL ) {
364                 /* shutdown a specific backend database */
365
366                 if ( be->bd_info->bi_nDB == 0 ) {
367                         /* no database of this type, we never opened it */
368                         return 0;
369                 }
370
371                 if ( be->bd_info->bi_db_close ) {
372                         be->bd_info->bi_db_close( be );
373                 }
374
375                 if( be->bd_info->bi_close ) {
376                         be->bd_info->bi_close( be->bd_info );
377                 }
378
379                 return 0;
380         }
381
382         /* close each backend database */
383         for( i = 0; i < nBackendDB; i++ ) {
384                 if ( backendDB[i].bd_info->bi_db_close ) {
385                         backendDB[i].bd_info->bi_db_close(
386                                 &backendDB[i] );
387                 }
388
389                 if(rc != 0) {
390 #ifdef NEW_LOGGING
391                         LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
392                                    "backend_shutdown: bi_close %s failed!\n",
393                                    backendDB[i].be_type ));
394 #else
395                         Debug( LDAP_DEBUG_ANY,
396                                 "backend_close: bi_close %s failed!\n",
397                                 backendDB[i].be_type, 0, 0 );
398 #endif
399                 }
400         }
401
402         /* close each backend type */
403         for( i = 0; i < nBackendInfo; i++ ) {
404                 if( backendInfo[i].bi_nDB == 0 ) {
405                         /* no database of this type */
406                         continue;
407                 }
408
409                 if( backendInfo[i].bi_close ) {
410                         backendInfo[i].bi_close(
411                                 &backendInfo[i] );
412                 }
413         }
414
415         return 0;
416 }
417
418 int backend_destroy(void)
419 {
420         int i;
421         BackendDB *bd;
422
423         /* destroy each backend database */
424         for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
425                 if ( bd->bd_info->bi_db_destroy ) {
426                         bd->bd_info->bi_db_destroy( bd );
427                 }
428                 ber_bvarray_free( bd->be_suffix );
429                 ber_bvarray_free( bd->be_nsuffix );
430                 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
431                 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
432                 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
433                 acl_destroy( bd->be_acl, global_acl );
434         }
435         free( backendDB );
436
437         /* destroy each backend type */
438         for( i = 0; i < nBackendInfo; i++ ) {
439                 if( backendInfo[i].bi_destroy ) {
440                         backendInfo[i].bi_destroy(
441                                 &backendInfo[i] );
442                 }
443         }
444
445 #ifdef SLAPD_MODULES
446         if (backendInfo != binfo) {
447            free(backendInfo);
448         }
449 #endif /* SLAPD_MODULES */
450
451         nBackendInfo = 0;
452         backendInfo = NULL;
453
454         return 0;
455 }
456
457 BackendInfo* backend_info(const char *type)
458 {
459         int i;
460
461         /* search for the backend type */
462         for( i = 0; i < nBackendInfo; i++ ) {
463                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
464                         return &backendInfo[i];
465                 }
466         }
467
468         return NULL;
469 }
470
471
472 BackendDB *
473 backend_db_init(
474     const char  *type
475 )
476 {
477         Backend *be;
478         BackendInfo *bi = backend_info(type);
479         int     rc = 0;
480
481         if( bi == NULL ) {
482                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
483                 return NULL;
484         }
485
486         backendDB = (BackendDB *) ch_realloc(
487                         (char *) backendDB,
488                     (nBackendDB + 1) * sizeof(Backend) );
489
490         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
491
492         be = &backends[nbackends++];
493
494         be->bd_info = bi;
495         be->be_def_limit = deflimit;
496         be->be_dfltaccess = global_default_access;
497
498         be->be_restrictops = global_restrictops;
499         be->be_requires = global_requires;
500         be->be_ssf_set = global_ssf_set;
501
502         /* assign a default depth limit for alias deref */
503         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
504
505         if(bi->bi_db_init) {
506                 rc = bi->bi_db_init( be );
507         }
508
509         if(rc != 0) {
510                 fprintf( stderr, "database init failed (%s)\n", type );
511                 nbackends--;
512                 return NULL;
513         }
514
515         bi->bi_nDB++;
516         return( be );
517 }
518
519 void
520 be_db_close( void )
521 {
522         int     i;
523
524         for ( i = 0; i < nbackends; i++ ) {
525                 if ( backends[i].bd_info->bi_db_close ) {
526                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
527                 }
528         }
529 }
530
531 Backend *
532 select_backend(
533         struct berval * dn,
534         int manageDSAit,
535         int noSubs )
536 {
537         int     i, j;
538         ber_len_t len, dnlen = dn->bv_len;
539         Backend *be = NULL;
540
541         for ( i = 0; i < nbackends; i++ ) {
542                 for ( j = 0; backends[i].be_nsuffix != NULL &&
543                     backends[i].be_nsuffix[j].bv_val != NULL; j++ )
544                 {
545                         if (( backends[i].be_flags & SLAP_BFLAG_GLUE_SUBORDINATE )
546                                 && noSubs )
547                         {
548                                 continue;
549                         }
550
551                         len = backends[i].be_nsuffix[j].bv_len;
552
553                         if ( len > dnlen ) {
554                                 /* suffix is longer than DN */
555                                 continue;
556                         }
557                         
558                         /*
559                          * input DN is normalized, so the separator check
560                          * need not look at escaping
561                          */
562                         if ( len && len < dnlen &&
563                                 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
564                         {
565                                 continue;
566                         }
567
568                         if ( strcmp( backends[i].be_nsuffix[j].bv_val,
569                                 &dn->bv_val[dnlen-len] ) == 0 )
570                         {
571                                 if( be == NULL ) {
572                                         be = &backends[i];
573
574                                         if( manageDSAit && len == dnlen ) {
575                                                 continue;
576                                         }
577                                 } else {
578                                         be = &backends[i];
579                                 }
580                                 return be;
581                         }
582                 }
583         }
584
585         return be;
586 }
587
588 int
589 be_issuffix(
590     Backend     *be,
591     struct berval       *bvsuffix
592 )
593 {
594         int     i;
595
596         for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL; i++ ) {
597                 if ( ber_bvcmp( &be->be_nsuffix[i], bvsuffix ) == 0 ) {
598                         return( 1 );
599                 }
600         }
601
602         return( 0 );
603 }
604
605 int
606 be_isroot( Backend *be, struct berval *ndn )
607 {
608         if ( !ndn->bv_len ) {
609                 return( 0 );
610         }
611
612         if ( !be->be_rootndn.bv_len ) {
613                 return( 0 );
614         }
615
616         return dn_match( &be->be_rootndn, ndn );
617 }
618
619 int
620 be_isupdate( Backend *be, struct berval *ndn )
621 {
622         if ( !ndn->bv_len ) {
623                 return( 0 );
624         }
625
626         if ( !be->be_update_ndn.bv_len ) {
627                 return( 0 );
628         }
629
630         return dn_match( &be->be_update_ndn, ndn );
631 }
632
633 struct berval *
634 be_root_dn( Backend *be )
635 {
636         return &be->be_rootdn;
637 }
638
639 int
640 be_isroot_pw( Backend *be,
641         Connection *conn,
642         struct berval *ndn,
643         struct berval *cred )
644 {
645         int result;
646
647         if ( ! be_isroot( be, ndn ) ) {
648                 return 0;
649         }
650
651         if( be->be_rootpw.bv_len == 0 ) {
652                 return 0;
653         }
654
655 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
656         ldap_pvt_thread_mutex_lock( &passwd_mutex );
657 #ifdef SLAPD_SPASSWD
658         lutil_passwd_sasl_conn = conn->c_sasl_context;
659 #endif
660 #endif
661
662         result = lutil_passwd( &be->be_rootpw, cred, NULL );
663
664 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
665 #ifdef SLAPD_SPASSWD
666         lutil_passwd_sasl_conn = NULL;
667 #endif
668         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
669 #endif
670
671         return result == 0;
672 }
673
674 int
675 be_entry_release_rw(
676         BackendDB *be,
677         Connection *conn,
678         Operation *op,
679         Entry *e,
680         int rw )
681 {
682         if ( be->be_release ) {
683                 /* free and release entry from backend */
684                 return be->be_release( be, conn, op, e, rw );
685         } else {
686                 /* free entry */
687                 entry_free( e );
688                 return 0;
689         }
690 }
691
692 int
693 backend_unbind(
694         Connection   *conn,
695         Operation    *op
696 )
697 {
698         int     i;
699
700         for ( i = 0; i < nbackends; i++ ) {
701                 if ( backends[i].be_unbind ) {
702                         (*backends[i].be_unbind)( &backends[i], conn, op );
703                 }
704         }
705
706         return 0;
707 }
708
709 int
710 backend_connection_init(
711         Connection   *conn
712 )
713 {
714         int     i;
715
716         for ( i = 0; i < nbackends; i++ ) {
717                 if ( backends[i].be_connection_init ) {
718                         (*backends[i].be_connection_init)( &backends[i], conn);
719                 }
720         }
721
722         return 0;
723 }
724
725 int
726 backend_connection_destroy(
727         Connection   *conn
728 )
729 {
730         int     i;
731
732         for ( i = 0; i < nbackends; i++ ) {
733                 if ( backends[i].be_connection_destroy ) {
734                         (*backends[i].be_connection_destroy)( &backends[i], conn);
735                 }
736         }
737
738         return 0;
739 }
740
741 static int
742 backend_check_controls(
743         Backend *be,
744         Connection *conn,
745         Operation *op,
746         const char **text )
747 {
748         LDAPControl **ctrls;
749         ctrls = op->o_ctrls;
750         if( ctrls == NULL ) {
751                 return LDAP_SUCCESS;
752         }
753
754         for( ; *ctrls != NULL ; ctrls++ ) {
755                 if( (*ctrls)->ldctl_iscritical &&
756                         !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
757                 {
758                         *text = "control unavailable in context";
759                         return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
760                 }
761         }
762
763         return LDAP_SUCCESS;
764 }
765
766 int
767 backend_check_restrictions(
768         Backend *be,
769         Connection *conn,
770         Operation *op,
771         struct berval *opdata,
772         const char **text )
773 {
774         int rc;
775         slap_mask_t restrictops;
776         slap_mask_t requires;
777         slap_mask_t opflag;
778         slap_ssf_set_t *ssf;
779         int updateop = 0;
780         int starttls = 0;
781         int session = 0;
782
783         if( be ) {
784                 rc = backend_check_controls( be, conn, op, text );
785
786                 if( rc != LDAP_SUCCESS ) {
787                         return rc;
788                 }
789
790                 restrictops = be->be_restrictops;
791                 requires = be->be_requires;
792                 ssf = &be->be_ssf_set;
793
794         } else {
795                 restrictops = global_restrictops;
796                 requires = global_requires;
797                 ssf = &global_ssf_set;
798         }
799
800         switch( op->o_tag ) {
801         case LDAP_REQ_ADD:
802                 opflag = SLAP_RESTRICT_OP_ADD;
803                 updateop++;
804                 break;
805         case LDAP_REQ_BIND:
806                 opflag = SLAP_RESTRICT_OP_BIND;
807                 session++;
808                 break;
809         case LDAP_REQ_COMPARE:
810                 opflag = SLAP_RESTRICT_OP_COMPARE;
811                 break;
812         case LDAP_REQ_DELETE:
813                 updateop++;
814                 opflag = SLAP_RESTRICT_OP_DELETE;
815                 break;
816         case LDAP_REQ_EXTENDED:
817                 opflag = SLAP_RESTRICT_OP_EXTENDED;
818
819                 if( !opdata ) {
820                         /* treat unspecified as a modify */
821                         opflag = SLAP_RESTRICT_OP_MODIFY;
822                         updateop++;
823                         break;
824                 }
825
826                 {
827                         struct berval bv = BER_BVC( LDAP_EXOP_START_TLS );
828                         if( ber_bvcmp( opdata, &bv ) == 0 ) {
829                                 session++;
830                                 starttls++;
831                                 break;
832                         }
833                 }
834
835                 {
836                         struct berval bv = BER_BVC( LDAP_EXOP_X_WHO_AM_I );
837                         if( ber_bvcmp( opdata, &bv ) == 0 ) {
838                                 break;
839                         }
840                 }
841
842                 /* treat everything else as a modify */
843                 opflag = SLAP_RESTRICT_OP_MODIFY;
844                 updateop++;
845                 break;
846
847         case LDAP_REQ_MODIFY:
848                 updateop++;
849                 opflag = SLAP_RESTRICT_OP_MODIFY;
850                 break;
851         case LDAP_REQ_RENAME:
852                 updateop++;
853                 opflag = SLAP_RESTRICT_OP_RENAME;
854                 break;
855         case LDAP_REQ_SEARCH:
856                 opflag = SLAP_RESTRICT_OP_SEARCH;
857                 break;
858         case LDAP_REQ_UNBIND:
859                 session++;
860                 opflag = 0;
861                 break;
862         default:
863                 *text = "restrict operations internal error";
864                 return LDAP_OTHER;
865         }
866
867         if ( !starttls ) {
868                 /* these checks don't apply to StartTLS */
869
870                 if( op->o_transport_ssf < ssf->sss_transport ) {
871                         *text = "transport confidentiality required";
872                         return LDAP_CONFIDENTIALITY_REQUIRED;
873                 }
874
875                 if( op->o_tls_ssf < ssf->sss_tls ) {
876                         *text = "TLS confidentiality required";
877                         return LDAP_CONFIDENTIALITY_REQUIRED;
878                 }
879
880                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
881                         /* these checks don't apply to SASL bind */
882
883                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
884                                 *text = "SASL confidentiality required";
885                                 return LDAP_CONFIDENTIALITY_REQUIRED;
886                         }
887
888                         if( op->o_ssf < ssf->sss_ssf ) {
889                                 *text = "confidentiality required";
890                                 return LDAP_CONFIDENTIALITY_REQUIRED;
891                         }
892                 }
893
894                 if( updateop ) {
895                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
896                                 *text = "transport update confidentiality required";
897                                 return LDAP_CONFIDENTIALITY_REQUIRED;
898                         }
899
900                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
901                                 *text = "TLS update confidentiality required";
902                                 return LDAP_CONFIDENTIALITY_REQUIRED;
903                         }
904
905                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
906                                 *text = "SASL update confidentiality required";
907                                 return LDAP_CONFIDENTIALITY_REQUIRED;
908                         }
909
910                         if( op->o_ssf < ssf->sss_update_ssf ) {
911                                 *text = "update confidentiality required";
912                                 return LDAP_CONFIDENTIALITY_REQUIRED;
913                         }
914
915                         if( op->o_ndn.bv_len == 0 ) {
916                                 *text = "modifications require authentication";
917                                 return LDAP_OPERATIONS_ERROR;
918                         }
919                 }
920         }
921
922         if ( !session ) {
923                 /* these checks don't apply to Bind, StartTLS, or Unbind */
924
925                 if( requires & SLAP_REQUIRE_STRONG ) {
926                         /* should check mechanism */
927                         if( ( op->o_transport_ssf < ssf->sss_transport
928                                 && op->o_authmech.bv_len == 0 ) || op->o_dn.bv_len == 0 )
929                         {
930                                 *text = "strong authentication required";
931                                 return LDAP_STRONG_AUTH_REQUIRED;
932                         }
933                 }
934
935                 if( requires & SLAP_REQUIRE_SASL ) {
936                         if( op->o_authmech.bv_len == 0 || op->o_dn.bv_len == 0 ) {
937                                 *text = "SASL authentication required";
938                                 return LDAP_STRONG_AUTH_REQUIRED;
939                         }
940                 }
941                         
942                 if( requires & SLAP_REQUIRE_AUTHC ) {
943                         if( op->o_dn.bv_len == 0 ) {
944                                 *text = "authentication required";
945                                 return LDAP_UNWILLING_TO_PERFORM;
946                         }
947                 }
948
949                 if( requires & SLAP_REQUIRE_BIND ) {
950                         int version;
951                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
952                         version = conn->c_protocol;
953                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
954
955                         if( !version ) {
956                                 /* no bind has occurred */
957                                 *text = "BIND required";
958                                 return LDAP_OPERATIONS_ERROR;
959                         }
960                 }
961
962                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
963                         if( op->o_protocol < LDAP_VERSION3 ) {
964                                 /* no bind has occurred */
965                                 *text = "operation restricted to LDAPv3 clients";
966                                 return LDAP_OPERATIONS_ERROR;
967                         }
968                 }
969         }
970
971         if( restrictops & opflag ) {
972                 if( restrictops == SLAP_RESTRICT_OP_READS ) {
973                         *text = "read operations restricted";
974                 } else {
975                         *text = "operation restricted";
976                 }
977                 return LDAP_UNWILLING_TO_PERFORM;
978         }
979
980         return LDAP_SUCCESS;
981 }
982
983 int backend_check_referrals(
984         Backend *be,
985         Connection *conn,
986         Operation *op,
987         struct berval *dn,
988         struct berval *ndn )
989 {
990         int rc = LDAP_SUCCESS;
991
992         if( be->be_chk_referrals ) {
993                 const char *text;
994
995                 rc = be->be_chk_referrals( be,
996                         conn, op, dn, ndn, &text );
997
998                 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
999                         send_ldap_result( conn, op, rc,
1000                                 NULL, text, NULL, NULL );
1001                 }
1002         }
1003
1004         return rc;
1005 }
1006
1007 int 
1008 backend_group(
1009         Backend *be,
1010         Connection *conn,
1011         Operation *op,
1012         Entry   *target,
1013         struct berval *gr_ndn,
1014         struct berval *op_ndn,
1015         ObjectClass *group_oc,
1016         AttributeDescription *group_at
1017 )
1018 {
1019         GroupAssertion *g;
1020
1021         if ( op->o_abandon ) return SLAPD_ABANDON;
1022
1023         if ( !dn_match( &target->e_nname, gr_ndn ) ) {
1024                 /* we won't attempt to send it to a different backend */
1025                 
1026                 be = select_backend( gr_ndn, 0, 0 );
1027
1028                 if (be == NULL) {
1029                         return LDAP_NO_SUCH_OBJECT;
1030                 }
1031         } 
1032
1033         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1034         for (g = conn->c_groups; g; g=g->ga_next) {
1035                 if (g->ga_be != be || g->ga_oc != group_oc ||
1036                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1037                         continue;
1038                 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1039                         break;
1040         }
1041         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1042         if (g) {
1043                 return g->ga_res;
1044         }
1045
1046         if( be->be_group ) {
1047                 int res = be->be_group( be, conn, op,
1048                         target, gr_ndn, op_ndn,
1049                         group_oc, group_at );
1050                 
1051                 if (op->o_tag != LDAP_REQ_BIND) {
1052                         g = ch_malloc(sizeof(GroupAssertion) + gr_ndn->bv_len);
1053                         g->ga_be = be;
1054                         g->ga_oc = group_oc;
1055                         g->ga_at = group_at;
1056                         g->ga_res = res;
1057                         g->ga_len = gr_ndn->bv_len;
1058                         strcpy(g->ga_ndn, gr_ndn->bv_val);
1059                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1060                         g->ga_next = conn->c_groups;
1061                         conn->c_groups = g;
1062                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1063                 }
1064
1065                 return res;
1066         }
1067
1068         return LDAP_UNWILLING_TO_PERFORM;
1069 }
1070
1071 int 
1072 backend_attribute(
1073         Backend *be,
1074         Connection *conn,
1075         Operation *op,
1076         Entry   *target,
1077         struct berval   *edn,
1078         AttributeDescription *entry_at,
1079         BerVarray *vals
1080 )
1081 {
1082         if ( target == NULL || !dn_match( &target->e_nname, edn ) ) {
1083                 /* we won't attempt to send it to a different backend */
1084                 
1085                 be = select_backend( edn, 0, 0 );
1086
1087                 if (be == NULL) {
1088                         return LDAP_NO_SUCH_OBJECT;
1089                 }
1090         } 
1091
1092         if( be->be_attribute ) {
1093                 return be->be_attribute( be, conn, op, target, edn,
1094                         entry_at, vals );
1095         }
1096
1097         return LDAP_UNWILLING_TO_PERFORM;
1098 }
1099
1100 Attribute *backend_operational(
1101         Backend *be,
1102         Connection *conn,
1103         Operation *op,
1104         Entry *e,
1105         AttributeName *attrs,
1106         int opattrs     )
1107 {
1108         Attribute *a = NULL, **ap = &a;
1109
1110 #ifdef SLAPD_SCHEMA_DN
1111         *ap = slap_operational_subschemaSubentry();
1112         ap = &(*ap)->a_next;
1113 #endif
1114
1115         /*
1116          * If operational attributes (allegedly) are required, 
1117          * and the backend supports specific operational attributes, 
1118          * add them to the attribute list
1119          */
1120         if ( ( opattrs || attrs ) && be && be->be_operational != NULL ) {
1121                 ( void )be->be_operational( be, conn, op, e, attrs, opattrs, ap );
1122         }
1123
1124         return a;
1125 }
1126