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