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