xref: /netbsd-src/external/bsd/openldap/dist/contrib/slapd-modules/samba4/vernum.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: vernum.c,v 1.1.1.2 2014/05/28 09:58:28 tron Exp $	*/
2 
3 /* vernum.c - RDN value overlay */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2014 The OpenLDAP Foundation.
8  * Portions Copyright 2008 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 the 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
21  * for inclusion in OpenLDAP Software.
22  */
23 
24 #include "portable.h"
25 
26 #ifdef SLAPD_OVER_VERNUM
27 
28 #include <stdio.h>
29 
30 #include "ac/string.h"
31 #include "ac/socket.h"
32 
33 #include "slap.h"
34 #include "config.h"
35 
36 #include "lutil.h"
37 
38 /*
39  * Maintain an attribute (e.g. msDS-KeyVersionNumber) that consists
40  * in a counter of modifications of another attribute (e.g. unicodePwd).
41  */
42 
43 typedef struct vernum_t {
44 	AttributeDescription	*vn_attr;
45 	AttributeDescription	*vn_vernum;
46 } vernum_t;
47 
48 static AttributeDescription	*ad_msDS_KeyVersionNumber;
49 
50 static struct berval		val_init = BER_BVC( "0" );
51 static slap_overinst 		vernum;
52 
53 static int
54 vernum_op_add( Operation *op, SlapReply *rs )
55 {
56 	slap_overinst	*on = (slap_overinst *) op->o_bd->bd_info;
57 	vernum_t	*vn = (vernum_t *)on->on_bi.bi_private;
58 
59 	Attribute *a, **ap;
60 	int rc;
61 
62 	/* NOTE: should we accept an entry still in mods format? */
63 	assert( op->ora_e != NULL );
64 
65 	if ( BER_BVISEMPTY( &op->ora_e->e_nname ) ) {
66 		return SLAP_CB_CONTINUE;
67 	}
68 
69 	a = attr_find( op->ora_e->e_attrs, vn->vn_attr );
70 	if ( a == NULL ) {
71 		return SLAP_CB_CONTINUE;
72 	}
73 
74 	if ( attr_find( op->ora_e->e_attrs, vn->vn_vernum ) != NULL ) {
75 		/* already present - leave it alone */
76 		return SLAP_CB_CONTINUE;
77 	}
78 
79 	a = attr_alloc( vn->vn_vernum );
80 
81 	value_add_one( &a->a_vals, &val_init );
82 	a->a_nvals = a->a_vals;
83 	a->a_numvals = 1;
84 
85 	for ( ap = &op->ora_e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
86 		/* goto tail */ ;
87 
88 	*ap = a;
89 
90 	return SLAP_CB_CONTINUE;
91 }
92 
93 static int
94 vernum_op_modify( Operation *op, SlapReply *rs )
95 {
96 	slap_overinst	*on = (slap_overinst *) op->o_bd->bd_info;
97 	vernum_t	*vn = (vernum_t *)on->on_bi.bi_private;
98 
99 	Modifications *ml, **mlp;
100 	struct berval val = BER_BVC( "1" );
101 	int rc;
102 	unsigned got = 0;
103 
104 	for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
105 		if ( ml->sml_desc == vn->vn_vernum ) {
106 			/* already present - leave it alone
107 			 * (or should we increment it anyway?) */
108 			return SLAP_CB_CONTINUE;
109 		}
110 
111 		if ( ml->sml_desc == vn->vn_attr ) {
112 			got = 1;
113 		}
114 	}
115 
116 	if ( !got ) {
117 		return SLAP_CB_CONTINUE;
118 	}
119 
120 	for ( mlp = &op->orm_modlist; *mlp != NULL; mlp = &(*mlp)->sml_next )
121 		/* goto tail */ ;
122 
123 	/* ITS#6561 */
124 #ifdef SLAP_MOD_ADD_IF_NOT_PRESENT
125 	/* the initial value is only added if the vernum attr is not present */
126 	ml = SLAP_CALLOC( sizeof( Modifications ), 1 );
127 	ml->sml_values = SLAP_CALLOC( sizeof( struct berval ) , 2 );
128 	value_add_one( &ml->sml_values, &val_init );
129 	ml->sml_nvalues = NULL;
130 	ml->sml_numvals = 1;
131 	ml->sml_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
132 	ml->sml_flags = SLAP_MOD_INTERNAL;
133 	ml->sml_desc = vn->vn_vernum;
134 	ml->sml_type = vn->vn_vernum->ad_cname;
135 
136 	*mlp = ml;
137 	mlp = &ml->sml_next;
138 #endif /* SLAP_MOD_ADD_IF_NOT_PRESENT */
139 
140 	/* this increments by 1 the vernum attr */
141 	ml = SLAP_CALLOC( sizeof( Modifications ), 1 );
142 	ml->sml_values = SLAP_CALLOC( sizeof( struct berval ) , 2 );
143 	value_add_one( &ml->sml_values, &val );
144 	ml->sml_nvalues = NULL;
145 	ml->sml_numvals = 1;
146 	ml->sml_op = LDAP_MOD_INCREMENT;
147 	ml->sml_flags = SLAP_MOD_INTERNAL;
148 	ml->sml_desc = vn->vn_vernum;
149 	ml->sml_type = vn->vn_vernum->ad_cname;
150 
151 	*mlp = ml;
152 
153 	return SLAP_CB_CONTINUE;
154 }
155 
156 static int
157 vernum_db_init(
158 	BackendDB	*be,
159 	ConfigReply	*cr)
160 {
161 	slap_overinst	*on = (slap_overinst *) be->bd_info;
162 	vernum_t	*vn = NULL;
163 
164 	if ( SLAP_ISGLOBALOVERLAY( be ) ) {
165 		Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
166 			"vernum_db_init: vernum cannot be used as global overlay.\n" );
167 		return 1;
168 	}
169 
170 	if ( be->be_nsuffix == NULL ) {
171 		Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
172 			"vernum_db_init: database must have suffix\n" );
173 		return 1;
174 	}
175 
176 	if ( BER_BVISNULL( &be->be_rootndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
177 		Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
178 			"vernum_db_init: missing rootdn for database DN=\"%s\", YMMV\n",
179 			be->be_suffix[ 0 ].bv_val );
180 	}
181 
182 	vn = (vernum_t *)ch_calloc( 1, sizeof( vernum_t ) );
183 
184 	on->on_bi.bi_private = (void *)vn;
185 
186 	return 0;
187 }
188 
189 typedef struct vernum_mod_t {
190 	struct berval ndn;
191 	struct vernum_mod_t *next;
192 } vernum_mod_t;
193 
194 typedef struct {
195 	BackendDB *bd;
196 	vernum_mod_t *mods;
197 } vernum_repair_cb_t;
198 
199 static int
200 vernum_repair_cb( Operation *op, SlapReply *rs )
201 {
202 	int rc;
203 	vernum_repair_cb_t *rcb = op->o_callback->sc_private;
204 	vernum_mod_t *mod;
205 	ber_len_t len;
206 	BackendDB *save_bd = op->o_bd;
207 
208 	switch ( rs->sr_type ) {
209 	case REP_SEARCH:
210 		break;
211 
212 	case REP_SEARCHREF:
213 	case REP_RESULT:
214 		return rs->sr_err;
215 
216 	default:
217 		assert( 0 );
218 	}
219 
220 	assert( rs->sr_entry != NULL );
221 
222 	len = sizeof( vernum_mod_t ) + rs->sr_entry->e_nname.bv_len + 1;
223 	mod = op->o_tmpalloc( len, op->o_tmpmemctx );
224 	mod->ndn.bv_len = rs->sr_entry->e_nname.bv_len;
225 	mod->ndn.bv_val = (char *)&mod[1];
226 	lutil_strncopy( mod->ndn.bv_val, rs->sr_entry->e_nname.bv_val, rs->sr_entry->e_nname.bv_len );
227 
228 	mod->next = rcb->mods;
229 	rcb->mods = mod;
230 
231 	Debug( LDAP_DEBUG_TRACE, "%s: vernum_repair_cb: scheduling entry DN=\"%s\" for repair\n",
232 		op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
233 
234 	return 0;
235 }
236 
237 static int
238 vernum_repair( BackendDB *be )
239 {
240 	slap_overinst *on = (slap_overinst *)be->bd_info;
241 	vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
242 	void *ctx = ldap_pvt_thread_pool_context();
243 	Connection conn = { 0 };
244 	OperationBuffer opbuf;
245 	Operation *op;
246 	BackendDB db;
247 	slap_callback sc = { 0 };
248 	vernum_repair_cb_t rcb = { 0 };
249 	SlapReply rs = { REP_RESULT };
250 	vernum_mod_t *rmod;
251 	int nrepaired = 0;
252 
253 	connection_fake_init2( &conn, &opbuf, ctx, 0 );
254 	op = &opbuf.ob_op;
255 
256 	op->o_tag = LDAP_REQ_SEARCH;
257 	memset( &op->oq_search, 0, sizeof( op->oq_search ) );
258 
259 	assert( !BER_BVISNULL( &be->be_nsuffix[ 0 ] ) );
260 
261 	op->o_bd = select_backend( &be->be_nsuffix[ 0 ], 0 );
262 	assert( op->o_bd != NULL );
263 	assert( op->o_bd->be_nsuffix != NULL );
264 
265 	op->o_req_dn = op->o_bd->be_suffix[ 0 ];
266 	op->o_req_ndn = op->o_bd->be_nsuffix[ 0 ];
267 
268 	op->o_dn = op->o_bd->be_rootdn;
269 	op->o_ndn = op->o_bd->be_rootndn;
270 
271 	op->ors_scope = LDAP_SCOPE_SUBTREE;
272 	op->ors_tlimit = SLAP_NO_LIMIT;
273 	op->ors_slimit = SLAP_NO_LIMIT;
274 	op->ors_attrs = slap_anlist_no_attrs;
275 
276 	op->ors_filterstr.bv_len = STRLENOF( "(&(=*)(!(=*)))" )
277 		+ vn->vn_attr->ad_cname.bv_len
278 		+ vn->vn_vernum->ad_cname.bv_len;
279 	op->ors_filterstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
280 	snprintf( op->ors_filterstr.bv_val, op->ors_filterstr.bv_len + 1,
281 		"(&(%s=*)(!(%s=*)))",
282 		vn->vn_attr->ad_cname.bv_val,
283 		vn->vn_vernum->ad_cname.bv_val );
284 
285 	op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
286 	if ( op->ors_filter == NULL ) {
287 		rs.sr_err = LDAP_OTHER;
288 		goto done_search;
289 	}
290 
291 	op->o_callback = &sc;
292 	sc.sc_response = vernum_repair_cb;
293 	sc.sc_private = &rcb;
294 	rcb.bd = &db;
295 	db = *be;
296 	db.bd_info = (BackendInfo *)on;
297 
298 	(void)op->o_bd->bd_info->bi_op_search( op, &rs );
299 
300 	op->o_tag = LDAP_REQ_MODIFY;
301 	sc.sc_response = slap_null_cb;
302 	sc.sc_private = NULL;
303 	memset( &op->oq_modify, 0, sizeof( req_modify_s ) );
304 
305 	for ( rmod = rcb.mods; rmod != NULL; ) {
306 		vernum_mod_t *rnext;
307 		Modifications mod;
308 		struct berval vals[2] = { BER_BVNULL };
309 		SlapReply rs2 = { REP_RESULT };
310 
311 		mod.sml_flags = SLAP_MOD_INTERNAL;
312 		mod.sml_op = LDAP_MOD_REPLACE;
313 		mod.sml_desc = vn->vn_vernum;
314 		mod.sml_type = vn->vn_vernum->ad_cname;
315 		mod.sml_values = vals;
316 		mod.sml_values[0] = val_init;
317 		mod.sml_nvalues = NULL;
318 		mod.sml_numvals = 1;
319 		mod.sml_next = NULL;
320 
321 		op->o_req_dn = rmod->ndn;
322 		op->o_req_ndn = rmod->ndn;
323 
324 		op->orm_modlist = &mod;
325 
326 		op->o_bd->be_modify( op, &rs2 );
327 
328 		slap_mods_free( op->orm_modlist->sml_next, 1 );
329 		if ( rs2.sr_err == LDAP_SUCCESS ) {
330 			Debug( LDAP_DEBUG_TRACE, "%s: vernum_repair: entry DN=\"%s\" repaired\n",
331 				op->o_log_prefix, rmod->ndn.bv_val, 0 );
332 			nrepaired++;
333 
334 		} else {
335 			Debug( LDAP_DEBUG_ANY, "%s: vernum_repair: entry DN=\"%s\" repair failed (%d)\n",
336 				op->o_log_prefix, rmod->ndn.bv_val, rs2.sr_err );
337 		}
338 
339 		rnext = rmod->next;
340 		op->o_tmpfree( rmod, op->o_tmpmemctx );
341 		rmod = rnext;
342 	}
343 
344 done_search:;
345 	op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
346 	filter_free_x( op, op->ors_filter, 1 );
347 
348 	Log1( LDAP_DEBUG_STATS, LDAP_LEVEL_INFO,
349 		"vernum: repaired=%d\n", nrepaired );
350 
351 	return 0;
352 }
353 
354 static int
355 vernum_db_open(
356 	BackendDB	*be,
357 	ConfigReply	*cr )
358 {
359 	slap_overinst *on = (slap_overinst *)be->bd_info;
360 	vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
361 
362 	if ( SLAP_SINGLE_SHADOW( be ) ) {
363 		Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
364 			"vernum incompatible with shadow database \"%s\".\n",
365 			be->be_suffix[ 0 ].bv_val );
366 		return 1;
367 	}
368 
369 	/* default: unicodePwd & msDS-KeyVersionNumber */
370 	if ( vn->vn_attr == NULL ) {
371 		const char *text = NULL;
372 		int rc;
373 
374 		rc = slap_str2ad( "unicodePwd", &vn->vn_attr, &text );
375 		if ( rc != LDAP_SUCCESS ) {
376 			Debug( LDAP_DEBUG_ANY, "vernum: unable to find attribute 'unicodePwd' (%d: %s)\n",
377 				rc, text, 0 );
378 			return 1;
379 		}
380 
381 		vn->vn_vernum = ad_msDS_KeyVersionNumber;
382 	}
383 
384 	return vernum_repair( be );
385 }
386 
387 static int
388 vernum_db_destroy(
389 	BackendDB	*be,
390 	ConfigReply	*cr )
391 {
392 	slap_overinst *on = (slap_overinst *)be->bd_info;
393 	vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
394 
395 	if ( vn ) {
396 		ch_free( vn );
397 		on->on_bi.bi_private = NULL;
398 	}
399 
400 	return 0;
401 }
402 
403 static struct {
404 	char	*desc;
405 	AttributeDescription **adp;
406 } as[] = {
407 	{ "( 1.2.840.113556.1.4.1782 "
408 		"NAME 'msDS-KeyVersionNumber' "
409 		"DESC 'in the original specification the syntax is 2.5.5.9' "
410 		"SYNTAX '1.3.6.1.4.1.1466.115.121.1.27' "
411 		"EQUALITY integerMatch "
412 		"SINGLE-VALUE "
413 		"USAGE dSAOperation "
414 		"NO-USER-MODIFICATION "
415 		")",
416 		&ad_msDS_KeyVersionNumber },
417 	{ NULL }
418 };
419 
420 int
421 vernum_initialize(void)
422 {
423 	int code, i;
424 
425 	for ( i = 0; as[ i ].desc != NULL; i++ ) {
426 		code = register_at( as[ i ].desc, as[ i ].adp, 0 );
427 		if ( code ) {
428 			Debug( LDAP_DEBUG_ANY,
429 				"vernum_initialize: register_at #%d failed\n",
430 				i, 0, 0 );
431 			return code;
432 		}
433 
434 		/* Allow Manager to set these as needed */
435 		if ( is_at_no_user_mod( (*as[ i ].adp)->ad_type ) ) {
436 			(*as[ i ].adp)->ad_type->sat_flags |=
437 				SLAP_AT_MANAGEABLE;
438 		}
439 	}
440 
441 	vernum.on_bi.bi_type = "vernum";
442 
443 	vernum.on_bi.bi_op_add = vernum_op_add;
444 	vernum.on_bi.bi_op_modify = vernum_op_modify;
445 
446 	vernum.on_bi.bi_db_init = vernum_db_init;
447 	vernum.on_bi.bi_db_open = vernum_db_open;
448 	vernum.on_bi.bi_db_destroy = vernum_db_destroy;
449 
450 	return overlay_register( &vernum );
451 }
452 
453 #if SLAPD_OVER_VERNUM == SLAPD_MOD_DYNAMIC
454 int
455 init_module( int argc, char *argv[] )
456 {
457 	return vernum_initialize();
458 }
459 #endif /* SLAPD_OVER_VERNUM == SLAPD_MOD_DYNAMIC */
460 
461 #endif /* SLAPD_OVER_VERNUM */
462