1/*	$NetBSD: backend.c,v 1.3 2021/08/14 16:15:00 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-2021 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.3 2021/08/14 16:15:00 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 */
39int
40monitor_subsys_backend_init(
41	BackendDB		*be,
42	monitor_subsys_t	*ms
43)
44{
45	monitor_info_t		*mi;
46	Entry			*e_backend, **ep;
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	mp = ( monitor_entry_t * )e_backend->e_private;
73	mp->mp_children = NULL;
74	ep = &mp->mp_children;
75
76	i = -1;
77	LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
78		char 		buf[ BACKMONITOR_BUFSIZE ];
79		BackendDB	*be;
80		struct berval 	bv;
81		int		j;
82		Entry		*e;
83
84		i++;
85
86		bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Backend %d", i );
87		bv.bv_val = buf;
88
89		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
90			mi->mi_oc_monitoredObject, NULL, NULL );
91
92		if ( e == NULL ) {
93			Debug( LDAP_DEBUG_ANY,
94				"monitor_subsys_backend_init: "
95				"unable to create entry \"cn=Backend %d,%s\"\n",
96				i, ms->mss_ndn.bv_val );
97			return( -1 );
98		}
99
100		ber_str2bv( bi->bi_type, 0, 0, &bv );
101		attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
102				&bv, NULL );
103		attr_merge_normalize_one( e_backend, mi->mi_ad_monitoredInfo,
104				&bv, NULL );
105
106		attr_merge_normalize_one( e, mi->mi_ad_monitorRuntimeConfig,
107			bi->bi_cf_ocs == NULL ? (struct berval *)&slap_false_bv :
108				(struct berval *)&slap_true_bv, NULL );
109
110		if ( bi->bi_controls ) {
111			int j;
112
113			for ( j = 0; bi->bi_controls[ j ]; j++ ) {
114				ber_str2bv( bi->bi_controls[ j ], 0, 0, &bv );
115				attr_merge_one( e, slap_schema.si_ad_supportedControl,
116						&bv, &bv );
117			}
118		}
119
120		j = -1;
121		LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
122			char		buf[ SLAP_LDAPDN_MAXLEN ];
123			struct berval	dn;
124
125			j++;
126
127			if ( be->bd_info != bi ) {
128				continue;
129			}
130
131			snprintf( buf, sizeof( buf ), "cn=Database %d,%s",
132					j, ms_database->mss_dn.bv_val );
133
134			ber_str2bv( buf, 0, 0, &dn );
135			attr_merge_normalize_one( e, slap_schema.si_ad_seeAlso,
136					&dn, NULL );
137		}
138
139		mp = monitor_entrypriv_create();
140		if ( mp == NULL ) {
141			return -1;
142		}
143		e->e_private = ( void * )mp;
144		mp->mp_info = ms;
145		mp->mp_flags = ms->mss_flags | MONITOR_F_SUB;
146
147		if ( monitor_cache_add( mi, e ) ) {
148			Debug( LDAP_DEBUG_ANY,
149				"monitor_subsys_backend_init: "
150				"unable to add entry \"cn=Backend %d,%s\"\n",
151				i,
152					ms->mss_ndn.bv_val );
153			return( -1 );
154		}
155
156		*ep = e;
157		ep = &mp->mp_next;
158	}
159
160	monitor_cache_release( mi, e_backend );
161
162	return( 0 );
163}
164
165