summaryrefslogtreecommitdiffstats
path: root/contrib/slapd-modules/usn/usn.c
blob: abd6d13df4db4485e90bf44d1058e1b942ab66b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* usn.c - Maintain Microsoft-style Update Sequence Numbers */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 2007-2022 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Howard Chu for inclusion in
 * OpenLDAP Software.
 */

#include "portable.h"

#ifdef SLAPD_OVER_USN

#include <stdio.h>

#include <ac/string.h>
#include <ac/socket.h>

#include "slap.h"
#include "slap-config.h"

/* This overlay intercepts write operations and adds a Microsoft-style
 * USN to the target entry.
 */

typedef struct usn_info {
	int ui_current;
	ldap_pvt_thread_mutex_t ui_mutex;
} usn_info_t;

static AttributeDescription *ad_usnCreated, *ad_usnChanged;

static struct {
	char *desc;
	AttributeDescription **adp;
} as[] = {
	{ "( 1.2.840.113556.1.2.19 "
	    "NAME 'uSNCreated' "
	    "SYNTAX '1.2.840.113556.1.4.906' "
		"SINGLE-VALUE "
		"NO-USER-MODIFICATION )",
		&ad_usnCreated },
	{ "( 1.2.840.113556.1.2.120 "
		"NAME 'uSNChanged' "
		"SYNTAX '1.2.840.113556.1.4.906' "
		"SINGLE-VALUE "
		"NO-USER-MODIFICATION )",
		&ad_usnChanged },
	{ NULL }
};

static int
usn_func( Operation *op, SlapReply *rs )
{
	slap_overinst		*on = (slap_overinst *) op->o_bd->bd_info;
	usn_info_t		*ui = on->on_bi.bi_private;
	int my_usn;
	char intbuf[64];
	struct berval bv[2];

	ldap_pvt_thread_mutex_lock( &ui->ui_mutex );
	ui->ui_current++;
	my_usn = ui->ui_current;
	ldap_pvt_thread_mutex_unlock( &ui->ui_mutex );

	BER_BVZERO(&bv[1]);
	bv[0].bv_val = intbuf;
	bv[0].bv_len = snprintf( intbuf, sizeof(intbuf), "%d", my_usn );
	switch(op->o_tag) {
	case LDAP_REQ_ADD:
		attr_merge( op->ora_e, ad_usnCreated, bv, NULL );
		attr_merge( op->ora_e, ad_usnChanged, bv, NULL );
		break;
	case LDAP_REQ_DELETE:
		/* Probably need to update root usnLastObjRem */
		break;
	default: {
		/* Modify, ModDN */
		Modifications *ml, *mod = ch_calloc( sizeof( Modifications ), 1 );
		for ( ml = op->orm_modlist; ml && ml->sml_next; ml = ml->sml_next );
		ml->sml_next = mod;
		mod->sml_desc = ad_usnChanged;
		mod->sml_numvals = 1;
		value_add_one( &mod->sml_values, &bv[0] );
		mod->sml_nvalues = NULL;
		mod->sml_op = LDAP_MOD_REPLACE;
		mod->sml_flags = 0;
		mod->sml_next = NULL;
		break;
		}
	}
	return SLAP_CB_CONTINUE;
}

static int
usn_operational(
	Operation *op,
	SlapReply *rs )
{
	slap_overinst		*on = (slap_overinst *)op->o_bd->bd_info;
	usn_info_t		*ui = (usn_info_t *)on->on_bi.bi_private;

	if ( rs->sr_entry &&
		dn_match( &rs->sr_entry->e_nname, op->o_bd->be_nsuffix )) {

		if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
			ad_inlist( ad_usnChanged, rs->sr_attrs )) {
			Attribute *a, **ap = NULL;
			char intbuf[64];
			struct berval bv;
			int my_usn;

			for ( a=rs->sr_entry->e_attrs; a; a=a->a_next ) {
				if ( a->a_desc == ad_usnChanged )
					break;
			}

			if ( !a ) {
				for ( ap = &rs->sr_operational_attrs; *ap;
					ap=&(*ap)->a_next );

					a = attr_alloc( ad_usnChanged );
					*ap = a;
				}

			if ( !ap ) {
				if ( rs_entry2modifiable( op,rs, on )) {
					a = attr_find( rs->sr_entry->e_attrs,
						ad_usnChanged );
				}
				ber_bvarray_free( a->a_vals );
				a->a_vals = NULL;
				a->a_numvals = 0;
			}
			ldap_pvt_thread_mutex_lock( &ui->ui_mutex );
			my_usn = ui->ui_current;
			ldap_pvt_thread_mutex_unlock( &ui->ui_mutex );
			bv.bv_len = snprintf( intbuf, sizeof(intbuf), "%d", my_usn );
			bv.bv_val = intbuf;
			attr_valadd( a, &bv, NULL, 1 );
		}
	}
	return SLAP_CB_CONTINUE;
}

/* Read the old USN from the underlying DB. This code is
 * stolen from the syncprov overlay.
 */
