1 /*	$NetBSD: overlay.c,v 1.1.1.3 2010/12/12 15:23:16 adam 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-2010 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 "portable.h"
25 
26 #include <stdio.h>
27 #include <ac/string.h>
28 
29 #include "slap.h"
30 #include "back-monitor.h"
31 
32 /*
33  * initializes overlay subentries
34  */
35 int
36 monitor_subsys_overlay_init(
37 	BackendDB		*be,
38 	monitor_subsys_t	*ms
39 )
40 {
41 	monitor_info_t		*mi;
42 	Entry			*e_overlay, **ep;
43 	int			i;
44 	monitor_entry_t		*mp;
45 	slap_overinst		*on;
46 	monitor_subsys_t	*ms_database;
47 
48 	mi = ( monitor_info_t * )be->be_private;
49 
50 	ms_database = monitor_back_get_subsys( SLAPD_MONITOR_DATABASE_NAME );
51 	if ( ms_database == NULL ) {
52 		Debug( LDAP_DEBUG_ANY,
53 			"monitor_subsys_backend_init: "
54 			"unable to get "
55 			"\"" SLAPD_MONITOR_DATABASE_NAME "\" "
56 			"subsystem\n",
57 			0, 0, 0 );
58 		return -1;
59 	}
60 
61 	if ( monitor_cache_get( mi, &ms->mss_ndn, &e_overlay ) ) {
62 		Debug( LDAP_DEBUG_ANY,
63 			"monitor_subsys_overlay_init: "
64 			"unable to get entry \"%s\"\n",
65 			ms->mss_ndn.bv_val, 0, 0 );
66 		return( -1 );
67 	}
68 
69 	mp = ( monitor_entry_t * )e_overlay->e_private;
70 	mp->mp_children = NULL;
71 	ep = &mp->mp_children;
72 
73 	for ( on = overlay_next( NULL ), i = 0; on; on = overlay_next( on ), i++ ) {
74 		char 		buf[ BACKMONITOR_BUFSIZE ];
75 		struct berval 	bv;
76 		int		j;
77 		Entry		*e;
78 		BackendDB	*be;
79 
80 		bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Overlay %d", i );
81 		bv.bv_val = buf;
82 		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
83 			mi->mi_oc_monitoredObject, mi, NULL, NULL );
84 		if ( e == NULL ) {
85 			Debug( LDAP_DEBUG_ANY,
86 				"monitor_subsys_overlay_init: "
87 				"unable to create entry \"cn=Overlay %d,%s\"\n",
88 				i, ms->mss_ndn.bv_val, 0 );
89 			return( -1 );
90 		}
91 		ber_str2bv( on->on_bi.bi_type, 0, 0, &bv );
92 		attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
93 		attr_merge_normalize_one( e, mi->mi_ad_monitorRuntimeConfig,
94 			on->on_bi.bi_cf_ocs ? (struct berval *)&slap_true_bv :
95 				(struct berval *)&slap_false_bv, NULL );
96 
97 		attr_merge_normalize_one( e_overlay, mi->mi_ad_monitoredInfo,
98 				&bv, NULL );
99 
100 		j = -1;
101 		LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
102 			char		buf[ SLAP_LDAPDN_MAXLEN ];
103 			struct berval	dn;
104 
105 			j++;
106 			if ( !overlay_is_inst( be, on->on_bi.bi_type ) ) {
107 				continue;
108 			}
109 
110 			snprintf( buf, sizeof( buf ), "cn=Database %d,%s",
111 					j, ms_database->mss_dn.bv_val );
112 
113 			ber_str2bv( buf, 0, 0, &dn );
114 			attr_merge_normalize_one( e, slap_schema.si_ad_seeAlso,
115 					&dn, NULL );
116 		}
117 
118 		mp = monitor_entrypriv_create();
119 		if ( mp == NULL ) {
120 			return -1;
121 		}
122 		e->e_private = ( void * )mp;
123 		mp->mp_info = ms;
124 		mp->mp_flags = ms->mss_flags
125 			| MONITOR_F_SUB;
126 
127 		if ( monitor_cache_add( mi, e ) ) {
128 			Debug( LDAP_DEBUG_ANY,
129 				"monitor_subsys_overlay_init: "
130 				"unable to add entry \"cn=Overlay %d,%s\"\n",
131 				i, ms->mss_ndn.bv_val, 0 );
132 			return( -1 );
133 		}
134 
135 		*ep = e;
136 		ep = &mp->mp_next;
137 	}
138 
139 	monitor_cache_release( mi, e_overlay );
140 
141 	return( 0 );
142 }
143 
144