1 /* entry.c - monitor backend entry handling routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2021 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21 
22 #include "portable.h"
23 
24 #include <slap.h>
25 #include "back-monitor.h"
26 
27 int
monitor_entry_update(Operation * op,SlapReply * rs,Entry * e)28 monitor_entry_update(
29 	Operation		*op,
30 	SlapReply		*rs,
31 	Entry 			*e
32 )
33 {
34 	monitor_info_t	*mi = ( monitor_info_t * )op->o_bd->be_private;
35 	monitor_entry_t *mp;
36 
37 	int		rc = SLAP_CB_CONTINUE;
38 
39 	assert( mi != NULL );
40 	assert( e != NULL );
41 	assert( e->e_private != NULL );
42 
43 	mp = ( monitor_entry_t * )e->e_private;
44 
45 	if ( mp->mp_cb ) {
46 		struct monitor_callback_t	*mc;
47 
48 		for ( mc = mp->mp_cb; mc; mc = mc->mc_next ) {
49 			if ( mc->mc_update ) {
50 				rc = mc->mc_update( op, rs, e, mc->mc_private );
51 				if ( rc != SLAP_CB_CONTINUE ) {
52 					break;
53 				}
54 			}
55 		}
56 	}
57 
58 	if ( rc == SLAP_CB_CONTINUE && mp->mp_info && mp->mp_info->mss_update ) {
59 		rc = mp->mp_info->mss_update( op, rs, e );
60 	}
61 
62 	if ( rc == SLAP_CB_CONTINUE ) {
63 		rc = LDAP_SUCCESS;
64 	}
65 
66 	return rc;
67 }
68 
69 int
monitor_entry_create(Operation * op,SlapReply * rs,struct berval * ndn,Entry * e_parent,Entry ** ep)70 monitor_entry_create(
71 	Operation		*op,
72 	SlapReply		*rs,
73 	struct berval		*ndn,
74 	Entry			*e_parent,
75 	Entry			**ep )
76 {
77 	monitor_info_t	*mi = ( monitor_info_t * )op->o_bd->be_private;
78 	monitor_entry_t *mp;
79 
80 	int		rc = SLAP_CB_CONTINUE;
81 
82 	assert( mi != NULL );
83 	assert( e_parent != NULL );
84 	assert( e_parent->e_private != NULL );
85 	assert( ep != NULL );
86 
87 	mp = ( monitor_entry_t * )e_parent->e_private;
88 
89 	if ( mp->mp_info && mp->mp_info->mss_create ) {
90 		rc = mp->mp_info->mss_create( op, rs, ndn, e_parent, ep );
91 	}
92 
93 	if ( rc == SLAP_CB_CONTINUE ) {
94 		rc = LDAP_SUCCESS;
95 	}
96 
97 	return rc;
98 }
99 
100 int
monitor_entry_modify(Operation * op,SlapReply * rs,Entry * e)101 monitor_entry_modify(
102 	Operation		*op,
103 	SlapReply		*rs,
104 	Entry 			*e
105 )
106 {
107 	monitor_info_t	*mi = ( monitor_info_t * )op->o_bd->be_private;
108 	monitor_entry_t *mp;
109 
110 	int		rc = SLAP_CB_CONTINUE;
111 
112 	assert( mi != NULL );
113 	assert( e != NULL );
114 	assert( e->e_private != NULL );
115 
116 	mp = ( monitor_entry_t * )e->e_private;
117 
118 	if ( mp->mp_cb ) {
119 		struct monitor_callback_t	*mc;
120 
121 		for ( mc = mp->mp_cb; mc; mc = mc->mc_next ) {
122 			if ( mc->mc_modify ) {
123 				rc = mc->mc_modify( op, rs, e, mc->mc_private );
124 				if ( rc != SLAP_CB_CONTINUE ) {
125 					break;
126 				}
127 			}
128 		}
129 	}
130 
131 	if ( rc == SLAP_CB_CONTINUE && mp->mp_info && mp->mp_info->mss_modify ) {
132 		rc = mp->mp_info->mss_modify( op, rs, e );
133 	}
134 
135 	if ( rc == SLAP_CB_CONTINUE ) {
136 		rc = LDAP_SUCCESS;
137 	}
138 
139 	return rc;
140 }
141 
142 int
monitor_entry_test_flags(monitor_entry_t * mp,int cond)143 monitor_entry_test_flags(
144 	monitor_entry_t		*mp,
145 	int			cond
146 )
147 {
148 	assert( mp != NULL );
149 
150 	return( ( mp->mp_flags & cond ) || ( mp->mp_info->mss_flags & cond ) );
151 }
152 
153 monitor_entry_t *
monitor_back_entrypriv_create(void)154 monitor_back_entrypriv_create( void )
155 {
156 	monitor_entry_t	*mp;
157 
158 	mp = ( monitor_entry_t * )ch_calloc( sizeof( monitor_entry_t ), 1 );
159 
160 	mp->mp_next = NULL;
161 	mp->mp_children = NULL;
162 	mp->mp_info = NULL;
163 	mp->mp_flags = MONITOR_F_NONE;
164 	mp->mp_cb = NULL;
165 
166 	ldap_pvt_thread_mutex_init( &mp->mp_mutex );
167 
168 	return mp;
169 }
170 
171 Entry *
monitor_entry_stub(struct berval * pdn,struct berval * pndn,struct berval * rdn,ObjectClass * oc,struct berval * create,struct berval * modify)172 monitor_entry_stub(
173 	struct berval *pdn,
174 	struct berval *pndn,
175 	struct berval *rdn,
176 	ObjectClass *oc,
177 	struct berval *create,
178 	struct berval *modify
179 )
180 {
181 	monitor_info_t *mi;
182 	AttributeDescription *nad = NULL;
183 	Entry *e;
184 	struct berval nat;
185 	char *ptr;
186 	const char *text;
187 	int rc;
188 
189 	mi = ( monitor_info_t * )be_monitor->be_private;
190 
191 	nat = *rdn;
192 	ptr = strchr( nat.bv_val, '=' );
193 	nat.bv_len = ptr - nat.bv_val;
194 	rc = slap_bv2ad( &nat, &nad, &text );
195 	if ( rc )
196 		return NULL;
197 
198 	e = entry_alloc();
199 	if ( e ) {
200 		struct berval nrdn;
201 
202 		rdnNormalize( 0, NULL, NULL, rdn, &nrdn, NULL );
203 		build_new_dn( &e->e_name, pdn, rdn, NULL );
204 		build_new_dn( &e->e_nname, pndn, &nrdn, NULL );
205 		ber_memfree( nrdn.bv_val );
206 		nat.bv_val = ptr + 1;
207 		nat.bv_len = rdn->bv_len - ( nat.bv_val - rdn->bv_val );
208 		attr_merge_normalize_one( e, slap_schema.si_ad_objectClass,
209 			&oc->soc_cname, NULL );
210 		attr_merge_normalize_one( e, slap_schema.si_ad_structuralObjectClass,
211 			&oc->soc_cname, NULL );
212 		attr_merge_normalize_one( e, nad, &nat, NULL );
213 		attr_merge_one( e, slap_schema.si_ad_creatorsName, &mi->mi_creatorsName,
214 			&mi->mi_ncreatorsName );
215 		attr_merge_one( e, slap_schema.si_ad_modifiersName, &mi->mi_creatorsName,
216 			&mi->mi_ncreatorsName );
217 		attr_merge_normalize_one( e, slap_schema.si_ad_createTimestamp,
218 			create ? create : &mi->mi_startTime, NULL );
219 		attr_merge_normalize_one( e, slap_schema.si_ad_modifyTimestamp,
220 			modify ? modify : &mi->mi_startTime, NULL );
221 	}
222 	return e;
223 }
224 
225 Entry *
monitor_entry_get_unlocked(struct berval * ndn)226 monitor_entry_get_unlocked(
227 	struct berval *ndn
228 )
229 {
230 	monitor_info_t *mi = ( monitor_info_t * )be_monitor->be_private;
231 	Entry *ret = NULL;
232 
233 	if ( !monitor_cache_get( mi, ndn, &ret ))
234 		monitor_cache_release( mi, ret );
235 	return ret;
236 }
237