]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
3f4f2f8867ef0ea60244a2bc95f216e5523bd70f
[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                                                 !SLAP_GLUE_SUBORDINATE( be ) ) {
570                                                 continue;
571                                         }
572                                 } else {
573                                         be = &backends[i];
574                                 }
575                                 return be;
576                         }
577                 }
578         }
579
580         return be;
581 }
582
583 int
584 be_issuffix(
585     Backend     *be,
586     struct berval       *bvsuffix
587 )
588 {
589         int     i;
590
591         for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL; i++ ) {
592                 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
593                         return( 1 );
594                 }
595         }
596
597         return( 0 );
598 }
599
600 int
601 be_isroot( Backend *be, struct berval *ndn )
602 {
603         if ( !ndn->bv_len ) {
604                 return( 0 );
605         }
606
607         if ( !be->be_rootndn.bv_len ) {
608                 return( 0 );
609         }
610
611         return dn_match( &be->be_rootndn, ndn );
612 }
613
614 int
615 be_isupdate( Backend *be, struct berval *ndn )
616 {
617         if ( !ndn->bv_len ) {
618                 return( 0 );
619         }
620
621         if ( !be->be_update_ndn.bv_len ) {
622                 return( 0 );
623         }
624
625         return dn_match( &be->be_update_ndn, ndn );
626 }
627
628 struct berval *
629 be_root_dn( Backend *be )
630 {
631         return &be->be_rootdn;
632 }
633
634 int
635 be_isroot_pw( Backend *be,
636         Connection *conn,
637         struct berval *ndn,
638         struct berval *cred )
639 {
640         int result;
641         char *errmsg;
642
643         if ( ! be_isroot( be, ndn ) ) {
644                 return 0;
645         }
646
647         if( be->be_rootpw.bv_len == 0 ) {
648                 return 0;
649         }
650
651 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
652         ldap_pvt_thread_mutex_lock( &passwd_mutex );
653 #ifdef SLAPD_SPASSWD
654         lutil_passwd_sasl_conn = conn->c_sasl_context;
655 #endif
656 #endif
657
658         result = lutil_passwd( &be->be_rootpw, cred, NULL, NULL );
659
660 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
661 #ifdef SLAPD_SPASSWD
662         lutil_passwd_sasl_conn = NULL;
663 #endif
664         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
665 #endif
666
667         return result == 0;
668 }
669
670 int
671 be_entry_release_rw(
672         BackendDB *be,
673         Connection *conn,
674         Operation *op,
675         Entry *e,
676         int rw )
677 {
678         if ( be->be_release ) {
679                 /* free and release entry from backend */
680                 return be->be_release( be, conn, op, e, rw );
681         } else {
682                 /* free entry */
683                 entry_free( e );
684                 return 0;
685         }
686 }
687
688 int
689 backend_unbind(
690         Connection   *conn,
691         Operation    *op
692 )
693 {
694         int             i;
695 #if defined( LDAP_SLAPI )
696         Slapi_PBlock *pb = op->o_pb;
697
698         int     rc;
699         slapi_x_connection_set_pb( pb, conn );
700         slapi_x_operation_set_pb( pb, op );
701 #endif /* defined( LDAP_SLAPI ) */
702
703         for ( i = 0; i < nbackends; i++ ) {
704 #if defined( LDAP_SLAPI )
705                 slapi_pblock_set( pb, SLAPI_BACKEND, (void *)&backends[i] );
706                 rc = doPluginFNs( &backends[i], SLAPI_PLUGIN_PRE_UNBIND_FN,
707                                 (Slapi_PBlock *)pb );
708                 if ( rc != 0 ) {
709                         /*
710                          * A preoperation plugin failure will abort the
711                          * entire operation.
712                          */
713 #ifdef NEW_LOGGING
714                         LDAP_LOG( OPERATION, INFO, "do_bind: Unbind preoperation plugin "
715                                         "failed\n", 0, 0, 0);
716 #else
717                         Debug(LDAP_DEBUG_TRACE, "do_bind: Unbind preoperation plugin "
718                                         "failed.\n", 0, 0, 0);
719 #endif
720                         return 0;
721                 }
722 #endif /* defined( LDAP_SLAPI ) */
723
724                 if ( backends[i].be_unbind ) {
725                         (*backends[i].be_unbind)( &backends[i], conn, op );
726                 }
727
728 #if defined( LDAP_SLAPI )
729                 if ( doPluginFNs( &backends[i], SLAPI_PLUGIN_POST_UNBIND_FN,
730                                 (Slapi_PBlock *)pb ) != 0 ) {
731 #ifdef NEW_LOGGING
732                         LDAP_LOG( OPERATION, INFO, "do_unbind: Unbind postoperation plugins "
733                                         "failed\n", 0, 0, 0);
734 #else
735                         Debug(LDAP_DEBUG_TRACE, "do_unbind: Unbind postoperation plugins "
736                                         "failed.\n", 0, 0, 0);
737 #endif
738                 }
739 #endif /* defined( LDAP_SLAPI ) */
740         }
741
742         return 0;
743 }
744
745 int
746 backend_connection_init(
747         Connection   *conn
748 )
749 {
750         int     i;
751
752         for ( i = 0; i < nbackends; i++ ) {
753                 if ( backends[i].be_connection_init ) {
754                         (*backends[i].be_connection_init)( &backends[i], conn);
755                 }
756         }
757
758         return 0;
759 }
760
761 int
762 backend_connection_destroy(
763         Connection   *conn
764 )
765 {
766         int     i;
767
768         for ( i = 0; i < nbackends; i++ ) {
769                 if ( backends[i].be_connection_destroy ) {
770                         (*backends[i].be_connection_destroy)( &backends[i], conn);
771                 }
772         }
773
774         return 0;
775 }
776
777 static int
778 backend_check_controls(
779         Backend *be,
780         Connection *conn,
781         Operation *op,
782         const char **text )
783 {
784         LDAPControl **ctrls = op->o_ctrls;
785
786         if( ctrls == NULL ) return LDAP_SUCCESS;
787
788         for( ; *ctrls != NULL ; ctrls++ ) {
789                 if( (*ctrls)->ldctl_iscritical &&
790                         !ldap_charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
791                 {
792                         *text = "control unavailable in context";
793                         return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
794                 }
795         }
796
797         return LDAP_SUCCESS;
798 }
799
800 int
801 backend_check_restrictions(
802         Backend *be,
803         Connection *conn,
804         Operation *op,
805         struct berval *opdata,
806         const char **text )
807 {
808         int rc;
809         slap_mask_t restrictops;
810         slap_mask_t requires;
811         slap_mask_t opflag;
812         slap_ssf_set_t *ssf;
813         int updateop = 0;
814         int starttls = 0;
815         int session = 0;
816
817         if( be ) {
818                 rc = backend_check_controls( be, conn, op, text );
819
820                 if( rc != LDAP_SUCCESS ) {
821                         return rc;
822                 }
823
824                 restrictops = be->be_restrictops;
825                 requires = be->be_requires;
826                 ssf = &be->be_ssf_set;
827
828         } else {
829                 restrictops = global_restrictops;
830                 requires = global_requires;
831                 ssf = &global_ssf_set;
832         }
833
834         switch( op->o_tag ) {
835         case LDAP_REQ_ADD:
836                 opflag = SLAP_RESTRICT_OP_ADD;
837                 updateop++;
838                 break;
839         case LDAP_REQ_BIND:
840                 opflag = SLAP_RESTRICT_OP_BIND;
841                 session++;
842                 break;
843         case LDAP_REQ_COMPARE:
844                 opflag = SLAP_RESTRICT_OP_COMPARE;
845                 break;
846         case LDAP_REQ_DELETE:
847                 updateop++;
848                 opflag = SLAP_RESTRICT_OP_DELETE;
849                 break;
850         case LDAP_REQ_EXTENDED:
851                 opflag = SLAP_RESTRICT_OP_EXTENDED;
852
853                 if( !opdata ) {
854                         /* treat unspecified as a modify */
855                         opflag = SLAP_RESTRICT_OP_MODIFY;
856                         updateop++;
857                         break;
858                 }
859
860                 {
861                         struct berval bv = BER_BVC( LDAP_EXOP_START_TLS );
862                         if( bvmatch( opdata, &bv ) ) {
863                                 session++;
864                                 starttls++;
865                                 break;
866                         }
867                 }
868
869                 {
870                         struct berval bv = BER_BVC( LDAP_EXOP_X_WHO_AM_I );
871                         if( bvmatch( opdata, &bv ) ) {
872                                 break;
873                         }
874                 }
875
876 #ifdef LDAP_EXOP_X_CANCEL
877                 {
878                         struct berval bv = BER_BVC( LDAP_EXOP_X_CANCEL );
879                         if ( bvmatch( opdata, &bv ) ) {
880                                 break;
881                         }
882                 }
883 #endif
884
885                 /* treat everything else as a modify */
886                 opflag = SLAP_RESTRICT_OP_MODIFY;
887                 updateop++;
888                 break;
889
890         case LDAP_REQ_MODIFY:
891                 updateop++;
892                 opflag = SLAP_RESTRICT_OP_MODIFY;
893                 break;
894         case LDAP_REQ_RENAME:
895                 updateop++;
896                 opflag = SLAP_RESTRICT_OP_RENAME;
897                 break;
898         case LDAP_REQ_SEARCH:
899                 opflag = SLAP_RESTRICT_OP_SEARCH;
900                 break;
901         case LDAP_REQ_UNBIND:
902                 session++;
903                 opflag = 0;
904                 break;
905         default:
906                 *text = "restrict operations internal error";
907                 return LDAP_OTHER;
908         }
909
910         if ( !starttls ) {
911                 /* these checks don't apply to StartTLS */
912
913                 if( op->o_transport_ssf < ssf->sss_transport ) {
914                         *text = "transport confidentiality required";
915                         return LDAP_CONFIDENTIALITY_REQUIRED;
916                 }
917
918                 if( op->o_tls_ssf < ssf->sss_tls ) {
919                         *text = "TLS confidentiality required";
920                         return LDAP_CONFIDENTIALITY_REQUIRED;
921                 }
922
923
924                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
925                         /* simple bind specific check */
926                         if( op->o_ssf < ssf->sss_simple_bind ) {
927                                 *text = "confidentiality required";
928                                 return LDAP_CONFIDENTIALITY_REQUIRED;
929                         }
930                 }
931
932                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
933                         /* these checks don't apply to SASL bind */
934
935                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
936                                 *text = "SASL confidentiality required";
937                                 return LDAP_CONFIDENTIALITY_REQUIRED;
938                         }
939
940                         if( op->o_ssf < ssf->sss_ssf ) {
941                                 *text = "confidentiality required";
942                                 return LDAP_CONFIDENTIALITY_REQUIRED;
943                         }
944                 }
945
946                 if( updateop ) {
947                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
948                                 *text = "transport update confidentiality required";
949                                 return LDAP_CONFIDENTIALITY_REQUIRED;
950                         }
951
952                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
953                                 *text = "TLS update confidentiality required";
954                                 return LDAP_CONFIDENTIALITY_REQUIRED;
955                         }
956
957                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
958                                 *text = "SASL update confidentiality required";
959                                 return LDAP_CONFIDENTIALITY_REQUIRED;
960                         }
961
962                         if( op->o_ssf < ssf->sss_update_ssf ) {
963                                 *text = "update confidentiality required";
964                                 return LDAP_CONFIDENTIALITY_REQUIRED;
965                         }
966
967                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
968                                 op->o_ndn.bv_len == 0 )
969                         {
970                                 *text = "modifications require authentication";
971                                 return LDAP_STRONG_AUTH_REQUIRED;
972                         }
973
974 #ifdef SLAP_X_LISTENER_MOD
975                         if ( ! ( conn->c_listener->sl_perms & S_IWUSR ) ) {
976                                 /* no "w" mode means readonly */
977                                 *text = "modifications not allowed on this listener";
978                                 return LDAP_UNWILLING_TO_PERFORM;
979                         }
980 #endif /* SLAP_X_LISTENER_MOD */
981                 }
982         }
983
984         if ( !session ) {
985                 /* these checks don't apply to Bind, StartTLS, or Unbind */
986
987                 if( requires & SLAP_REQUIRE_STRONG ) {
988                         /* should check mechanism */
989                         if( ( op->o_transport_ssf < ssf->sss_transport
990                                 && op->o_authmech.bv_len == 0 ) || op->o_dn.bv_len == 0 )
991                         {
992                                 *text = "strong authentication required";
993                                 return LDAP_STRONG_AUTH_REQUIRED;
994                         }
995                 }
996
997                 if( requires & SLAP_REQUIRE_SASL ) {
998                         if( op->o_authmech.bv_len == 0 || op->o_dn.bv_len == 0 ) {
999                                 *text = "SASL authentication required";
1000                                 return LDAP_STRONG_AUTH_REQUIRED;
1001                         }
1002                 }
1003                         
1004                 if( requires & SLAP_REQUIRE_AUTHC ) {
1005                         if( op->o_dn.bv_len == 0 ) {
1006                                 *text = "authentication required";
1007                                 return LDAP_UNWILLING_TO_PERFORM;
1008                         }
1009                 }
1010
1011                 if( requires & SLAP_REQUIRE_BIND ) {
1012                         int version;
1013                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1014                         version = conn->c_protocol;
1015                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1016
1017                         if( !version ) {
1018                                 /* no bind has occurred */
1019                                 *text = "BIND required";
1020                                 return LDAP_OPERATIONS_ERROR;
1021                         }
1022                 }
1023
1024                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1025                         if( op->o_protocol < LDAP_VERSION3 ) {
1026                                 /* no bind has occurred */
1027                                 *text = "operation restricted to LDAPv3 clients";
1028                                 return LDAP_OPERATIONS_ERROR;
1029                         }
1030                 }
1031
1032 #ifdef SLAP_X_LISTENER_MOD
1033                 if ( !starttls && op->o_dn.bv_len == 0 ) {
1034                         if ( ! ( conn->c_listener->sl_perms & S_IXUSR ) ) {
1035                                 /* no "x" mode means bind required */
1036                                 *text = "bind required on this listener";
1037                                 return LDAP_STRONG_AUTH_REQUIRED;
1038                         }
1039                 }
1040
1041                 if ( !starttls && !updateop ) {
1042                         if ( ! ( conn->c_listener->sl_perms & S_IRUSR ) ) {
1043                                 /* no "r" mode means no read */
1044                                 *text = "read not allowed on this listener";
1045                                 return LDAP_UNWILLING_TO_PERFORM;
1046                         }
1047                 }
1048 #endif /* SLAP_X_LISTENER_MOD */
1049
1050         }
1051
1052         if( restrictops & opflag ) {
1053                 if( restrictops == SLAP_RESTRICT_OP_READS ) {
1054                         *text = "read operations restricted";
1055                 } else {
1056                         *text = "operation restricted";
1057                 }
1058                 return LDAP_UNWILLING_TO_PERFORM;
1059         }
1060
1061         return LDAP_SUCCESS;
1062 }
1063
1064 int backend_check_referrals(
1065         Backend *be,
1066         Connection *conn,
1067         Operation *op,
1068         struct berval *dn,
1069         struct berval *ndn )
1070 {
1071         int rc = LDAP_SUCCESS;
1072
1073         if( be->be_chk_referrals ) {
1074                 const char *text;
1075
1076                 rc = be->be_chk_referrals( be,
1077                         conn, op, dn, ndn, &text );
1078
1079                 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
1080                         send_ldap_result( conn, op, rc,
1081                                 NULL, text, NULL, NULL );
1082                 }
1083         }
1084
1085         return rc;
1086 }
1087
1088 int 
1089 backend_group(
1090         Backend *be,
1091         Connection *conn,
1092         Operation *op,
1093         Entry   *target,
1094         struct berval *gr_ndn,
1095         struct berval *op_ndn,
1096         ObjectClass *group_oc,
1097         AttributeDescription *group_at
1098 )
1099 {
1100         GroupAssertion *g;
1101
1102         if ( op->o_abandon ) return SLAPD_ABANDON;
1103
1104         if ( !dn_match( &target->e_nname, gr_ndn ) ) {
1105                 /* we won't attempt to send it to a different backend */
1106                 
1107                 be = select_backend( gr_ndn, 0, 0 );
1108
1109                 if (be == NULL) {
1110                         return LDAP_NO_SUCH_OBJECT;
1111                 }
1112         } 
1113
1114         for (g = op->o_groups; g; g=g->ga_next) {
1115                 if (g->ga_be != be || g->ga_oc != group_oc ||
1116                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1117                         continue;
1118                 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1119                         break;
1120         }
1121
1122         if (g) {
1123                 return g->ga_res;
1124         }
1125
1126         if( be->be_group ) {
1127                 int res = be->be_group( be, conn, op,
1128                         target, gr_ndn, op_ndn,
1129                         group_oc, group_at );
1130                 
1131                 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1132                         g = ch_malloc(sizeof(GroupAssertion) + gr_ndn->bv_len);
1133                         g->ga_be = be;
1134                         g->ga_oc = group_oc;
1135                         g->ga_at = group_at;
1136                         g->ga_res = res;
1137                         g->ga_len = gr_ndn->bv_len;
1138                         strcpy(g->ga_ndn, gr_ndn->bv_val);
1139                         g->ga_next = op->o_groups;
1140                         op->o_groups = g;
1141                 }
1142
1143                 return res;
1144         }
1145
1146         return LDAP_UNWILLING_TO_PERFORM;
1147 }
1148
1149 int 
1150 backend_attribute(
1151         Backend *be,
1152         Connection *conn,
1153         Operation *op,
1154         Entry   *target,
1155         struct berval   *edn,
1156         AttributeDescription *entry_at,
1157         BerVarray *vals
1158 )
1159 {
1160         if ( target == NULL || !dn_match( &target->e_nname, edn ) ) {
1161                 /* we won't attempt to send it to a different backend */
1162                 
1163                 be = select_backend( edn, 0, 0 );
1164
1165                 if (be == NULL) {
1166                         return LDAP_NO_SUCH_OBJECT;
1167                 }
1168         } 
1169
1170         if( be->be_attribute ) {
1171                 return be->be_attribute( be, conn, op, target, edn,
1172                         entry_at, vals );
1173         }
1174
1175         return LDAP_UNWILLING_TO_PERFORM;
1176 }
1177
1178 Attribute *backend_operational(
1179         Backend *be,
1180         Connection *conn,
1181         Operation *op,
1182         Entry *e,
1183         AttributeName *attrs,
1184         int opattrs     )
1185 {
1186         Attribute *a = NULL, **ap = &a;
1187
1188         /*
1189          * If operational attributes (allegedly) are required, 
1190          * and the backend supports specific operational attributes, 
1191          * add them to the attribute list
1192          */
1193         if ( opattrs || ( attrs &&
1194                 ad_inlist( slap_schema.si_ad_subschemaSubentry, attrs )) ) {
1195                 *ap = slap_operational_subschemaSubentry( be );
1196                 ap = &(*ap)->a_next;
1197         }
1198
1199         if ( ( opattrs || attrs ) && be && be->be_operational != NULL ) {
1200                 ( void )be->be_operational( be, conn, op, e, attrs, opattrs, ap );
1201         }
1202
1203         return a;
1204 }
1205