Home | History | Annotate | Line # | Download | only in back-monitor
      1 /*	$NetBSD: backend.c,v 1.4 2025/09/05 21:16:28 christos Exp $	*/
      2 
      3 /* backend.c - deals with backend subsystem */
      4 /* $OpenLDAP$ */
      5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  *
      7  * Copyright 2001-2024 The OpenLDAP Foundation.
      8  * Portions Copyright 2001-2003 Pierangelo Masarati.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted only as authorized by the OpenLDAP
     13  * Public License.
     14  *
     15  * A copy of this license is available in file LICENSE in the
     16  * top-level directory of the distribution or, alternatively, at
     17  * <http://www.OpenLDAP.org/license.html>.
     18  */
     19 /* ACKNOWLEDGEMENTS:
     20  * This work was initially developed by Pierangelo Masarati for inclusion
     21  * in OpenLDAP Software.
     22  */
     23 
     24 
     25 #include <sys/cdefs.h>
     26 __RCSID("$NetBSD: backend.c,v 1.4 2025/09/05 21:16:28 christos Exp $");
     27 
     28 #include "portable.h"
     29 
     30 #include <stdio.h>
     31 #include <ac/string.h>
     32 
     33 #include "slap.h"
     34 #include "back-monitor.h"
     35 
     36 /*
     37  * initializes backend subentries
     38  */
     39 int
     40 monitor_subsys_backend_init(
     41 	BackendDB		*be,
     42 	monitor_subsys_t	*ms
     43 )
     44 {
     45 	monitor_info_t		*mi;
     46 	Entry			*e_backend;
     47 	int			i;
     48 	monitor_entry_t		*mp;
     49 	monitor_subsys_t	*ms_database;
     50 	BackendInfo			*bi;
     51 
     52 	mi = ( monitor_info_t * )be->be_private;
     53 
     54 	ms_database = monitor_back_get_subsys( SLAPD_MONITOR_DATABASE_NAME );
     55 	if ( ms_database == NULL ) {
     56 		Debug( LDAP_DEBUG_ANY,
     57 			"monitor_subsys_backend_init: "
     58 			"unable to get "
     59 			"\"" SLAPD_MONITOR_DATABASE_NAME "\" "
     60 			"subsystem\n" );
     61 		return -1;
     62 	}
     63 
     64 	if ( monitor_cache_get( mi, &ms->mss_ndn, &e_backend ) ) {
     65 		Debug( LDAP_DEBUG_ANY,
     66 			"monitor_subsys_backend_init: "
     67 			"unable to get entry \"%s\"\n",
     68 			ms->mss_ndn.bv_val );
     69 		return( -1 );
     70 	}
     71 
     72 	i = -1;
     73 	LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
     74 		char 		buf[ BACKMONITOR_BUFSIZE ];
     75 		BackendDB	*be;
     76 		struct berval 	bv;
     77 		int		j;
     78 		Entry		*e;
     79 
     80 		i++;
     81 
     82 		bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Backend %d", i );
     83 		bv.bv_val = buf;
     84 
     85 		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
     86 			mi->mi_oc_monitoredObject, NULL, NULL );
     87 
     88 		if ( e == NULL ) {
     89 			Debug( LDAP_DEBUG_ANY,
     90 				"monitor_subsys_backend_init: "
     91 				"unable to create entry \"cn=Backend %d,%s\"\n",
     92 				i, ms->mss_ndn.bv_val );
     93 			return( -1 );
     94 		}
     95 
     96 		ber_str2bv( bi->bi_type, 0, 0, &bv );
     97 		attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
     98 				&bv, NULL );
     99 		attr_merge_normalize_one( e_backend, mi->mi_ad_monitoredInfo,
    100 				&bv, NULL );
    101 
    102 		attr_merge_normalize_one( e, mi->mi_ad_monitorRuntimeConfig,
    103 			bi->bi_cf_ocs == NULL ? (struct berval *)&slap_false_bv :
    104 				(struct berval *)&slap_true_bv, NULL );
    105 
    106 		if ( bi->bi_controls ) {
    107 			int j;
    108 
    109 			for ( j = 0; bi->bi_controls[ j ]; j++ ) {
    110 				ber_str2bv( bi->bi_controls[ j ], 0, 0, &bv );
    111 				attr_merge_one( e, slap_schema.si_ad_supportedControl,
    112 						&bv, &bv );
    113 			}
    114 		}
    115 
    116 		j = -1;
    117 		LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
    118 			char		buf[ SLAP_LDAPDN_MAXLEN ];
    119 			struct berval	dn;
    120 
    121 			j++;
    122 
    123 			if ( be->bd_info != bi ) {
    124 				continue;
    125 			}
    126 
    127 			snprintf( buf, sizeof( buf ), "cn=Database %d,%s",
    128 					j, ms_database->mss_dn.bv_val );
    129 
    130 			ber_str2bv( buf, 0, 0, &dn );
    131 			attr_merge_normalize_one( e, slap_schema.si_ad_seeAlso,
    132 					&dn, NULL );
    133 		}
    134 
    135 		mp = monitor_entrypriv_create();
    136 		if ( mp == NULL ) {
    137 			return -1;
    138 		}
    139 		e->e_private = ( void * )mp;
    140 		mp->mp_info = ms;
    141 		mp->mp_flags = ms->mss_flags | MONITOR_F_SUB;
    142 
    143 		if ( monitor_cache_add( mi, e, e_backend ) ) {
    144 			Debug( LDAP_DEBUG_ANY,
    145 				"monitor_subsys_backend_init: "
    146 				"unable to add entry \"cn=Backend %d,%s\"\n",
    147 				i,
    148 					ms->mss_ndn.bv_val );
    149 			return( -1 );
    150 		}
    151 	}
    152 
    153 	monitor_cache_release( mi, e_backend );
    154 
    155 	return( 0 );
    156 }
    157 
    158