1/* pguid.c - Parent GUID value overlay */ 2/* $OpenLDAP$ */ 3/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 4 * 5 * Copyright 1998-2011 The OpenLDAP Foundation. 6 * Portions Copyright 2008 Pierangelo Masarati. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted only as authorized by the OpenLDAP 11 * Public License. 12 * 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>. 16 */ 17/* ACKNOWLEDGEMENTS: 18 * This work was initially developed by Pierangelo Masarati 19 * for inclusion in OpenLDAP Software. 20 */ 21 22#include "portable.h" 23 24#ifdef SLAPD_OVER_PGUID 25 26#include <stdio.h> 27 28#include "ac/string.h" 29#include "ac/socket.h" 30 31#include "slap.h" 32#include "config.h" 33 34#include "lutil.h" 35 36/* 37 * Maintain an attribute (parentUUID) that contains the value 38 * of the entryUUID of the parent entry (used by Samba4) 39 */ 40 41static AttributeDescription *ad_parentUUID; 42 43static slap_overinst pguid; 44 45static int 46pguid_op_add( Operation *op, SlapReply *rs ) 47{ 48 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; 49 50 struct berval pdn, pndn; 51 Entry *e = NULL; 52 Attribute *a; 53 int rc; 54 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; 58 } 59 60 dnParent( &op->o_req_dn, &pdn ); 61 dnParent( &op->o_req_ndn, &pndn ); 62 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; 68 } 69 70 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID ); 71 if ( a == NULL ) { 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 ); 74 75 } else { 76 assert( a->a_numvals == 1 ); 77 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] ); 80 81 } else { 82 Modifications *ml; 83 Modifications *mod; 84 85 assert( op->ora_modlist != NULL ); 86 87 for ( ml = op->ora_modlist; ml != NULL; ml = ml->sml_next ) { 88 if ( ml->sml_mod.sm_desc == slap_schema.si_ad_entryUUID ) { 89 break; 90 } 91 } 92 93 if ( ml == NULL ) { 94 ml = op->ora_modlist; 95 } 96 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; 105 106 ber_dupbv( &mod->sml_values[0], &a->a_vals[0] ); 107 BER_BVZERO( &mod->sml_values[1] ); 108 109 mod->sml_next = ml->sml_next; 110 ml->sml_next = mod; 111 } 112 } 113 114 if ( e != NULL ) { 115 (void)overlay_entry_release_ov( op, e, 0, on ); 116 } 117 118 return SLAP_CB_CONTINUE; 119} 120 121static int 122pguid_op_rename( Operation *op, SlapReply *rs ) 123{ 124 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; 125 126 Entry *e = NULL; 127 Attribute *a; 128 int rc; 129 130 if ( op->orr_nnewSup == NULL ) { 131 return SLAP_CB_CONTINUE; 132 } 133 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; 139 } 140 141 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID ); 142 if ( a == NULL ) { 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 ); 145 146 } else { 147 Modifications *mod; 148 149 assert( a->a_numvals == 1 ); 150 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; 159 160 ber_dupbv( &mod->sml_values[0], &a->a_vals[0] ); 161 BER_BVZERO( &mod->sml_values[1] ); 162 163 mod->sml_next = op->orr_modlist; 164 op->orr_modlist = mod; 165 } 166 167 if ( e != NULL ) { 168 (void)overlay_entry_release_ov( op, e, 0, on ); 169 } 170 171 return SLAP_CB_CONTINUE; 172} 173 174static int 175pguid_db_init( 176 BackendDB *be, 177 ConfigReply *cr) 178{ 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" ); 182 return 1; 183 } 184 185 if ( be->be_nsuffix == NULL ) { 186 Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_ERR, 187 "pguid_db_init: database must have suffix\n" ); 188 return 1; 189 } 190 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 ); 195 } 196 197 return 0; 198} 199 200typedef struct pguid_mod_t { 201 struct berval ndn; 202 struct berval pguid; 203 struct pguid_mod_t *next; 204} pguid_mod_t; 205 206typedef struct { 207 slap_overinst *on; 208 pguid_mod_t *mods; 209} pguid_repair_cb_t; 210 211static int 212pguid_repair_cb( Operation *op, SlapReply *rs ) 213{ 214 int rc; 215 pguid_repair_cb_t *pcb = op->o_callback->sc_private; 216 Entry *e = NULL; 217 Attribute *a; 218 struct berval pdn, pndn; 219 220 switch ( rs->sr_type ) { 221 case REP_SEARCH: 222 break; 223 224 case REP_SEARCHREF: 225 case REP_RESULT: 226 return rs->sr_err; 227 228 default: 229 assert( 0 ); 230 } 231 232 assert( rs->sr_entry != NULL ); 233 234 dnParent( &rs->sr_entry->e_name, &pdn ); 235 dnParent( &rs->sr_entry->e_nname, &pndn ); 236 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 ); 241 return 0; 242 } 243 244 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID ); 245 if ( a == NULL ) { 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 ); 248 249 } else { 250 ber_len_t len; 251 pguid_mod_t *mod; 252 253 assert( a->a_numvals == 1 ); 254 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 ); 263 264 mod->next = pcb->mods; 265 pcb->mods = mod; 266 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 ); 269 } 270 271 if ( e != NULL ) { 272 (void)overlay_entry_release_ov( op, e, 0, pcb->on ); 273 } 274 275 return 0; 276} 277 278static int 279pguid_repair( BackendDB *be ) 280{ 281 slap_overinst *on = (slap_overinst *)be->bd_info; 282 void *ctx = ldap_pvt_thread_pool_context(); 283 Connection conn = { 0 }; 284 OperationBuffer opbuf; 285 Operation *op; 286 slap_callback sc = { 0 }; 287 pguid_repair_cb_t pcb = { 0 }; 288 SlapReply rs = { REP_RESULT }; 289 pguid_mod_t *pmod; 290 int nrepaired = 0; 291 292 connection_fake_init2( &conn, &opbuf, ctx, 0 ); 293 op = &opbuf.ob_op; 294 295 op->o_tag = LDAP_REQ_SEARCH; 296 memset( &op->oq_search, 0, sizeof( op->oq_search ) ); 297 298 op->o_bd = select_backend( &be->be_nsuffix[ 0 ], 0 ); 299 300 op->o_req_dn = op->o_bd->be_suffix[ 0 ]; 301 op->o_req_ndn = op->o_bd->be_nsuffix[ 0 ]; 302 303 op->o_dn = op->o_bd->be_rootdn; 304 op->o_ndn = op->o_bd->be_rootndn; 305 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; 310 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 ); 315 316 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val ); 317 if ( op->ors_filter == NULL ) { 318 rs.sr_err = LDAP_OTHER; 319 goto done_search; 320 } 321 322 op->o_callback = ≻ 323 sc.sc_response = pguid_repair_cb; 324 sc.sc_private = &pcb; 325 pcb.on = on; 326 327 (void)op->o_bd->bd_info->bi_op_search( op, &rs ); 328 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 ) ); 333 334 for ( pmod = pcb.mods; pmod != NULL; ) { 335 pguid_mod_t *pnext; 336 337 Modifications *mod; 338 SlapReply rs2 = { REP_RESULT }; 339 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; 349 350 ber_dupbv( &mod->sml_values[0], &pmod->pguid ); 351 BER_BVZERO( &mod->sml_values[1] ); 352 353 op->o_req_dn = pmod->ndn; 354 op->o_req_ndn = pmod->ndn; 355 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 ); 362 nrepaired++; 363 364 } else { 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 ); 367 } 368 369 pnext = pmod->next; 370 op->o_tmpfree( pmod, op->o_tmpmemctx ); 371 pmod = pnext; 372 } 373 374done_search:; 375 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx ); 376 filter_free_x( op, op->ors_filter, 1 ); 377 378 Log1( LDAP_DEBUG_STATS, LDAP_LEVEL_INFO, 379 "pguid: repaired=%d\n", nrepaired ); 380 381 return rs.sr_err; 382} 383 384/* search all entries without parentUUID; "repair" them */ 385static int 386pguid_db_open( 387 BackendDB *be, 388 ConfigReply *cr ) 389{ 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 ); 394 return 1; 395 } 396 397 pguid_repair( be ); 398 399 return 0; 400} 401 402static struct { 403 char *desc; 404 AttributeDescription **adp; 405} as[] = { 406 { "( 1.3.6.1.4.1.4203.666.1.59 " 407 "NAME 'parentUUID' " 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 " 413 "SINGLE-VALUE " 414 "NO-USER-MODIFICATION " 415 ")", 416 &ad_parentUUID }, 417 { NULL } 418}; 419 420int 421pguid_initialize(void) 422{ 423 int code, i; 424 425 for ( i = 0; as[ i ].desc != NULL; i++ ) { 426 code = register_at( as[ i ].desc, as[ i ].adp, 0 ); 427 if ( code ) { 428 Debug( LDAP_DEBUG_ANY, 429 "pguid_initialize: register_at #%d failed\n", 430 i, 0, 0 ); 431 return code; 432 } 433 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 |= 437 SLAP_AT_MANAGEABLE; 438 } 439 } 440 441 pguid.on_bi.bi_type = "pguid"; 442 443 pguid.on_bi.bi_op_add = pguid_op_add; 444 pguid.on_bi.bi_op_modrdn = pguid_op_rename; 445 446 pguid.on_bi.bi_db_init = pguid_db_init; 447 pguid.on_bi.bi_db_open = pguid_db_open; 448 449 return overlay_register( &pguid ); 450} 451 452#if SLAPD_OVER_PGUID == SLAPD_MOD_DYNAMIC 453int 454init_module( int argc, char *argv[] ) 455{ 456 return pguid_initialize(); 457} 458#endif /* SLAPD_OVER_PGUID == SLAPD_MOD_DYNAMIC */ 459 460#endif /* SLAPD_OVER_PGUID */ 461