]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
fix schema definitions (duplicate objectClass OID still there)
[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( Operation *op )
635 {
636         int result;
637
638         if ( ! be_isroot( op->o_bd, &op->o_req_ndn ) ) {
639                 return 0;
640         }
641
642         if( op->o_bd->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 = op->o_conn->c_sasl_context;
650 #endif
651 #endif
652
653         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_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         Operation *op,
668         Entry *e,
669         int rw )
670 {
671         if ( op->o_bd->be_release ) {
672                 /* free and release entry from backend */
673                 return op->o_bd->be_release( op, e, rw );
674         } else {
675                 /* free entry */
676                 entry_free( e );
677                 return 0;
678         }
679 }
680
681 int
682 backend_unbind( Operation *op, SlapReply *rs )
683 {
684         int             i;
685 #if defined( LDAP_SLAPI )
686         Slapi_PBlock *pb = op->o_pb;
687
688         int     rc;
689         slapi_x_pblock_set_operation( pb, op );
690 #endif /* defined( LDAP_SLAPI ) */
691
692         for ( i = 0; i < nbackends; i++ ) {
693 #if defined( LDAP_SLAPI )
694                 slapi_pblock_set( pb, SLAPI_BACKEND, (void *)&backends[i] );
695                 rc = doPluginFNs( &backends[i], SLAPI_PLUGIN_PRE_UNBIND_FN,
696                                 (Slapi_PBlock *)pb );
697                 if ( rc != 0 ) {
698                         /*
699                          * A preoperation plugin failure will abort the
700                          * entire operation.
701                          */
702 #ifdef NEW_LOGGING
703                         LDAP_LOG( OPERATION, INFO, "do_bind: Unbind preoperation plugin "
704                                         "failed\n", 0, 0, 0);
705 #else
706                         Debug(LDAP_DEBUG_TRACE, "do_bind: Unbind preoperation plugin "
707                                         "failed.\n", 0, 0, 0);
708 #endif
709                         return 0;
710                 }
711 #endif /* defined( LDAP_SLAPI ) */
712
713                 if ( backends[i].be_unbind ) {
714                         op->o_bd = &backends[i];
715                         (*backends[i].be_unbind)( op, rs );
716                 }
717
718 #if defined( LDAP_SLAPI )
719                 if ( doPluginFNs( &backends[i], SLAPI_PLUGIN_POST_UNBIND_FN,
720                                 (Slapi_PBlock *)pb ) != 0 ) {
721 #ifdef NEW_LOGGING
722                         LDAP_LOG( OPERATION, INFO, "do_unbind: Unbind postoperation plugins "
723                                         "failed\n", 0, 0, 0);
724 #else
725                         Debug(LDAP_DEBUG_TRACE, "do_unbind: Unbind postoperation plugins "
726                                         "failed.\n", 0, 0, 0);
727 #endif
728                 }
729 #endif /* defined( LDAP_SLAPI ) */
730         }
731
732         return 0;
733 }
734
735 int
736 backend_connection_init(
737         Connection   *conn
738 )
739 {
740         int     i;
741
742         for ( i = 0; i < nbackends; i++ ) {
743                 if ( backends[i].be_connection_init ) {
744                         (*backends[i].be_connection_init)( &backends[i], conn);
745                 }
746         }
747
748         return 0;
749 }
750
751 int
752 backend_connection_destroy(
753         Connection   *conn
754 )
755 {
756         int     i;
757
758         for ( i = 0; i < nbackends; i++ ) {
759                 if ( backends[i].be_connection_destroy ) {
760                         (*backends[i].be_connection_destroy)( &backends[i], conn);
761                 }
762         }
763
764         return 0;
765 }
766
767 static int
768 backend_check_controls(
769         Operation *op,
770         SlapReply *rs )
771 {
772         LDAPControl **ctrls = op->o_ctrls;
773         rs->sr_err = LDAP_SUCCESS;
774
775         if( ctrls ) {
776                 for( ; *ctrls != NULL ; ctrls++ ) {
777                         if( (*ctrls)->ldctl_iscritical &&
778                                 !ldap_charray_inlist( op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
779                         {
780                                 rs->sr_text = "control unavailable in context";
781                                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
782                                 break;
783                         }
784                 }
785         }
786
787         return rs->sr_err;
788 }
789
790 int
791 backend_check_restrictions(
792         Operation *op,
793         SlapReply *rs,
794         struct berval *opdata )
795 {
796         slap_mask_t restrictops;
797         slap_mask_t requires;
798         slap_mask_t opflag;
799         slap_ssf_set_t *ssf;
800         int updateop = 0;
801         int starttls = 0;
802         int session = 0;
803
804         if( op->o_bd ) {
805                 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
806                         return rs->sr_err;
807                 }
808
809                 restrictops = op->o_bd->be_restrictops;
810                 requires = op->o_bd->be_requires;
811                 ssf = &op->o_bd->be_ssf_set;
812
813         } else {
814                 restrictops = global_restrictops;
815                 requires = global_requires;
816                 ssf = &global_ssf_set;
817         }
818
819         switch( op->o_tag ) {
820         case LDAP_REQ_ADD:
821                 opflag = SLAP_RESTRICT_OP_ADD;
822                 updateop++;
823                 break;
824         case LDAP_REQ_BIND:
825                 opflag = SLAP_RESTRICT_OP_BIND;
826                 session++;
827                 break;
828         case LDAP_REQ_COMPARE:
829                 opflag = SLAP_RESTRICT_OP_COMPARE;
830                 break;
831         case LDAP_REQ_DELETE:
832                 updateop++;
833                 opflag = SLAP_RESTRICT_OP_DELETE;
834                 break;
835         case LDAP_REQ_EXTENDED:
836                 opflag = SLAP_RESTRICT_OP_EXTENDED;
837
838                 if( !opdata ) {
839                         /* treat unspecified as a modify */
840                         opflag = SLAP_RESTRICT_OP_MODIFY;
841                         updateop++;
842                         break;
843                 }
844
845                 {
846                         if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
847                                 session++;
848                                 starttls++;
849                                 break;
850                         }
851                 }
852
853                 {
854                         if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
855                                 break;
856                         }
857                 }
858
859 #ifdef LDAP_EXOP_X_CANCEL
860                 {
861                         if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
862                                 break;
863                         }
864                 }
865 #endif
866
867                 /* treat everything else as a modify */
868                 opflag = SLAP_RESTRICT_OP_MODIFY;
869                 updateop++;
870                 break;
871
872         case LDAP_REQ_MODIFY:
873                 updateop++;
874                 opflag = SLAP_RESTRICT_OP_MODIFY;
875                 break;
876         case LDAP_REQ_RENAME:
877                 updateop++;
878                 opflag = SLAP_RESTRICT_OP_RENAME;
879                 break;
880         case LDAP_REQ_SEARCH:
881                 opflag = SLAP_RESTRICT_OP_SEARCH;
882                 break;
883         case LDAP_REQ_UNBIND:
884                 session++;
885                 opflag = 0;
886                 break;
887         default:
888                 rs->sr_text = "restrict operations internal error";
889                 rs->sr_err = LDAP_OTHER;
890                 return rs->sr_err;
891         }
892
893         if ( !starttls ) {
894                 /* these checks don't apply to StartTLS */
895
896                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
897                 if( op->o_transport_ssf < ssf->sss_transport ) {
898                         rs->sr_text = "transport confidentiality required";
899                         return rs->sr_err;
900                 }
901
902                 if( op->o_tls_ssf < ssf->sss_tls ) {
903                         rs->sr_text = "TLS confidentiality required";
904                         return rs->sr_err;
905                 }
906
907
908                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
909                         /* simple bind specific check */
910                         if( op->o_ssf < ssf->sss_simple_bind ) {
911                                 rs->sr_text = "confidentiality required";
912                                 return rs->sr_err;
913                         }
914                 }
915
916                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
917                         /* these checks don't apply to SASL bind */
918
919                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
920                                 rs->sr_text = "SASL confidentiality required";
921                                 return rs->sr_err;
922                         }
923
924                         if( op->o_ssf < ssf->sss_ssf ) {
925                                 rs->sr_text = "confidentiality required";
926                                 return rs->sr_err;
927                         }
928                 }
929
930                 if( updateop ) {
931                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
932                                 rs->sr_text = "transport update confidentiality required";
933                                 return rs->sr_err;
934                         }
935
936                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
937                                 rs->sr_text = "TLS update confidentiality required";
938                                 return rs->sr_err;
939                         }
940
941                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
942                                 rs->sr_text = "SASL update confidentiality required";
943                                 return rs->sr_err;
944                         }
945
946                         if( op->o_ssf < ssf->sss_update_ssf ) {
947                                 rs->sr_text = "update confidentiality required";
948                                 return rs->sr_err;
949                         }
950
951                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
952                                 op->o_ndn.bv_len == 0 )
953                         {
954                                 rs->sr_text = "modifications require authentication";
955                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
956                                 return rs->sr_err;
957                         }
958
959 #ifdef SLAP_X_LISTENER_MOD
960                         if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
961                                 /* no "w" mode means readonly */
962                                 rs->sr_text = "modifications not allowed on this listener";
963                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
964                                 return rs->sr_err;
965                         }
966 #endif /* SLAP_X_LISTENER_MOD */
967                 }
968         }
969
970         if ( !session ) {
971                 /* these checks don't apply to Bind, StartTLS, or Unbind */
972
973                 if( requires & SLAP_REQUIRE_STRONG ) {
974                         /* should check mechanism */
975                         if( ( op->o_transport_ssf < ssf->sss_transport
976                                 && op->o_authmech.bv_len == 0 ) || op->o_dn.bv_len == 0 )
977                         {
978                                 rs->sr_text = "strong authentication required";
979                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
980                                 return rs->sr_err;
981                         }
982                 }
983
984                 if( requires & SLAP_REQUIRE_SASL ) {
985                         if( op->o_authmech.bv_len == 0 || op->o_dn.bv_len == 0 ) {
986                                 rs->sr_text = "SASL authentication required";
987                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
988                                 return rs->sr_err;
989                         }
990                 }
991                         
992                 if( requires & SLAP_REQUIRE_AUTHC ) {
993                         if( op->o_dn.bv_len == 0 ) {
994                                 rs->sr_text = "authentication required";
995                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
996                                 return rs->sr_err;
997                         }
998                 }
999
1000                 if( requires & SLAP_REQUIRE_BIND ) {
1001                         int version;
1002                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1003                         version = op->o_conn->c_protocol;
1004                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1005
1006                         if( !version ) {
1007                                 /* no bind has occurred */
1008                                 rs->sr_text = "BIND required";
1009                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1010                                 return rs->sr_err;
1011                         }
1012                 }
1013
1014                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1015                         if( op->o_protocol < LDAP_VERSION3 ) {
1016                                 /* no bind has occurred */
1017                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1018                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1019                                 return rs->sr_err;
1020                         }
1021                 }
1022
1023 #ifdef SLAP_X_LISTENER_MOD
1024                 if ( !starttls && op->o_dn.bv_len == 0 ) {
1025                         if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & S_IXOTH ) ) {
1026                                 /* no "x" mode means bind required */
1027                                 rs->sr_text = "bind required on this listener";
1028                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1029                                 return rs->sr_err;
1030                         }
1031                 }
1032
1033                 if ( !starttls && !updateop ) {
1034                         if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH ) ) ) {
1035                                 /* no "r" mode means no read */
1036                                 rs->sr_text = "read not allowed on this listener";
1037                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1038                                 return rs->sr_err;
1039                         }
1040                 }
1041 #endif /* SLAP_X_LISTENER_MOD */
1042
1043         }
1044
1045         if( restrictops & opflag ) {
1046                 if( restrictops == SLAP_RESTRICT_OP_READS ) {
1047                         rs->sr_text = "read operations restricted";
1048                 } else {
1049                         rs->sr_text = "operation restricted";
1050                 }
1051                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1052                 return rs->sr_err;
1053         }
1054
1055         rs->sr_err = LDAP_SUCCESS;
1056         return rs->sr_err;
1057 }
1058
1059 int backend_check_referrals( Operation *op, SlapReply *rs )
1060 {
1061         rs->sr_err = LDAP_SUCCESS;
1062
1063         if( op->o_bd->be_chk_referrals ) {
1064                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1065
1066                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1067                         send_ldap_result( op, rs );
1068                 }
1069         }
1070
1071         return rs->sr_err;
1072 }
1073
1074 int
1075 be_entry_get_rw(
1076         Operation *op,
1077         struct berval *ndn,
1078         ObjectClass *oc,
1079         AttributeDescription *at,
1080         int rw,
1081         Entry **e )
1082 {
1083         Backend *be = op->o_bd;
1084         int rc;
1085
1086         *e = NULL;
1087
1088         op->o_bd = select_backend( ndn, 0, 0 );
1089
1090         if (op->o_bd == NULL) {
1091                 rc = LDAP_NO_SUCH_OBJECT;
1092         } else if ( op->o_bd->be_fetch ) {
1093                 rc = ( op->o_bd->be_fetch )( op, ndn,
1094                         oc, at, rw, e );
1095         } else {
1096                 rc = LDAP_UNWILLING_TO_PERFORM;
1097         }
1098         op->o_bd = be;
1099         return rc;
1100 }
1101
1102 int 
1103 backend_group(
1104         Operation *op,
1105         Entry   *target,
1106         struct berval *gr_ndn,
1107         struct berval *op_ndn,
1108         ObjectClass *group_oc,
1109         AttributeDescription *group_at
1110 )
1111 {
1112         Entry *e;
1113         Attribute *a;
1114         int rc;
1115         GroupAssertion *g;
1116
1117         if ( op->o_abandon ) return SLAPD_ABANDON;
1118
1119         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1120
1121         for (g = op->o_conn->c_groups; g; g=g->ga_next) {
1122                 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1123                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1124                         continue;
1125                 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1126                         break;
1127         }
1128
1129         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1130
1131         if (g) {
1132                 return g->ga_res;
1133         }
1134
1135         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1136                 e = target;
1137         } else {
1138                 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1139         }
1140         if ( e ) {
1141                 a = attr_find( e->e_attrs, group_at );
1142                 if ( a ) {
1143                         rc = value_find_ex( group_at,
1144                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1145                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1146                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1147                 } else {
1148                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1149                 }
1150                 if (e != target ) {
1151                         be_entry_release_r( op, e );
1152                 }
1153         } else {
1154                 rc = LDAP_NO_SUCH_OBJECT;
1155         }
1156
1157         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1158                 g = ch_malloc(sizeof(GroupAssertion) + gr_ndn->bv_len);
1159                 g->ga_be = op->o_bd;
1160                 g->ga_oc = group_oc;
1161                 g->ga_at = group_at;
1162                 g->ga_res = rc;
1163                 g->ga_len = gr_ndn->bv_len;
1164                 strcpy(g->ga_ndn, gr_ndn->bv_val);
1165                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1166                 g->ga_next = op->o_conn->c_groups;
1167                 op->o_conn->c_groups = g;
1168                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1169         }
1170
1171         return rc;
1172 }
1173
1174 int 
1175 backend_attribute(
1176         Operation *op,
1177         Entry   *target,
1178         struct berval   *edn,
1179         AttributeDescription *entry_at,
1180         BerVarray *vals
1181 )
1182 {
1183         Entry *e;
1184         Attribute *a;
1185         int i, j, rc = LDAP_SUCCESS;
1186         AccessControlState acl_state = ACL_STATE_INIT;
1187
1188         if ( target && dn_match( &target->e_nname, edn ) ) {
1189                 e = target;
1190         } else {
1191                 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1192                 if ( rc != LDAP_SUCCESS ) return rc;
1193         } 
1194
1195         if ( e ) {
1196                 a = attr_find( e->e_attrs, entry_at );
1197                 if ( a ) {
1198                         BerVarray v;
1199
1200                         if ( op->o_conn && access_allowed( op,
1201                                 e, entry_at, NULL, ACL_AUTH,
1202                                 &acl_state ) == 0 ) {
1203                                 rc = LDAP_INSUFFICIENT_ACCESS;
1204                                 goto freeit;
1205                         }
1206
1207                         for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1208                         
1209                         v = op->o_tmpalloc( sizeof(struct berval) * (i+1), op->o_tmpmemctx );
1210                         for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1211                                 if ( op->o_conn && access_allowed( op,
1212                                         e, entry_at,
1213                                         &a->a_nvals[i],
1214                                         ACL_AUTH, &acl_state ) == 0 ) {
1215                                         continue;
1216                                 }
1217                                 ber_dupbv_x( &v[j],
1218                                         &a->a_nvals[i], op->o_tmpmemctx );
1219                                 if (v[j].bv_val ) j++;
1220                         }
1221                         if (j == 0) {
1222                                 op->o_tmpfree( v, op->o_tmpmemctx );
1223                                 *vals = NULL;
1224                                 rc = LDAP_INSUFFICIENT_ACCESS;
1225                         } else {
1226                                 v[j].bv_val = NULL;
1227                                 v[j].bv_len = 0;
1228                                 *vals = v;
1229                                 rc = LDAP_SUCCESS;
1230                         }
1231                 }
1232 freeit:         if (e != target ) {
1233                         be_entry_release_r( op, e );
1234                 }
1235         }
1236
1237         return rc;
1238 }
1239
1240 Attribute *backend_operational(
1241         Operation *op,
1242         SlapReply *rs,
1243         int opattrs     )
1244 {
1245         Attribute *a = NULL, **ap = &a;
1246
1247         /*
1248          * If operational attributes (allegedly) are required, 
1249          * and the backend supports specific operational attributes, 
1250          * add them to the attribute list
1251          */
1252         if ( opattrs || ( op->ors_attrs &&
1253                 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1254                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1255                 ap = &(*ap)->a_next;
1256         }
1257
1258         if ( ( opattrs || op->ors_attrs ) && op->o_bd && op->o_bd->be_operational != NULL ) {
1259                 ( void )op->o_bd->be_operational( op, rs, opattrs, ap );
1260         }
1261
1262         return a;
1263 }
1264