]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
b754376c334685c424bf284831f7f5b2525cae9e
[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 2001-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26
27 #include "slap.h"
28 #include "back-monitor.h"
29
30 #if defined(LDAP_SLAPI)
31 #include "slapi.h"
32 static int monitor_back_add_plugin( Backend *be, Entry *e );
33 #endif /* defined(LDAP_SLAPI) */
34
35 #if defined(SLAPD_LDAP) 
36 #include "../back-ldap/back-ldap.h"
37 #endif /* defined(SLAPD_LDAP) */
38
39 int
40 monitor_subsys_database_init(
41         BackendDB       *be
42 )
43 {
44         struct monitorinfo      *mi;
45         Entry                   *e, *e_database, *e_tmp;
46         int                     i;
47         struct monitorentrypriv *mp;
48         struct berval *tf;
49
50         assert( be != NULL );
51
52         mi = ( struct monitorinfo * )be->be_private;
53
54         if ( monitor_cache_get( mi, 
55                                 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn, 
56                                 &e_database ) ) {
57 #ifdef NEW_LOGGING
58                 LDAP_LOG( OPERATION, CRIT,
59                         "monitor_subsys_database_init: "
60                         "unable to get entry '%s'\n",
61                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
62 #else
63                 Debug( LDAP_DEBUG_ANY,
64                         "monitor_subsys_database_init: "
65                         "unable to get entry '%s'\n%s%s",
66                         monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 
67                         "", "" );
68 #endif
69                 return( -1 );
70         }
71         tf = (global_restrictops & SLAP_RESTRICT_OP_WRITES) ?
72                 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv ; 
73         attr_merge_one( e_database, mi->mi_ad_readOnly, tf, tf );
74
75         e_tmp = NULL;
76         for ( i = nBackendDB; i--; ) {
77                 char            buf[ BACKMONITOR_BUFSIZE ];
78                 int             j;
79                 slap_overinfo   *oi = NULL;
80                 BackendInfo     *bi;
81
82                 be = &backendDB[i];
83
84                 bi = be->bd_info;
85
86                 if ( strcmp( be->bd_info->bi_type, "over" ) == 0 ) {
87                         oi = (slap_overinfo *)be->bd_info;
88                         bi = oi->oi_orig;
89                 }
90
91                 /* Subordinates are not exposed as their own naming context */
92                 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
93                         continue;
94                 }
95
96                 snprintf( buf, sizeof( buf ),
97                                 "dn: cn=Database %d,%s\n"
98                                 "objectClass: %s\n"
99                                 "structuralObjectClass: %s\n"
100                                 "cn: Database %d\n"
101                                 "description: This object contains the type of the database.\n"
102                                 "%s: %s\n"
103                                 "createTimestamp: %s\n"
104                                 "modifyTimestamp: %s\n",
105                                 i,
106                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
107                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
108                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
109                                 i,
110                                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
111                                 bi->bi_type,
112                                 mi->mi_startTime.bv_val,
113                                 mi->mi_startTime.bv_val );
114                 
115                 e = str2entry( buf );
116                 if ( e == NULL ) {
117 #ifdef NEW_LOGGING
118                         LDAP_LOG( OPERATION, CRIT,
119                                 "monitor_subsys_database_init: "
120                                 "unable to create entry 'cn=Database %d,%s'\n",
121                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
122 #else
123                         Debug( LDAP_DEBUG_ANY,
124                                 "monitor_subsys_database_init: "
125                                 "unable to create entry 'cn=Database %d,%s'\n%s",
126                                 i, 
127                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
128                                 "" );
129 #endif
130                         return( -1 );
131                 }
132                 
133                 if ( SLAP_MONITOR(be) ) {
134                         attr_merge( e, slap_schema.si_ad_monitorContext,
135                                         be->be_suffix, be->be_nsuffix );
136                         attr_merge( e_database, slap_schema.si_ad_monitorContext,
137                                         be->be_suffix, be->be_nsuffix );
138                 } else {
139                         attr_merge( e, slap_schema.si_ad_namingContexts,
140                                         be->be_suffix, be->be_nsuffix );
141                         attr_merge( e_database, slap_schema.si_ad_namingContexts,
142                                         be->be_suffix, be->be_nsuffix );
143                 }
144                 tf = (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) ?
145                         (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv ; 
146                 attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
147
148                 if ( oi != NULL ) {
149                         slap_overinst *on = oi->oi_list;
150
151                         for ( ; on; on = on->on_next ) {
152                                 struct berval           bv;
153                                 slap_overinst           *on2;
154                                 
155                                 bv.bv_val = on->on_bi.bi_type;
156                                 bv.bv_len = strlen( bv.bv_val );
157                                 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
158                                                 &bv, NULL );
159
160                                 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
161                                         if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
162                                                 break;
163                                         }
164                                 }
165                                 assert( on2 );
166
167                                 snprintf( buf, sizeof( buf ), 
168                                         "cn=Overlay %d,%s", 
169                                         j, monitor_subsys[SLAPD_MONITOR_OVERLAY].mss_dn.bv_val );
170                                 bv.bv_val = buf;
171                                 bv.bv_len = strlen( buf );
172                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
173                                                 &bv, NULL );
174                         }
175                 }
176
177 #if defined(SLAPD_LDAP) 
178                 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
179                         struct ldapinfo         *li = (struct ldapinfo *)be->be_private;
180                         struct berval           bv;
181
182                         bv.bv_val = li->url;
183                         bv.bv_len = strlen( bv.bv_val );
184                         attr_merge_normalize_one( e, mi->mi_ad_labeledURI,
185                                         &bv, NULL );
186                 }
187 #endif /* defined(SLAPD_LDAP) */
188
189                 for ( j = nBackendInfo; j--; ) {
190                         if ( backendInfo[ j ].bi_type == bi->bi_type ) {
191                                 struct berval           bv;
192
193                                 snprintf( buf, sizeof( buf ), 
194                                         "cn=Backend %d,%s", 
195                                         j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
196                                 bv.bv_val = buf;
197                                 bv.bv_len = strlen( buf );
198                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
199                                                 &bv, NULL );
200                                 break;
201                         }
202                 }
203                 /* we must find it! */
204                 assert( j >= 0 );
205
206                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
207                 e->e_private = ( void * )mp;
208                 mp->mp_next = e_tmp;
209                 mp->mp_children = NULL;
210                 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
211                 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
212                         | MONITOR_F_SUB;
213
214                 if ( monitor_cache_add( mi, e ) ) {
215 #ifdef NEW_LOGGING
216                         LDAP_LOG( OPERATION, CRIT,
217                                 "monitor_subsys_database_init: "
218                                 "unable to add entry 'cn=Database %d,%s'\n",
219                                 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
220 #else
221                         Debug( LDAP_DEBUG_ANY,
222                                 "monitor_subsys_database_init: "
223                                 "unable to add entry 'cn=Database %d,%s'\n",
224                                 i, 
225                                 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
226                                 0 );
227 #endif
228                         return( -1 );
229                 }
230
231 #if defined(LDAP_SLAPI)
232                 monitor_back_add_plugin( be, e );
233 #endif /* defined(LDAP_SLAPI) */
234
235                 e_tmp = e;
236         }
237         
238         mp = ( struct monitorentrypriv * )e_database->e_private;
239         mp->mp_children = e_tmp;
240
241         monitor_cache_release( mi, e_database );
242
243         return( 0 );
244 }
245
246 int
247 monitor_subsys_database_modify(
248         Operation       *op,
249         Entry           *e
250 )
251 {
252         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
253         int rc = LDAP_OTHER;
254         Attribute *save_attrs;
255         Modifications *modlist = op->oq_modify.rs_modlist;
256         Modifications *ml;
257         Backend *be;
258         int gotval = 1, i, n, cur;
259         
260         i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
261         if ( i != 1 )
262                 return LDAP_UNWILLING_TO_PERFORM;
263
264         if ( n < 0 || n >= nBackendDB )
265                 return LDAP_NO_SUCH_OBJECT;
266
267         be = &backendDB[n];
268         cur = (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) ? 1 : 0;
269
270         save_attrs = e->e_attrs;
271         e->e_attrs = attrs_dup( e->e_attrs );
272
273         for ( ml=modlist; ml; ml=ml->sml_next ) {
274                 Modification *mod = &ml->sml_mod;
275
276                 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
277                         int val = -1;
278
279                         if ( mod->sm_values ) {
280                                 /* single-valued */
281                                 if ( !BER_BVISNULL(&mod->sm_values[1]) ) {
282                                         rc = LDAP_CONSTRAINT_VIOLATION;
283                                         break;
284                                 }
285                                 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
286                                         val = 1;
287                                 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
288                                         val = 0;
289                                 }
290                         }
291                         switch( mod->sm_op ) {
292                         case LDAP_MOD_DELETE:
293                                 if ( val < 0 || val == cur ) {
294                                         gotval--;
295                                         cur = -1;
296                                 } else {
297                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
298                                 }
299                                 break;
300                         case LDAP_MOD_REPLACE:
301                                 gotval--;
302                                 cur = -1;
303                                 /* FALLTHRU */
304                         case LDAP_MOD_ADD:
305                                 if ( val < 0 ) {
306                                         rc = LDAP_INVALID_SYNTAX;
307                                 } else {
308                                         gotval++;
309                                         cur = val;
310                                 }
311                                 break;
312                         default:
313                                 rc = LDAP_OTHER;
314                                 break;
315                         }
316                         if ( rc ) {
317                                 break;
318                         }
319                 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
320                 /* accept all operational attributes */
321                         attr_delete( &e->e_attrs, mod->sm_desc );
322                         rc = attr_merge( e, mod->sm_desc, mod->sm_values,
323                                 mod->sm_nvalues );
324                         if ( rc ) {
325                                 rc = LDAP_OTHER;
326                                 break;
327                         }
328                 } else {
329                         rc = LDAP_UNWILLING_TO_PERFORM;
330                         break;
331                 }
332         }
333         if ( gotval == 1 && cur >= 0 ) {
334                 struct berval *tf;
335                 tf = cur ? (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
336                 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
337                 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
338                 if ( rc == LDAP_SUCCESS ) {
339                         if ( cur ) {
340                                 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
341                         } else {
342                                 be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
343                         }
344                 } else {
345                         rc = LDAP_OTHER;
346                 }
347         } else {
348                 rc = LDAP_CONSTRAINT_VIOLATION;
349         }
350         if ( rc == LDAP_SUCCESS ) {
351                 attrs_free( save_attrs );
352         } else {
353                 Attribute *tmp = e->e_attrs;
354                 e->e_attrs = save_attrs;
355                 attrs_free( tmp );
356         }
357         return rc;
358 }
359
360 #if defined(LDAP_SLAPI)
361 static int
362 monitor_back_add_plugin( Backend *be, Entry *e_database )
363 {
364         Slapi_PBlock            *pCurrentPB; 
365         int                     i, rc = LDAP_SUCCESS;
366         struct monitorinfo      *mi = ( struct monitorinfo * )be->be_private;
367
368         if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
369                 /*
370                  * LDAP_OTHER is returned if no plugins are installed
371                  */
372                 rc = LDAP_OTHER;
373                 goto done;
374         }
375
376         i = 0;
377         do {
378                 Slapi_PluginDesc        *srchdesc;
379                 char                    buf[ BACKMONITOR_BUFSIZE ];
380                 struct berval           bv;
381
382                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
383                                 &srchdesc );
384                 if ( rc != LDAP_SUCCESS ) {
385                         goto done;
386                 }
387
388                 snprintf( buf, sizeof(buf),
389                                 "plugin %d name: %s; "
390                                 "vendor: %s; "
391                                 "version: %s; "
392                                 "description: %s", 
393                                 i,
394                                 srchdesc->spd_id,
395                                 srchdesc->spd_vendor,
396                                 srchdesc->spd_version,
397                                 srchdesc->spd_description );
398
399                 bv.bv_val = buf;
400                 bv.bv_len = strlen( buf );
401                 attr_merge_normalize_one( e_database,
402                                 mi->mi_ad_monitoredInfo, &bv, NULL );
403
404                 i++;
405
406         } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
407                         && ( pCurrentPB != NULL ) );
408
409 done:
410         return rc;
411 }
412 #endif /* defined(LDAP_SLAPI) */