static int
usn_db_open(
	BackendDB *be,
	ConfigReply *cr)
{
	slap_overinst   *on = (slap_overinst *) be->bd_info;
	usn_info_t *ui = (usn_info_t *)on->on_bi.bi_private;

	Connection conn = { 0 };
	OperationBuffer opbuf;
	Operation *op;
	Entry *e = NULL;
	Attribute *a;
	int rc;
	void *thrctx = NULL;

	thrctx = ldap_pvt_thread_pool_context();
	connection_fake_init( &conn, &opbuf, thrctx );
	op = &opbuf.ob_op;
	op->o_bd = be;
	op->o_dn = be->be_rootdn;
	op->o_ndn = be->be_rootndn;

	rc = overlay_entry_get_ov( op, be->be_nsuffix, NULL,
		slap_schema.si_ad_contextCSN, 0, &e, on );

	if ( e ) {
		a = attr_find( e->e_attrs, ad_usnChanged );
		if ( a ) {
			ui->ui_current = atoi( a->a_vals[0].bv_val );
		}
		overlay_entry_release_ov( op, e, 0, on );
	}
	return 0;
}

static int
usn_db_init(
	BackendDB *be,
	ConfigReply *cr
)
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
	usn_info_t	*ui;

	if ( SLAP_ISGLOBALOVERLAY( be ) ) {
		Debug( LDAP_DEBUG_ANY,
			"usn must be instantiated within a database.\n" );
		return 1;
	}

	ui = ch_calloc(1, sizeof(usn_info_t));
	ldap_pvt_thread_mutex_init( &ui->ui_mutex );
	on->on_bi.bi_private = ui;
	return 0;
}

static int
usn_db_close(
	BackendDB *be,
	ConfigReply *cr
)
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
	usn_info_t	*ui = on->on_bi.bi_private;
	Connection conn = {0};
	OperationBuffer opbuf;
	Operation *op;
	SlapReply rs = {REP_RESULT};
	void *thrctx;

	Modifications mod;
	slap_callback cb = {0};
	char intbuf[64];
	struct berval bv[2];

	thrctx = ldap_pvt_thread_pool_context();
	connection_fake_init( &conn, &opbuf, thrctx );
	op = &opbuf.ob_op;
	op->o_bd = be;
	BER_BVZERO( &bv[1] );
	bv[0].bv_len = snprintf( intbuf, sizeof(intbuf), "%d", ui->ui_current );
	bv[0].bv_val = intbuf;
	mod.sml_numvals = 1;
	mod.sml_values = bv;
	mod.sml_nvalues = NULL;
	mod.sml_desc = ad_usnChanged;
	mod.sml_op = LDAP_MOD_REPLACE;
	mod.sml_flags = 0;
	mod.sml_next = NULL;

	cb.sc_response = slap_null_cb;
	op->o_tag = LDAP_REQ_MODIFY;
	op->o_callback = &cb;
	op->orm_modlist = &mod;
	op->orm_no_opattrs = 1;
	op->o_dn = be->be_rootdn;
	op->o_ndn = be->be_rootndn;
	op->o_req_dn = op->o_bd->be_suffix[0];
	op->o_req_ndn = op->o_bd->be_nsuffix[0];
	op->o_bd->bd_info = on->on_info->oi_orig;
	op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
	op->o_no_schema_check = 1;
	op->o_bd->be_modify( op, &rs );
	if ( mod.sml_next != NULL ) {
		slap_mods_free( mod.sml_next, 1 );
	}
	return 0;
}

static int
usn_db_destroy(
	BackendDB *be,
	ConfigReply *cr
)
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
	usn_info_t	*ui = on->on_bi.bi_private;

	ldap_pvt_thread_mutex_destroy( &ui->ui_mutex );
	ch_free( ui );
	on->on_bi.bi_private = NULL;
	return 0;
}

/* This overlay is set up for dynamic loading via moduleload. For static
 * configuration, you'll need to arrange for the slap_overinst to be
 * initialized and registered by some other function inside slapd.
 */

static slap_overinst usn;

int
usn_init( void )
{
	int i, code;

	memset( &usn, 0, sizeof( slap_overinst ) );
	usn.on_bi.bi_type = "usn";
	usn.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
	usn.on_bi.bi_db_init = usn_db_init;
	usn.on_bi.bi_db_destroy = usn_db_destroy;
	usn.on_bi.bi_db_open = usn_db_open;
	usn.on_bi.bi_db_close = usn_db_close;

	usn.on_bi.bi_op_modify = usn_func;
	usn.on_bi.bi_op_modrdn = usn_func;
	usn.on_bi.bi_op_add = usn_func;
	usn.on_bi.bi_op_delete = usn_func;
	usn.on_bi.bi_operational = usn_operational;

	for ( i = 0; as[i].desc; i++ ) {
		code = register_at( as[i].desc, as[i].adp, 0 );
		if ( code ) {
			Debug( LDAP_DEBUG_ANY,
				"usn_init: register_at #%d failed\n", i );
			return code;
		}
	}
	return overlay_register( &usn );
}

#if SLAPD_OVER_USN == SLAPD_MOD_DYNAMIC
int
init_module( int argc, char *argv[] )
{
	return usn_init();
}
#endif /* SLAPD_OVER_USN == SLAPD_MOD_DYNAMIC */

#endif /* defined(SLAPD_OVER_USN) */