]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
Notice and acknowledgements
[openldap] / servers / slapd / back-monitor / database.c
1 /* database.c - deals with database subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Pierangelo Masarati for inclusion
18  * in OpenLDAP Software.
19  */
20 /* This is an altered version */
21 /*
22  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
23  * 
24  * This work has beed deveolped for the OpenLDAP Foundation 
25  * in the hope that it may be useful to the Open Source community, 
26  * but WITHOUT ANY WARRANTY.
27  * 
28  * Permission is granted to anyone to use this software for any purpose
29  * on any computer system, and to alter it and redistribute it, subject
30  * to the following restrictions:
31  * 
32  * 1. The author and SysNet s.n.c. are not responsible for the consequences
33  *    of use of this software, no matter how awful, even if they arise from
34  *    flaws in it.
35  * 
36  * 2. The origin of this software must not be misrepresented, either by
37  *    explicit claim or by omission.  Since few users ever read sources,
38  *    credits should appear in the documentation.
39  * 
40  * 3. Altered versions must be plainly marked as such, and must not be
41  *    misrepresented as being the original software.  Since few users
42  *    ever read sources, credits should appear in the documentation.
43  *    SysNet s.n.c. cannot be responsible for the consequences of the
44  *    alterations.
45  * 
46  * 4. This notice may not be removed or altered.
47  */
48
49 #ifndef _BACK_MONITOR_H_
50 #define _BACK_MONITOR_H_
51
52 #include <ldap_pvt.h>
53 #include <ldap_pvt_thread.h>
54 #include <avl.h>
55 #include <slap.h>
56
57 LDAP_BEGIN_DECL
58
59 /*
60  * The cache maps DNs to Entries.
61 /*
62  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
63  * 
64  * This work has beed deveolped for the OpenLDAP Foundation 
65  * in the hope that it may be useful to the Open Source community, 
66  * but WITHOUT ANY WARRANTY.
67  * 
68  * Permission is granted to anyone to use this software for any purpose
69  * on any computer system, and to alter it and redistribute it, subject
70  * to the following restrictions:
71  * 
72  * 1. The author and SysNet s.n.c. are not responsible for the consequences
73  *    of use of this software, no matter how awful, even if they arise from
74  *    flaws in it.
75  * 
76  * 2. The origin of this software must not be misrepresented, either by
77  *    explicit claim or by omission.  Since few users ever read sources,
78  *    credits should appear in the documentation.
79  * 
80  * 3. Altered versions must be plainly marked as such, and must not be
81  *    misrepresented as being the original software.  Since few users
82  *    ever read sources, credits should appear in the documentation.
83  *    SysNet s.n.c. cannot be responsible for the consequences of the
84  *    alterations.
85  * 
86  * 4. This notice may not be removed or altered.
87  */
88
89 #include "portable.h"
90
91 #include <stdio.h>
92 #include <ac/string.h>
93
94 #include "slap.h"
95 #include "back-monitor.h"
96
97 #if defined(LDAP_SLAPI)
98 #include "slapi.h"
99 static int monitor_back_add_plugin( Backend *be, Entry *e );
100 #endif /* defined(LDAP_SLAPI) */
101
102 int
103 monitor_subsys_database_init(
104         BackendDB       *be
105 )
106 {
107         struct monitorinfo      *mi;
108         Entry                   *e, *e_database, *e_tmp;
109         int                     i;
110         struct monitorentrypriv *mp;
111
112         assert( be != NULL );
113
114         mi = ( struct monitorinfo * )be->be_private;
115
116         if ( monitor_cache_get( mi, 
117                                 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn, 
118                                 &e_database ) ) {
119 #ifdef NEW_LOGGING
120                 LDAP_LOG( OPERATION, CRIT,
121                         "monitor_subsys_database_init: "
122                         "unable to get entry '%s'\n",
123                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
124 #else
125                 Debug( LDAP_DEBUG_ANY,
126                         "monitor_subsys_database_init: "
127                         "unable to get entry '%s'\n%s%s",
128                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 
129                         "", "" );
130 #endif
131                 return( -1 );
132         }
133
134         e_tmp = NULL;
135         for ( i = nBackendDB; i--; ) {
136                 char buf[ BACKMONITOR_BUFSIZE ];
137                 int j;
138
139                 be = &backendDB[i];
140
141                 /* Subordinates are not exposed as their own naming context */
142                 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
143                         continue;
144                 }
145
146                 snprintf( buf, sizeof( buf ),
147                                 "dn: cn=Database %d,%s\n"
148                                 "objectClass: %s\n"
149                                 "structuralObjectClass: %s\n"
150                                 "cn: Database %d\n"
151                                 "description: This object contains the type of the database.\n"
152                                 "%s: %s\n"
153                                 "createTimestamp: %s\n"
154                                 "modifyTimestamp: %s\n",
155                                 i,
156                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
157                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
158                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
159                                 i,
160                                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
161                                 be->bd_info->bi_type,
162                                 mi->mi_startTime.bv_val,
163                                 mi->mi_startTime.bv_val );
164                 
165                 e = str2entry( buf );
166                 if ( e == NULL ) {
167 #ifdef NEW_LOGGING
168                         LDAP_LOG( OPERATION, CRIT,
169                                 "monitor_subsys_database_init: "
170                                 "unable to create entry 'cn=Database %d,%s'\n",
171                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
172 #else
173                         Debug( LDAP_DEBUG_ANY,
174                                 "monitor_subsys_database_init: "
175                                 "unable to create entry 'cn=Database %d,%s'\n%s",
176                                 i, 
177                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
178                                 "" );
179 #endif
180                         return( -1 );
181                 }
182                 
183                 if ( be->be_flags & SLAP_BFLAG_MONITOR ) {
184                         attr_merge( e, slap_schema.si_ad_monitorContext,
185                                         be->be_suffix, be->be_nsuffix );
186                         attr_merge( e_database, slap_schema.si_ad_monitorContext,
187                                         be->be_suffix, be->be_nsuffix );
188                 } else {
189                         attr_merge( e, slap_schema.si_ad_namingContexts,
190                                         be->be_suffix, be->be_nsuffix );
191                         attr_merge( e_database, slap_schema.si_ad_namingContexts,
192                                         be->be_suffix, be->be_nsuffix );
193                 }
194
195                 for ( j = nBackendInfo; j--; ) {
196                         if ( backendInfo[ j ].bi_type == be->bd_info->bi_type ) {
197                                 struct berval           bv;
198
199                                 snprintf( buf, sizeof( buf ), 
200                                         "cn=Backend %d,%s", 
201                                         j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
202                                 bv.bv_val = buf;
203                                 bv.bv_len = strlen( buf );
204                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
205                                                 &bv, NULL );
206                                 break;
207                         }
208                 }
209                 /* we must find it! */
210                 assert( j >= 0 );
211
212                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
213                 e->e_private = ( void * )mp;
214                 mp->mp_next = e_tmp;
215                 mp->mp_children = NULL;
216                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
217                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
218                         | MONITOR_F_SUB;
219
220                 if ( monitor_cache_add( mi, e ) ) {
221 #ifdef NEW_LOGGING
222                         LDAP_LOG( OPERATION, CRIT,
223                                 "monitor_subsys_database_init: "
224                                 "unable to add entry 'cn=Database %d,%s'\n",
225                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
226 #else
227                         Debug( LDAP_DEBUG_ANY,
228                                 "monitor_subsys_database_init: "
229                                 "unable to add entry 'cn=Database %d,%s'\n",
230                                 i, 
231                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
232                                 0 );
233 #endif
234                         return( -1 );
235                 }
236
237 #if defined(LDAP_SLAPI)
238                 monitor_back_add_plugin( be, e );
239 #endif /* defined(LDAP_SLAPI) */
240
241                 e_tmp = e;
242         }
243         
244         mp = ( struct monitorentrypriv * )e_database->e_private;
245         mp->mp_children = e_tmp;
246
247         monitor_cache_release( mi, e_database );
248
249         return( 0 );
250 }
251
252 #if defined(LDAP_SLAPI)
253 static int
254 monitor_back_add_plugin( Backend *be, Entry *e_database )
255 {
256         Slapi_PBlock            *pCurrentPB; 
257         int                     i, rc = LDAP_SUCCESS;
258         struct monitorinfo      *mi = ( struct monitorinfo * )be->be_private;
259
260         if ( slapi_x_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
261                 /*
262                  * LDAP_OTHER is returned if no plugins are installed
263                  */
264                 rc = LDAP_OTHER;
265                 goto done;
266         }
267
268         i = 0;
269         do {
270                 Slapi_PluginDesc        *srchdesc;
271                 char                    buf[ BACKMONITOR_BUFSIZE ];
272                 struct berval           bv;
273
274                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
275                                 &srchdesc );
276                 if ( rc != LDAP_SUCCESS ) {
277                         goto done;
278                 }
279
280                 snprintf( buf, sizeof(buf),
281                                 "plugin %d name: %s; "
282                                 "vendor: %s; "
283                                 "version: %s; "
284                                 "description: %s", 
285                                 i,
286                                 srchdesc->spd_id,
287                                 srchdesc->spd_vendor,
288                                 srchdesc->spd_version,
289                                 srchdesc->spd_description );
290
291                 bv.bv_val = buf;
292                 bv.bv_len = strlen( buf );
293                 attr_merge_normalize_one( e_database,
294                                 mi->mi_ad_monitoredInfo, &bv, NULL );
295
296                 i++;
297
298         } while ( ( slapi_x_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
299                         && ( pCurrentPB != NULL ) );
300
301 done:
302         return rc;
303 }
304 #endif /* defined(LDAP_SLAPI) */