]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
Add validation.
[openldap] / servers / slapd / back-monitor / database.c
1 /* database.c - deals with database subsystem */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include "slap.h"
39 #include "back-monitor.h"
40
41 int
42 monitor_subsys_database_init(
43         BackendDB       *be
44 )
45 {
46         struct monitorinfo      *mi;
47         Entry                   *e, *e_database, *e_tmp;
48         int                     i;
49         struct monitorentrypriv *mp;
50         AttributeDescription    *ad_nc = slap_schema.si_ad_namingContexts;
51         AttributeDescription    *ad_seeAlso = NULL;
52         const char              *text = NULL;
53
54         assert( be != NULL );
55         assert( monitor_ad_desc != NULL );
56
57         mi = ( struct monitorinfo * )be->be_private;
58
59         if ( monitor_cache_get( mi, 
60                                 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn, 
61                                 &e_database ) ) {
62 #ifdef NEW_LOGGING
63                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
64                         "monitor_subsys_database_init: "
65                         "unable to get entry '%s'\n",
66                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val ));
67 #else
68                 Debug( LDAP_DEBUG_ANY,
69                         "monitor_subsys_database_init: "
70                         "unable to get entry '%s'\n%s%s",
71                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 
72                         "", "" );
73 #endif
74                 return( -1 );
75         }
76
77         if ( slap_str2ad( "seeAlso", &ad_seeAlso, &text ) != LDAP_SUCCESS ) {
78                 return( -1 );
79         }
80
81         e_tmp = NULL;
82         for ( i = nBackendDB; i--; ) {
83                 char buf[1024];
84                 int j;
85
86                 be = &backendDB[i];
87
88                 snprintf( buf, sizeof( buf ),
89                                 "dn: cn=Database %d,%s\n"
90                                 SLAPD_MONITOR_OBJECTCLASSES
91                                 "cn: Database %d\n"
92                                 "description: %s",
93                                 i,
94                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
95                                 i,
96                                 be->bd_info->bi_type );
97                 
98                 e = str2entry( buf );
99                 if ( e == NULL ) {
100 #ifdef NEW_LOGGING
101                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
102                                 "monitor_subsys_database_init: "
103                                 "unable to create entry 'cn=Database %d,%s'\n",
104                                 i, 
105                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val ));
106 #else
107                         Debug( LDAP_DEBUG_ANY,
108                                 "monitor_subsys_database_init: "
109                                 "unable to create entry 'cn=Database %d,%s'\n%s",
110                                 i, 
111                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
112                                 "" );
113 #endif
114                         return( -1 );
115                 }
116                 
117                 for ( j = 0; be->be_suffix[j]; j++ ) {
118                         struct berval           bv[ 2 ];
119
120                         bv[ 0 ] = *be->be_suffix[ j ];
121                         bv[ 1 ].bv_val = NULL;
122
123                         attr_merge( e, ad_nc, bv );
124                         attr_merge( e_database, ad_nc, bv );
125                 }
126
127                 for ( j = nBackendInfo; j--; ) {
128                         if ( &backendInfo[ j ] == be->bd_info ) {
129                                 struct berval           bv[ 2 ];
130
131                                 /* we check the pointer; the test on the
132                                  * string should be more reliable */
133                                 assert( strcasecmp( backendInfo[ j ].bi_type,
134                                         be->bd_info->bi_type ) == 0 );
135
136                                 snprintf( buf, sizeof( buf ), 
137                                         "cn=Backend %d,%s", 
138                                         j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
139                                 bv[ 0 ].bv_val = buf;
140                                 bv[ 0 ].bv_len = strlen( buf );
141                                 bv[ 1 ].bv_val = NULL;
142                                 attr_merge( e, ad_seeAlso, bv );
143                                 break;
144                         }
145                 }
146                 /* we must find it! */
147                 assert( j >= 0 );
148
149                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
150                 e->e_private = ( void * )mp;
151                 mp->mp_next = e_tmp;
152                 mp->mp_children = NULL;
153                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
154                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
155                         | MONITOR_F_SUB;
156
157                 if ( monitor_cache_add( mi, e ) ) {
158 #ifdef NEW_LOGGING
159                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
160                                 "monitor_subsys_database_init: "
161                                 "unable to add entry 'cn=Database %d,%s'\n",
162                                 i, 
163                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val ));
164 #else
165                         Debug( LDAP_DEBUG_ANY,
166                                 "monitor_subsys_database_init: "
167                                 "unable to add entry 'cn=Database %d,%s'\n",
168                                 i, 
169                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
170                                 0 );
171 #endif
172                         return( -1 );
173                 }
174
175                 e_tmp = e;
176         }
177         
178         mp = ( struct monitorentrypriv * )e_database->e_private;
179         mp->mp_children = e_tmp;
180
181         monitor_cache_release( mi, e_database );
182
183         return( 0 );
184 }
185