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