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