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