1 /* pguid.c - Parent GUID 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_PGUID
28 #include "ac/string.h"
29 #include "ac/socket.h"
37 * Maintain an attribute (parentUUID) that contains the value
38 * of the entryUUID of the parent entry (used by Samba4)
41 static AttributeDescription *ad_parentUUID;
43 static slap_overinst pguid;
46 pguid_op_add( Operation *op, SlapReply *rs )
48 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
50 struct berval pdn, pndn;
55 /* don't care about suffix entry */
56 if ( dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] ) ) {
57 return SLAP_CB_CONTINUE;
60 dnParent( &op->o_req_dn, &pdn );
61 dnParent( &op->o_req_ndn, &pndn );
63 rc = overlay_entry_get_ov( op, &pndn, NULL, slap_schema.si_ad_entryUUID, 0, &e, on );
64 if ( rc != LDAP_SUCCESS || e == NULL ) {
65 Debug( LDAP_DEBUG_ANY, "%s: pguid_op_add: unable to get parent entry DN=\"%s\" (%d)\n",
66 op->o_log_prefix, pdn.bv_val, rc );
67 return SLAP_CB_CONTINUE;
70 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
72 Debug( LDAP_DEBUG_ANY, "%s: pguid_op_add: unable to find entryUUID of parent entry DN=\"%s\" (%d)\n",
73 op->o_log_prefix, pdn.bv_val, rc );
76 assert( a->a_numvals == 1 );
78 if ( op->ora_e != NULL ) {
79 attr_merge_one( op->ora_e, ad_parentUUID, &a->a_vals[0], a->a_nvals == a->a_vals ? NULL : &a->a_nvals[0] );
85 assert( op->ora_modlist != NULL );
87 for ( ml = op->ora_modlist; ml != NULL; ml = ml->sml_next ) {
88 if ( ml->sml_mod.sm_desc == slap_schema.si_ad_entryUUID ) {
97 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
98 mod->sml_flags = SLAP_MOD_INTERNAL;
99 mod->sml_op = LDAP_MOD_ADD;
100 mod->sml_desc = ad_parentUUID;
101 mod->sml_type = ad_parentUUID->ad_cname;
102 mod->sml_values = ch_malloc( sizeof( struct berval ) * 2 );
103 mod->sml_nvalues = NULL;
104 mod->sml_numvals = 1;
106 ber_dupbv( &mod->sml_values[0], &a->a_vals[0] );
107 BER_BVZERO( &mod->sml_values[1] );
109 mod->sml_next = ml->sml_next;
115 (void)overlay_entry_release_ov( op, e, 0, on );
118 return SLAP_CB_CONTINUE;
122 pguid_op_rename( Operation *op, SlapReply *rs )
124 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
130 if ( op->orr_nnewSup == NULL ) {
131 return SLAP_CB_CONTINUE;
134 rc = overlay_entry_get_ov( op, op->orr_nnewSup, NULL, slap_schema.si_ad_entryUUID, 0, &e, on );
135 if ( rc != LDAP_SUCCESS || e == NULL ) {
136 Debug( LDAP_DEBUG_ANY, "%s: pguid_op_rename: unable to get newSuperior entry DN=\"%s\" (%d)\n",
137 op->o_log_prefix, op->orr_newSup->bv_val, rc );
138 return SLAP_CB_CONTINUE;
141 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
143 Debug( LDAP_DEBUG_ANY, "%s: pguid_op_rename: unable to find entryUUID of newSuperior entry DN=\"%s\" (%d)\n",
144 op->o_log_prefix, op->orr_newSup->bv_val, rc );
149 assert( a->a_numvals == 1 );
151 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
152 mod->sml_flags = SLAP_MOD_INTERNAL;
153 mod->sml_op = LDAP_MOD_REPLACE;
154 mod->sml_desc = ad_parentUUID;
155 mod->sml_type = ad_parentUUID->ad_cname;
156 mod->sml_values = ch_malloc( sizeof( struct berval ) * 2 );
157 mod->sml_nvalues = NULL;
158 mod->sml_numvals = 1;
160 ber_dupbv( &mod->sml_values[0], &a->a_vals[0] );
161 BER_BVZERO( &mod->sml_values[1] );
163 mod->sml_next = op->orr_modlist;
164 op->orr_modlist = mod;
168 (void)overlay_entry_release_ov( op, e, 0, on );
171 return SLAP_CB_CONTINUE;
179 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
180 Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
181 "pguid_db_init: pguid cannot be used as global overlay.\n" );
185 if ( be->be_nsuffix == NULL ) {
186 Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
187 "pguid_db_init: database must have suffix\n" );
191 if ( BER_BVISNULL( &be->be_rootndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
192 Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
193 "pguid_db_init: missing rootdn for database DN=\"%s\", YMMV\n",
194 be->be_suffix[ 0 ].bv_val );
200 typedef struct pguid_mod_t {
203 struct pguid_mod_t *next;
212 pguid_repair_cb( Operation *op, SlapReply *rs )
215 pguid_repair_cb_t *pcb = op->o_callback->sc_private;
218 struct berval pdn, pndn;
220 switch ( rs->sr_type ) {
232 assert( rs->sr_entry != NULL );
234 dnParent( &rs->sr_entry->e_name, &pdn );
235 dnParent( &rs->sr_entry->e_nname, &pndn );
237 rc = overlay_entry_get_ov( op, &pndn, NULL, slap_schema.si_ad_entryUUID, 0, &e, pcb->on );
238 if ( rc != LDAP_SUCCESS || e == NULL ) {
239 Debug( LDAP_DEBUG_ANY, "%s: pguid_repair_cb: unable to get parent entry DN=\"%s\" (%d)\n",
240 op->o_log_prefix, pdn.bv_val, rc );
244 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
246 Debug( LDAP_DEBUG_ANY, "%s: pguid_repair_cb: unable to find entryUUID of parent entry DN=\"%s\" (%d)\n",
247 op->o_log_prefix, pdn.bv_val, rc );
253 assert( a->a_numvals == 1 );
255 len = sizeof( pguid_mod_t ) + rs->sr_entry->e_nname.bv_len + 1 + a->a_vals[0].bv_len + 1;
256 mod = op->o_tmpalloc( len, op->o_tmpmemctx );
257 mod->ndn.bv_len = rs->sr_entry->e_nname.bv_len;
258 mod->ndn.bv_val = (char *)&mod[1];
259 mod->pguid.bv_len = a->a_vals[0].bv_len;
260 mod->pguid.bv_val = (char *)&mod->ndn.bv_val[mod->ndn.bv_len + 1];
261 lutil_strncopy( mod->ndn.bv_val, rs->sr_entry->e_nname.bv_val, rs->sr_entry->e_nname.bv_len );
262 lutil_strncopy( mod->pguid.bv_val, a->a_vals[0].bv_val, a->a_vals[0].bv_len );
264 mod->next = pcb->mods;
267 Debug( LDAP_DEBUG_TRACE, "%s: pguid_repair_cb: scheduling entry DN=\"%s\" for repair\n",
268 op->o_log_prefix, rs->sr_entry->e_name.bv_val, 0 );
272 (void)overlay_entry_release_ov( op, e, 0, pcb->on );
279 pguid_repair( BackendDB *be )
281 slap_overinst *on = (slap_overinst *)be->bd_info;
282 void *ctx = ldap_pvt_thread_pool_context();
283 Connection conn = { 0 };
284 OperationBuffer opbuf;
286 slap_callback sc = { 0 };
287 pguid_repair_cb_t pcb = { 0 };
288 SlapReply rs = { REP_RESULT };
292 connection_fake_init2( &conn, &opbuf, ctx, 0 );
295 op->o_tag = LDAP_REQ_SEARCH;
296 memset( &op->oq_search, 0, sizeof( op->oq_search ) );
298 op->o_bd = select_backend( &be->be_nsuffix[ 0 ], 0 );
300 op->o_req_dn = op->o_bd->be_suffix[ 0 ];
301 op->o_req_ndn = op->o_bd->be_nsuffix[ 0 ];
303 op->o_dn = op->o_bd->be_rootdn;
304 op->o_ndn = op->o_bd->be_rootndn;
306 op->ors_scope = LDAP_SCOPE_SUBORDINATE;
307 op->ors_tlimit = SLAP_NO_LIMIT;
308 op->ors_slimit = SLAP_NO_LIMIT;
309 op->ors_attrs = slap_anlist_no_attrs;
311 op->ors_filterstr.bv_len = STRLENOF( "(!(=*))" ) + ad_parentUUID->ad_cname.bv_len;
312 op->ors_filterstr.bv_val = op->o_tmpalloc( op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
313 snprintf( op->ors_filterstr.bv_val, op->ors_filterstr.bv_len + 1,
314 "(!(%s=*))", ad_parentUUID->ad_cname.bv_val );
316 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
317 if ( op->ors_filter == NULL ) {
318 rs.sr_err = LDAP_OTHER;
322 op->o_callback = ≻
323 sc.sc_response = pguid_repair_cb;
324 sc.sc_private = &pcb;
327 (void)op->o_bd->bd_info->bi_op_search( op, &rs );
329 op->o_tag = LDAP_REQ_MODIFY;
330 sc.sc_response = slap_null_cb;
331 sc.sc_private = NULL;
332 memset( &op->oq_modify, 0, sizeof( req_modify_s ) );
334 for ( pmod = pcb.mods; pmod != NULL; ) {
338 SlapReply rs2 = { REP_RESULT };
340 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
341 mod->sml_flags = SLAP_MOD_INTERNAL;
342 mod->sml_op = LDAP_MOD_REPLACE;
343 mod->sml_desc = ad_parentUUID;
344 mod->sml_type = ad_parentUUID->ad_cname;
345 mod->sml_values = ch_malloc( sizeof( struct berval ) * 2 );
346 mod->sml_nvalues = NULL;
347 mod->sml_numvals = 1;
348 mod->sml_next = NULL;
350 ber_dupbv( &mod->sml_values[0], &pmod->pguid );
351 BER_BVZERO( &mod->sml_values[1] );
353 op->o_req_dn = pmod->ndn;
354 op->o_req_ndn = pmod->ndn;
356 op->orm_modlist = mod;
357 op->o_bd->be_modify( op, &rs2 );
358 slap_mods_free( op->orm_modlist, 1 );
359 if ( rs2.sr_err == LDAP_SUCCESS ) {
360 Debug( LDAP_DEBUG_TRACE, "%s: pguid_repair: entry DN=\"%s\" repaired\n",
361 op->o_log_prefix, pmod->ndn.bv_val, 0 );
365 Debug( LDAP_DEBUG_ANY, "%s: pguid_repair: entry DN=\"%s\" repair failed (%d)\n",
366 op->o_log_prefix, pmod->ndn.bv_val, rs2.sr_err );
370 op->o_tmpfree( pmod, op->o_tmpmemctx );
375 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
376 filter_free_x( op, op->ors_filter, 1 );
378 Log1( LDAP_DEBUG_STATS, LDAP_LEVEL_INFO,
379 "pguid: repaired=%d\n", nrepaired );
384 /* search all entries without parentUUID; "repair" them */
390 if ( SLAP_SINGLE_SHADOW( be ) ) {
391 Log1( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR,
392 "pguid incompatible with shadow database \"%s\".\n",
393 be->be_suffix[ 0 ].bv_val );
404 AttributeDescription **adp;
406 { "( 1.3.6.1.4.1.4203.666.1.59 "
408 "DESC 'the value of the entryUUID of the parent' "
409 "EQUALITY UUIDMatch "
410 "ORDERING UUIDOrderingMatch "
411 "SYNTAX 1.3.6.1.1.16.1 "
412 "USAGE dSAOperation "
414 "NO-USER-MODIFICATION "
421 pguid_initialize(void)
425 for ( i = 0; as[ i ].desc != NULL; i++ ) {
426 code = register_at( as[ i ].desc, as[ i ].adp, 0 );
428 Debug( LDAP_DEBUG_ANY,
429 "pguid_initialize: register_at #%d failed\n",
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 |=
441 pguid.on_bi.bi_type = "pguid";
443 pguid.on_bi.bi_op_add = pguid_op_add;
444 pguid.on_bi.bi_op_modrdn = pguid_op_rename;
446 pguid.on_bi.bi_db_init = pguid_db_init;
447 pguid.on_bi.bi_db_open = pguid_db_open;
449 return overlay_register( &pguid );
452 #if SLAPD_OVER_PGUID == SLAPD_MOD_DYNAMIC
454 init_module( int argc, char *argv[] )
456 return pguid_initialize();
458 #endif /* SLAPD_OVER_PGUID == SLAPD_MOD_DYNAMIC */
460 #endif /* SLAPD_OVER_PGUID */