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