1 /* vernum.c - RDN value overlay */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2012 The OpenLDAP Foundation.
6 * Portions Copyright 2008 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Pierangelo Masarati
19 * for inclusion in OpenLDAP Software.
24 #ifdef SLAPD_OVER_VERNUM
28 #include "ac/string.h"
29 #include "ac/socket.h"
37 * Maintain an attribute (e.g. msDS-KeyVersionNumber) that consists
38 * in a counter of modifications of another attribute (e.g. unicodePwd).
41 typedef struct vernum_t {
42 AttributeDescription *vn_attr;
43 AttributeDescription *vn_vernum;
46 static AttributeDescription *ad_msDS_KeyVersionNumber;
48 static struct berval val_init = BER_BVC( "0" );
49 static slap_overinst vernum;
52 vernum_op_add( Operation *op, SlapReply *rs )
54 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
55 vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
60 /* NOTE: should we accept an entry still in mods format? */
61 assert( op->ora_e != NULL );
63 if ( BER_BVISEMPTY( &op->ora_e->e_nname ) ) {
64 return SLAP_CB_CONTINUE;
67 a = attr_find( op->ora_e->e_attrs, vn->vn_attr );
69 return SLAP_CB_CONTINUE;
72 if ( attr_find( op->ora_e->e_attrs, vn->vn_vernum ) != NULL ) {
73 /* already present - leave it alone */
74 return SLAP_CB_CONTINUE;
77 a = attr_alloc( vn->vn_vernum );
79 value_add_one( &a->a_vals, &val_init );
80 a->a_nvals = a->a_vals;
83 for ( ap = &op->ora_e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
88 return SLAP_CB_CONTINUE;
92 vernum_op_modify( Operation *op, SlapReply *rs )
94 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
95 vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
97 Modifications *ml, **mlp;
98 struct berval val = BER_BVC( "1" );
102 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
103 if ( ml->sml_desc == vn->vn_vernum ) {
104 /* already present - leave it alone
105 * (or should we increment it anyway?) */
106 return SLAP_CB_CONTINUE;
109 if ( ml->sml_desc == vn->vn_attr ) {
115 return SLAP_CB_CONTINUE;
118 for ( mlp = &op->orm_modlist; *mlp != NULL; mlp = &(*mlp)->sml_next )
122 #ifdef SLAP_MOD_ADD_IF_NOT_PRESENT
123 /* the initial value is only added if the vernum attr is not present */
124 ml = SLAP_CALLOC( sizeof( Modifications ), 1 );
125 ml->sml_values = SLAP_CALLOC( sizeof( struct berval ) , 2 );
126 value_add_one( &ml->sml_values, &val_init );
127 ml->sml_nvalues = NULL;
129 ml->sml_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
130 ml->sml_flags = SLAP_MOD_INTERNAL;
131 ml->sml_desc = vn->vn_vernum;
132 ml->sml_type = vn->vn_vernum->ad_cname;
136 #endif /* SLAP_MOD_ADD_IF_NOT_PRESENT */
138 /* this increments by 1 the vernum attr */
139 ml = SLAP_CALLOC( sizeof( Modifications ), 1 );
140 ml->sml_values = SLAP_CALLOC( sizeof( struct berval ) , 2 );
141 value_add_one( &ml->sml_values, &val );
142 ml->sml_nvalues = NULL;
144 ml->sml_op = LDAP_MOD_INCREMENT;
145 ml->sml_flags = SLAP_MOD_INTERNAL;
146 ml->sml_desc = vn->vn_vernum;
147 ml->sml_type = vn->vn_vernum->ad_cname;
151 return SLAP_CB_CONTINUE;
159 slap_overinst *on = (slap_overinst *) be->bd_info;
162 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
163 Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
164 "vernum_db_init: vernum cannot be used as global overlay.\n" );
168 if ( be->be_nsuffix == NULL ) {
169 Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
170 "vernum_db_init: database must have suffix\n" );
174 if ( BER_BVISNULL( &be->be_rootndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
175 Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
176 "vernum_db_init: missing rootdn for database DN=\"%s\", YMMV\n",
177 be->be_suffix[ 0 ].bv_val );
180 vn = (vernum_t *)ch_calloc( 1, sizeof( vernum_t ) );
182 on->on_bi.bi_private = (void *)vn;
187 typedef struct vernum_mod_t {
189 struct vernum_mod_t *next;
195 } vernum_repair_cb_t;
198 vernum_repair_cb( Operation *op, SlapReply *rs )
201 vernum_repair_cb_t *rcb = op->o_callback->sc_private;
204 BackendDB *save_bd = op->o_bd;
206 switch ( rs->sr_type ) {
218 assert( rs->sr_entry != NULL );
220 len = sizeof( vernum_mod_t ) + rs->sr_entry->e_nname.bv_len + 1;
221 mod = op->o_tmpalloc( len, op->o_tmpmemctx );
222 mod->ndn.bv_len = rs->sr_entry->e_nname.bv_len;
223 mod->ndn.bv_val = (char *)&mod[1];
224 lutil_strncopy( mod->ndn.bv_val, rs->sr_entry->e_nname.bv_val, rs->sr_entry->e_nname.bv_len );
226 mod->next = rcb->mods;
229 Debug( LDAP_DEBUG_TRACE, "%s: vernum_repair_cb: scheduling entry DN=\"%s\" for repair\n",
230 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
236 vernum_repair( BackendDB *be )
238 slap_overinst *on = (slap_overinst *)be->bd_info;
239 vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
240 void *ctx = ldap_pvt_thread_pool_context();
241 Connection conn = { 0 };
242 OperationBuffer opbuf;
245 slap_callback sc = { 0 };
246 vernum_repair_cb_t rcb = { 0 };
247 SlapReply rs = { REP_RESULT };
251 connection_fake_init2( &conn, &opbuf, ctx, 0 );
254 op->o_tag = LDAP_REQ_SEARCH;
255 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
257 assert( !BER_BVISNULL( &be->be_nsuffix[ 0 ] ) );
259 op->o_bd = select_backend( &be->be_nsuffix[ 0 ], 0 );
260 assert( op->o_bd != NULL );
261 assert( op->o_bd->be_nsuffix != NULL );
263 op->o_req_dn = op->o_bd->be_suffix[ 0 ];
264 op->o_req_ndn = op->o_bd->be_nsuffix[ 0 ];
266 op->o_dn = op->o_bd->be_rootdn;
267 op->o_ndn = op->o_bd->be_rootndn;
269 op->ors_scope = LDAP_SCOPE_SUBTREE;
270 op->ors_tlimit = SLAP_NO_LIMIT;
271 op->ors_slimit = SLAP_NO_LIMIT;
272 op->ors_attrs = slap_anlist_no_attrs;
274 op->ors_filterstr.bv_len = STRLENOF( "(&(=*)(!(=*)))" )
275 + vn->vn_attr->ad_cname.bv_len
276 + vn->vn_vernum->ad_cname.bv_len;
277 op->ors_filterstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
278 snprintf( op->ors_filterstr.bv_val, op->ors_filterstr.bv_len + 1,
279 "(&(%s=*)(!(%s=*)))",
280 vn->vn_attr->ad_cname.bv_val,
281 vn->vn_vernum->ad_cname.bv_val );
283 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
284 if ( op->ors_filter == NULL ) {
285 rs.sr_err = LDAP_OTHER;
289 op->o_callback = ≻
290 sc.sc_response = vernum_repair_cb;
291 sc.sc_private = &rcb;
294 db.bd_info = (BackendInfo *)on;
296 (void)op->o_bd->bd_info->bi_op_search( op, &rs );
298 op->o_tag = LDAP_REQ_MODIFY;
299 sc.sc_response = slap_null_cb;
300 sc.sc_private = NULL;
301 memset( &op->oq_modify, 0, sizeof( req_modify_s ) );
303 for ( rmod = rcb.mods; rmod != NULL; ) {
306 struct berval vals[2] = { BER_BVNULL };
307 SlapReply rs2 = { REP_RESULT };
309 mod.sml_flags = SLAP_MOD_INTERNAL;
310 mod.sml_op = LDAP_MOD_REPLACE;
311 mod.sml_desc = vn->vn_vernum;
312 mod.sml_type = vn->vn_vernum->ad_cname;
313 mod.sml_values = vals;
314 mod.sml_values[0] = val_init;
315 mod.sml_nvalues = NULL;
319 op->o_req_dn = rmod->ndn;
320 op->o_req_ndn = rmod->ndn;
322 op->orm_modlist = &mod;
324 op->o_bd->be_modify( op, &rs2 );
326 slap_mods_free( op->orm_modlist->sml_next, 1 );
327 if ( rs2.sr_err == LDAP_SUCCESS ) {
328 Debug( LDAP_DEBUG_TRACE, "%s: vernum_repair: entry DN=\"%s\" repaired\n",
329 op->o_log_prefix, rmod->ndn.bv_val, 0 );
333 Debug( LDAP_DEBUG_ANY, "%s: vernum_repair: entry DN=\"%s\" repair failed (%d)\n",
334 op->o_log_prefix, rmod->ndn.bv_val, rs2.sr_err );
338 op->o_tmpfree( rmod, op->o_tmpmemctx );
343 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
344 filter_free_x( op, op->ors_filter, 1 );
346 Log1( LDAP_DEBUG_STATS, LDAP_LEVEL_INFO,
347 "vernum: repaired=%d\n", nrepaired );
357 slap_overinst *on = (slap_overinst *)be->bd_info;
358 vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
360 if ( SLAP_SINGLE_SHADOW( be ) ) {
361 Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
362 "vernum incompatible with shadow database \"%s\".\n",
363 be->be_suffix[ 0 ].bv_val );
367 /* default: unicodePwd & msDS-KeyVersionNumber */
368 if ( vn->vn_attr == NULL ) {
369 const char *text = NULL;
372 rc = slap_str2ad( "unicodePwd", &vn->vn_attr, &text );
373 if ( rc != LDAP_SUCCESS ) {
374 Debug( LDAP_DEBUG_ANY, "vernum: unable to find attribute 'unicodePwd' (%d: %s)\n",
379 vn->vn_vernum = ad_msDS_KeyVersionNumber;
382 return vernum_repair( be );
390 slap_overinst *on = (slap_overinst *)be->bd_info;
391 vernum_t *vn = (vernum_t *)on->on_bi.bi_private;
395 on->on_bi.bi_private = NULL;
403 AttributeDescription **adp;
405 { "( 1.2.840.113556.1.4.1782 "
406 "NAME 'msDS-KeyVersionNumber' "
407 "DESC 'in the original specification the syntax is 2.5.5.9' "
408 "SYNTAX '1.3.6.1.4.1.1466.115.121.1.27' "
409 "EQUALITY integerMatch "
411 "USAGE dSAOperation "
412 "NO-USER-MODIFICATION "
414 &ad_msDS_KeyVersionNumber },
419 vernum_initialize(void)
423 for ( i = 0; as[ i ].desc != NULL; i++ ) {
424 code = register_at( as[ i ].desc, as[ i ].adp, 0 );
426 Debug( LDAP_DEBUG_ANY,
427 "vernum_initialize: register_at #%d failed\n",
432 /* Allow Manager to set these as needed */
433 if ( is_at_no_user_mod( (*as[ i ].adp)->ad_type ) ) {
434 (*as[ i ].adp)->ad_type->sat_flags |=
439 vernum.on_bi.bi_type = "vernum";
441 vernum.on_bi.bi_op_add = vernum_op_add;
442 vernum.on_bi.bi_op_modify = vernum_op_modify;
444 vernum.on_bi.bi_db_init = vernum_db_init;
445 vernum.on_bi.bi_db_open = vernum_db_open;
446 vernum.on_bi.bi_db_destroy = vernum_db_destroy;
448 return overlay_register( &vernum );
451 #if SLAPD_OVER_VERNUM == SLAPD_MOD_DYNAMIC
453 init_module( int argc, char *argv[] )
455 return vernum_initialize();
457 #endif /* SLAPD_OVER_VERNUM == SLAPD_MOD_DYNAMIC */
459 #endif /* SLAPD_OVER_VERNUM */