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