1 /* monitor.c - monitor ldap backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2006 The OpenLDAP Foundation.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/string.h>
28 #include <ac/unistd.h>
29 #include <ac/stdlib.h>
33 #include "back-ldap.h"
37 static ObjectClass *oc_olmLDAPDatabase;
39 static AttributeDescription *ad_olmDbURIList;
42 * NOTE: there's some confusion in monitor OID arc;
43 * by now, let's consider:
45 * Subsystems monitor attributes 1.3.6.1.4.1.4203.666.1.55.0
46 * Databases monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1
47 * LDAP database monitor attributes 1.3.6.1.4.1.4203.666.1.55.0.1.2
49 * Subsystems monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0
50 * Databases monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1
51 * LDAP database monitor objectclasses 1.3.6.1.4.1.4203.666.3.16.0.1.2
58 { "olmLDAPAttributes", "olmDatabaseAttributes:2" },
59 { "olmLDAPObjectClasses", "olmDatabaseObjectClasses:2" },
66 AttributeDescription **ad;
68 { "( olmLDAPAttributes:1 "
69 "NAME ( 'olmDbURIList' ) "
70 "DESC 'List of URIs a proxy is serving; can be modified run-time' "
81 /* augments an existing object, so it must be AUXILIARY
82 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
83 { "( olmLDAPObjectClasses:1 "
84 "NAME ( 'olmLDAPDatabase' ) "
89 &oc_olmLDAPDatabase },
95 ldap_back_monitor_info_destroy( ldapinfo_t * li )
97 if ( !BER_BVISNULL( &li->li_monitor_info.lmi_rdn ) )
98 ch_free( li->li_monitor_info.lmi_rdn.bv_val );
99 if ( !BER_BVISNULL( &li->li_monitor_info.lmi_nrdn ) )
100 ch_free( li->li_monitor_info.lmi_nrdn.bv_val );
101 if ( !BER_BVISNULL( &li->li_monitor_info.lmi_filter ) )
102 ch_free( li->li_monitor_info.lmi_filter.bv_val );
103 if ( !BER_BVISNULL( &li->li_monitor_info.lmi_more_filter ) )
104 ch_free( li->li_monitor_info.lmi_more_filter.bv_val );
106 memset( &li->li_monitor_info, 0, sizeof( li->li_monitor_info ) );
112 ldap_back_monitor_update(
118 ldapinfo_t *li = (ldapinfo_t *)priv;
122 /* update olmDbURIList */
123 a = attr_find( e->e_attrs, ad_olmDbURIList );
127 assert( a->a_vals != NULL );
128 assert( !BER_BVISNULL( &a->a_vals[ 0 ] ) );
129 assert( BER_BVISNULL( &a->a_vals[ 1 ] ) );
131 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
133 ber_str2bv( li->li_uri, 0, 0, &bv );
134 if ( !bvmatch( &a->a_vals[ 0 ], &bv ) ) {
135 ber_bvreplace( &a->a_vals[ 0 ], &bv );
138 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
141 return SLAP_CB_CONTINUE;
145 ldap_back_monitor_modify(
151 ldapinfo_t *li = (ldapinfo_t *) priv;
153 Attribute *save_attrs = NULL;
155 *ml_olmDbURIList = NULL;
156 struct berval ul = BER_BVNULL;
159 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
160 if ( ml->sml_desc == ad_olmDbURIList ) {
161 if ( ml_olmDbURIList != NULL ) {
162 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
163 rs->sr_text = "conflicting modifications";
167 if ( ml->sml_op != LDAP_MOD_REPLACE ) {
168 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
169 rs->sr_text = "modification not allowed";
173 ml_olmDbURIList = ml;
180 return SLAP_CB_CONTINUE;
183 save_attrs = attrs_dup( e->e_attrs );
185 if ( ml_olmDbURIList != NULL ) {
187 LDAPURLDesc *ludlist = NULL;
190 ml = ml_olmDbURIList;
191 assert( ml->sml_nvalues != NULL );
193 if ( BER_BVISNULL( &ml->sml_nvalues[ 0 ] ) ) {
194 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
195 rs->sr_text = "no value provided";
199 if ( !BER_BVISNULL( &ml->sml_nvalues[ 1 ] ) ) {
200 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
201 rs->sr_text = "multiple values provided";
205 rc = ldap_url_parselist_ext( &ludlist,
206 ml->sml_nvalues[ 0 ].bv_val, NULL,
207 LDAP_PVT_URL_PARSE_NOEMPTY_HOST
208 | LDAP_PVT_URL_PARSE_DEF_PORT );
209 if ( rc != LDAP_URL_SUCCESS ) {
210 rs->sr_err = LDAP_INVALID_SYNTAX;
211 rs->sr_text = "unable to parse URI list";
215 ul.bv_val = ldap_url_list2urls( ludlist );
216 ldap_free_urllist( ludlist );
217 if ( ul.bv_val == NULL ) {
218 rs->sr_err = LDAP_OTHER;
221 ul.bv_len = strlen( ul.bv_val );
223 a = attr_find( e->e_attrs, ad_olmDbURIList );
225 if ( a->a_nvals == a->a_vals ) {
226 a->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
229 ber_bvreplace( &a->a_vals[ 0 ], &ul );
230 ber_bvreplace( &a->a_nvals[ 0 ], &ul );
233 attr_merge_normalize_one( e, ad_olmDbURIList, &ul, NULL );
238 if ( !BER_BVISNULL( &ul ) ) {
239 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
241 ch_free( li->li_uri );
243 li->li_uri = ul.bv_val;
244 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
250 if ( !BER_BVISNULL( &ul ) ) {
251 ldap_memfree( ul.bv_val );
254 if ( rs->sr_err == LDAP_SUCCESS ) {
255 attrs_free( save_attrs );
256 return SLAP_CB_CONTINUE;
259 attrs_free( e->e_attrs );
260 e->e_attrs = save_attrs;
266 ldap_back_monitor_free(
270 ldapinfo_t *li = (ldapinfo_t *)priv;
272 if ( !slapd_shutdown && !BER_BVISNULL( &li->li_monitor_info.lmi_rdn ) ) {
273 ldap_back_monitor_info_destroy( li );
276 return SLAP_CB_CONTINUE;
280 ldap_back_monitor_conn_create(
287 monitor_entry_t *mp_parent;
288 ldap_monitor_info_t *lmi;
291 assert( e_parent->e_private != NULL );
293 mp_parent = e_parent->e_private;
294 lmi = (ldap_monitor_info_t *)mp_parent->mp_info;
297 /* do the hard work! */
303 * call from within ldap_back_initialize()
306 ldap_back_monitor_initialize( void )
313 static int ldap_back_monitor_initialized = 0;
315 /* register schema here; if compiled as dynamic object,
316 * must be loaded __after__ back_monitor.la */
318 if ( ldap_back_monitor_initialized++ ) {
322 if ( backend_info( "monitor" ) == NULL ) {
326 argv[ 0 ] = "back-ldap monitor";
330 for ( i = 0; s_oid[ i ].name; i++ ) {
332 argv[ 1 ] = s_oid[ i ].name;
333 argv[ 2 ] = s_oid[ i ].oid;
335 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
336 Debug( LDAP_DEBUG_ANY,
337 "ldap_back_monitor_initialize: unable to add "
338 "objectIdentifier \"%s=%s\"\n",
339 s_oid[ i ].name, s_oid[ i ].oid, 0 );
344 for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
345 code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
346 if ( code != LDAP_SUCCESS ) {
347 Debug( LDAP_DEBUG_ANY,
348 "ldap_back_monitor_initialize: register_at failed\n",
353 for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
354 code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
355 if ( code != LDAP_SUCCESS ) {
356 Debug( LDAP_DEBUG_ANY,
357 "ldap_back_monitor_initialize: register_oc failed\n",
366 * call from within ldap_back_db_init()
369 ldap_back_monitor_db_init( BackendDB *be )
373 rc = ldap_back_monitor_initialize();
374 if ( rc != LDAP_SUCCESS ) {
378 #if 0 /* uncomment to turn monitoring on by default */
379 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
386 * call from within ldap_back_db_open()
389 ldap_back_monitor_db_open( BackendDB *be )
391 ldapinfo_t *li = (ldapinfo_t *) be->be_private;
392 char buf[ BACKMONITOR_BUFSIZE ];
394 monitor_callback_t *cb = NULL;
395 struct berval suffix, *filter, *base;
398 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
399 struct berval timestamp;
402 monitor_extra_t *mbe;
404 if ( !SLAP_DBMONITORING( be ) ) {
408 /* check if monitor is configured and usable */
409 mi = backend_info( "monitor" );
410 if ( !mi || !mi->bi_extra ) {
411 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
416 /* don't bother if monitor is not configured */
417 if ( !mbe->is_configured() ) {
418 static int warning = 0;
420 if ( warning++ == 0 ) {
421 Debug( LDAP_DEBUG_ANY, "ldap_back_monitor_db_open: "
422 "monitoring disabled; "
423 "configure monitor database to enable\n",
430 /* set up the fake subsystem that is used to create
431 * the volatile connection entries */
432 li->li_monitor_info.lmi_mss.mss_name = "back-ldap";
433 li->li_monitor_info.lmi_mss.mss_flags = MONITOR_F_VOLATILE_CH;
434 li->li_monitor_info.lmi_mss.mss_create = ldap_back_monitor_conn_create;
436 li->li_monitor_info.lmi_li = li;
437 li->li_monitor_info.lmi_scope = LDAP_SCOPE_SUBORDINATE;
438 base = &li->li_monitor_info.lmi_base;
439 BER_BVSTR( base, "cn=databases,cn=monitor" );
440 filter = &li->li_monitor_info.lmi_filter;
441 BER_BVZERO( filter );
443 suffix.bv_len = ldap_bv2escaped_filter_value_len( &be->be_nsuffix[ 0 ] );
444 if ( suffix.bv_len == be->be_nsuffix[ 0 ].bv_len ) {
445 suffix = be->be_nsuffix[ 0 ];
448 ldap_bv2escaped_filter_value( &be->be_nsuffix[ 0 ], &suffix );
451 filter->bv_len = STRLENOF( "(&" )
452 + li->li_monitor_info.lmi_more_filter.bv_len
453 + STRLENOF( "(monitoredInfo=" )
454 + strlen( be->bd_info->bi_type )
455 + STRLENOF( ")(!(monitorOverlay=" )
456 + strlen( be->bd_info->bi_type )
457 + STRLENOF( "))(namingContexts:distinguishedNameMatch:=" )
458 + suffix.bv_len + STRLENOF( "))" );
459 ptr = filter->bv_val = ch_malloc( filter->bv_len + 1 );
460 ptr = lutil_strcopy( ptr, "(&" );
461 ptr = lutil_strncopy( ptr, li->li_monitor_info.lmi_more_filter.bv_val,
462 li->li_monitor_info.lmi_more_filter.bv_len );
463 ptr = lutil_strcopy( ptr, "(monitoredInfo=" );
464 ptr = lutil_strcopy( ptr, be->bd_info->bi_type );
465 ptr = lutil_strcopy( ptr, ")(!(monitorOverlay=" );
466 ptr = lutil_strcopy( ptr, be->bd_info->bi_type );
467 ptr = lutil_strcopy( ptr, "))(namingContexts:distinguishedNameMatch:=" );
468 ptr = lutil_strncopy( ptr, suffix.bv_val, suffix.bv_len );
469 ptr = lutil_strcopy( ptr, "))" );
471 assert( filter->bv_len == ptr - filter->bv_val );
473 if ( suffix.bv_val != be->be_nsuffix[ 0 ].bv_val ) {
474 ch_free( suffix.bv_val );
477 now = slap_get_time();
478 timestamp.bv_val = timebuf;
479 timestamp.bv_len = sizeof( timebuf );
480 slap_timestamp( &now, ×tamp );
482 /* caller (e.g. an overlay based on back-ldap) may want to use
483 * a different RDN... */
484 if ( BER_BVISNULL( &li->li_monitor_info.lmi_rdn ) ) {
485 ber_str2bv( "cn=Connections", 0, 1, &li->li_monitor_info.lmi_rdn );
488 ptr = ber_bvchr( &li->li_monitor_info.lmi_rdn, '=' );
489 assert( ptr != NULL );
493 snprintf( buf, sizeof( buf ),
495 "objectClass: monitorContainer\n"
498 "createTimestamp: %s\n"
499 "modifiersName: %s\n"
500 "modifyTimestamp: %s\n",
501 li->li_monitor_info.lmi_rdn.bv_val,
503 li->li_monitor_info.lmi_rdn.bv_val,
505 BER_BVISNULL( &be->be_rootdn ) ? SLAPD_ANONYMOUS : be->be_rootdn.bv_val,
507 BER_BVISNULL( &be->be_rootdn ) ? SLAPD_ANONYMOUS : be->be_rootdn.bv_val,
509 e = str2entry( buf );
517 /* add labeledURI and special, modifiable URI value */
518 if ( li->li_uri != NULL ) {
520 LDAPURLDesc *ludlist = NULL;
523 rc = ldap_url_parselist_ext( &ludlist,
525 LDAP_PVT_URL_PARSE_NOEMPTY_HOST
526 | LDAP_PVT_URL_PARSE_DEF_PORT );
527 if ( rc != LDAP_URL_SUCCESS ) {
528 Debug( LDAP_DEBUG_ANY,
529 "ldap_back_monitor_db_open: "
530 "unable to parse URI list (ignored)\n",
533 for ( ; ludlist != NULL; ) {
534 LDAPURLDesc *next = ludlist->lud_next;
536 bv.bv_val = ldap_url_desc2str( ludlist );
537 assert( bv.bv_val != NULL );
538 ldap_free_urldesc( ludlist );
539 bv.bv_len = strlen( bv.bv_val );
540 attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
542 ch_free( bv.bv_val );
548 ber_str2bv( li->li_uri, 0, 0, &bv );
549 attr_merge_normalize_one( e, ad_olmDbURIList,
553 ber_dupbv( &li->li_monitor_info.lmi_nrdn, &e->e_nname );
555 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
556 cb->mc_update = ldap_back_monitor_update;
557 cb->mc_modify = ldap_back_monitor_modify;
558 cb->mc_free = ldap_back_monitor_free;
559 cb->mc_private = (void *)li;
561 rc = mbe->register_entry_parent( e, cb,
562 (monitor_subsys_t *)&li->li_monitor_info,
563 MONITOR_F_VOLATILE_CH,
564 base, LDAP_SCOPE_SUBORDINATE, filter );
578 if ( !BER_BVISNULL( filter ) ) {
579 ch_free( filter->bv_val );
580 BER_BVZERO( filter );
584 /* store for cleanup */
585 li->li_monitor_info.lmi_cb = (void *)cb;
595 * call from within ldap_back_db_close()
598 ldap_back_monitor_db_close( BackendDB *be )
600 ldapinfo_t *li = (ldapinfo_t *) be->be_private;
602 if ( li && !BER_BVISNULL( &li->li_monitor_info.lmi_filter ) ) {
604 monitor_extra_t *mbe;
606 /* check if monitor is configured and usable */
607 mi = backend_info( "monitor" );
608 if ( mi && mi->bi_extra ) {
611 mbe->unregister_entry_parent(
612 &li->li_monitor_info.lmi_nrdn,
613 (monitor_callback_t *)li->li_monitor_info.lmi_cb,
614 &li->li_monitor_info.lmi_base,
615 li->li_monitor_info.lmi_scope,
616 &li->li_monitor_info.lmi_filter );
624 * call from within ldap_back_db_destroy()
627 ldap_back_monitor_db_destroy( BackendDB *be )
629 ldapinfo_t *li = (ldapinfo_t *) be->be_private;
632 (void)ldap_back_monitor_info_destroy( li );