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