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