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