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