1/* $NetBSD$ */ 2 3/* OpenLDAP: pkg/ldap/servers/slapd/slapadd.c,v 1.36.2.17 2010/04/19 16:53:02 quanah Exp */ 4/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1998-2010 The OpenLDAP Foundation. 7 * Portions Copyright 1998-2003 Kurt D. Zeilenga. 8 * Portions Copyright 2003 IBM Corporation. 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 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 Kurt Zeilenga for inclusion 21 * in OpenLDAP Software. Additional signficant contributors include 22 * Jong Hyuk Choi 23 * Pierangelo Masarati 24 */ 25 26#include "portable.h" 27 28#include <stdio.h> 29 30#include <ac/stdlib.h> 31 32#include <ac/ctype.h> 33#include <ac/string.h> 34#include <ac/socket.h> 35#include <ac/unistd.h> 36 37#include <lber.h> 38#include <ldif.h> 39#include <lutil.h> 40#include <lutil_meter.h> 41#include <sys/stat.h> 42 43#include "slapcommon.h" 44 45static char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ]; 46static char maxcsnbuf[ LDAP_PVT_CSNSTR_BUFSIZE * ( SLAP_SYNC_SID_MAX + 1 ) ]; 47 48int 49slapadd( int argc, char **argv ) 50{ 51 char *buf = NULL; 52 const char *text; 53 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' }; 54 size_t textlen = sizeof textbuf; 55 const char *progname = "slapadd"; 56 57 struct berval csn; 58 struct berval maxcsn[ SLAP_SYNC_SID_MAX + 1 ]; 59 unsigned long sid; 60 struct berval bvtext; 61 Attribute *attr; 62 Entry *ctxcsn_e; 63 ID ctxcsn_id, id; 64 OperationBuffer opbuf; 65 Operation *op; 66 67 int match; 68 int checkvals; 69 int lineno, nextline, ldifrc; 70 int lmax; 71 int rc = EXIT_SUCCESS; 72 int manage = 0; 73 74 int enable_meter = 0; 75 lutil_meter_t meter; 76 struct stat stat_buf; 77 78 /* default "000" */ 79 csnsid = 0; 80 81 if ( isatty (2) ) enable_meter = 1; 82 slap_tool_init( progname, SLAPADD, argc, argv ); 83 84 memset( &opbuf, 0, sizeof(opbuf) ); 85 op = &opbuf.ob_op; 86 op->o_hdr = &opbuf.ob_hdr; 87 88 if( !be->be_entry_open || 89 !be->be_entry_close || 90 !be->be_entry_put || 91 (update_ctxcsn && 92 (!be->be_dn2id_get || 93 !be->be_entry_get || 94 !be->be_entry_modify)) ) 95 { 96 fprintf( stderr, "%s: database doesn't support necessary operations.\n", 97 progname ); 98 if ( dryrun ) { 99 fprintf( stderr, "\t(dry) continuing...\n" ); 100 101 } else { 102 exit( EXIT_FAILURE ); 103 } 104 } 105 106 checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1; 107 108 lmax = 0; 109 nextline = 0; 110 111 /* enforce schema checking unless not disabled */ 112 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) { 113 SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK); 114 } 115 116 if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) { 117 fprintf( stderr, "%s: could not open database.\n", 118 progname ); 119 exit( EXIT_FAILURE ); 120 } 121 122 if ( update_ctxcsn ) { 123 maxcsn[ 0 ].bv_val = maxcsnbuf; 124 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) { 125 maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_PVT_CSNSTR_BUFSIZE; 126 maxcsn[ sid ].bv_len = 0; 127 } 128 } 129 130 if ( enable_meter 131#ifdef LDAP_DEBUG 132 /* tools default to "none" */ 133 && slap_debug == LDAP_DEBUG_NONE 134#endif 135 && !fstat ( fileno ( ldiffp->fp ), &stat_buf ) 136 && S_ISREG(stat_buf.st_mode) ) { 137 enable_meter = !lutil_meter_open( 138 &meter, 139 &lutil_meter_text_display, 140 &lutil_meter_linear_estimator, 141 stat_buf.st_size); 142 } else { 143 enable_meter = 0; 144 } 145 146 /* nextline is the line number of the end of the current entry */ 147 for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0; 148 lineno=nextline+1 ) 149 { 150 BackendDB *bd; 151 Entry *e; 152 153 if ( lineno < jumpline ) 154 continue; 155 156 e = str2entry2( buf, checkvals ); 157 158 if ( enable_meter ) 159 lutil_meter_update( &meter, 160 ftell( ldiffp->fp ), 161 0); 162 163 /* 164 * Initialize text buffer 165 */ 166 bvtext.bv_len = textlen; 167 bvtext.bv_val = textbuf; 168 bvtext.bv_val[0] = '\0'; 169 170 if( e == NULL ) { 171 fprintf( stderr, "%s: could not parse entry (line=%d)\n", 172 progname, lineno ); 173 rc = EXIT_FAILURE; 174 if( continuemode ) continue; 175 break; 176 } 177 178 /* make sure the DN is not empty */ 179 if( BER_BVISEMPTY( &e->e_nname ) && 180 !BER_BVISEMPTY( be->be_nsuffix )) 181 { 182 fprintf( stderr, "%s: line %d: " 183 "cannot add entry with empty dn=\"%s\"", 184 progname, lineno, e->e_dn ); 185 bd = select_backend( &e->e_nname, nosubordinates ); 186 if ( bd ) { 187 BackendDB *bdtmp; 188 int dbidx = 0; 189 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) { 190 if ( bdtmp == bd ) break; 191 dbidx++; 192 } 193 194 assert( bdtmp != NULL ); 195 196 fprintf( stderr, "; did you mean to use database #%d (%s)?", 197 dbidx, 198 bd->be_suffix[0].bv_val ); 199 200 } 201 fprintf( stderr, "\n" ); 202 rc = EXIT_FAILURE; 203 entry_free( e ); 204 if( continuemode ) continue; 205 break; 206 } 207 208 /* check backend */ 209 bd = select_backend( &e->e_nname, nosubordinates ); 210 if ( bd != be ) { 211 fprintf( stderr, "%s: line %d: " 212 "database #%d (%s) not configured to hold \"%s\"", 213 progname, lineno, 214 dbnum, 215 be->be_suffix[0].bv_val, 216 e->e_dn ); 217 if ( bd ) { 218 BackendDB *bdtmp; 219 int dbidx = 0; 220 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) { 221 if ( bdtmp == bd ) break; 222 dbidx++; 223 } 224 225 assert( bdtmp != NULL ); 226 227 fprintf( stderr, "; did you mean to use database #%d (%s)?", 228 dbidx, 229 bd->be_suffix[0].bv_val ); 230 231 } else { 232 fprintf( stderr, "; no database configured for that naming context" ); 233 } 234 fprintf( stderr, "\n" ); 235 rc = EXIT_FAILURE; 236 entry_free( e ); 237 if( continuemode ) continue; 238 break; 239 } 240 241 { 242 Attribute *oc = attr_find( e->e_attrs, 243 slap_schema.si_ad_objectClass ); 244 245 if( oc == NULL ) { 246 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n", 247 progname, e->e_dn, lineno, 248 "no objectClass attribute"); 249 rc = EXIT_FAILURE; 250 entry_free( e ); 251 if( continuemode ) continue; 252 break; 253 } 254 255 /* check schema */ 256 op->o_bd = be; 257 258 if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) { 259 rc = entry_schema_check( op, e, NULL, manage, 1, NULL, 260 &text, textbuf, textlen ); 261 262 if( rc != LDAP_SUCCESS ) { 263 fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n", 264 progname, e->e_dn, lineno, rc, text ); 265 rc = EXIT_FAILURE; 266 entry_free( e ); 267 if( continuemode ) continue; 268 break; 269 } 270 textbuf[ 0 ] = '\0'; 271 } 272 } 273 274 if ( SLAP_LASTMOD(be) ) { 275 time_t now = slap_get_time(); 276 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ]; 277 struct berval vals[ 2 ]; 278 279 struct berval name, timestamp; 280 281 struct berval nvals[ 2 ]; 282 struct berval nname; 283 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ]; 284 285 enum { 286 GOT_NONE = 0x0, 287 GOT_CSN = 0x1, 288 GOT_UUID = 0x2, 289 GOT_ALL = (GOT_CSN|GOT_UUID) 290 } got = GOT_ALL; 291 292 vals[1].bv_len = 0; 293 vals[1].bv_val = NULL; 294 295 nvals[1].bv_len = 0; 296 nvals[1].bv_val = NULL; 297 298 csn.bv_len = ldap_pvt_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 ); 299 csn.bv_val = csnbuf; 300 301 timestamp.bv_val = timebuf; 302 timestamp.bv_len = sizeof(timebuf); 303 304 slap_timestamp( &now, ×tamp ); 305 306 if ( BER_BVISEMPTY( &be->be_rootndn ) ) { 307 BER_BVSTR( &name, SLAPD_ANONYMOUS ); 308 nname = name; 309 } else { 310 name = be->be_rootdn; 311 nname = be->be_rootndn; 312 } 313 314 if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID ) 315 == NULL ) 316 { 317 got &= ~GOT_UUID; 318 vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) ); 319 vals[0].bv_val = uuidbuf; 320 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL ); 321 } 322 323 if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName ) 324 == NULL ) 325 { 326 vals[0] = name; 327 nvals[0] = nname; 328 attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals ); 329 } 330 331 if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp ) 332 == NULL ) 333 { 334 vals[0] = timestamp; 335 attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL ); 336 } 337 338 if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN ) 339 == NULL ) 340 { 341 got &= ~GOT_CSN; 342 vals[0] = csn; 343 attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL ); 344 } 345 346 if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName ) 347 == NULL ) 348 { 349 vals[0] = name; 350 nvals[0] = nname; 351 attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals ); 352 } 353 354 if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp ) 355 == NULL ) 356 { 357 vals[0] = timestamp; 358 attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL ); 359 } 360 361 if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) { 362 char buf[SLAP_TEXT_BUFLEN]; 363 364 snprintf( buf, sizeof(buf), 365 "%s%s%s", 366 ( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ), 367 ( !(got & GOT_CSN) ? "," : "" ), 368 ( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) ); 369 370 Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n", 371 progname, buf, e->e_name.bv_val ); 372 } 373 374 if ( update_ctxcsn ) { 375 int rc_sid; 376 377 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN ); 378 assert( attr != NULL ); 379 380 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] ); 381 if ( rc_sid < 0 ) { 382 Debug( LDAP_DEBUG_ANY, "%s: could not " 383 "extract SID from entryCSN=%s, entry dn=\"%s\"\n", 384 progname, attr->a_nvals[ 0 ].bv_val, e->e_name.bv_val ); 385 386 } else { 387 assert( rc_sid <= SLAP_SYNC_SID_MAX ); 388 389 sid = (unsigned)rc_sid; 390 if ( maxcsn[ sid ].bv_len != 0 ) { 391 match = 0; 392 value_match( &match, slap_schema.si_ad_entryCSN, 393 slap_schema.si_ad_entryCSN->ad_type->sat_ordering, 394 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX, 395 &maxcsn[ sid ], &attr->a_nvals[0], &text ); 396 } else { 397 match = -1; 398 } 399 if ( match < 0 ) { 400 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val ); 401 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len; 402 } 403 } 404 } 405 } 406 407 if ( !dryrun ) { 408 id = be->be_entry_put( be, e, &bvtext ); 409 if( id == NOID ) { 410 fprintf( stderr, "%s: could not add entry dn=\"%s\" " 411 "(line=%d): %s\n", progname, e->e_dn, 412 lineno, bvtext.bv_val ); 413 rc = EXIT_FAILURE; 414 entry_free( e ); 415 if( continuemode ) continue; 416 break; 417 } 418 if ( verbose ) 419 fprintf( stderr, "added: \"%s\" (%08lx)\n", 420 e->e_dn, (long) id ); 421 } else { 422 if ( verbose ) 423 fprintf( stderr, "added: \"%s\"\n", 424 e->e_dn ); 425 } 426 427 entry_free( e ); 428 } 429 430 if ( ldifrc < 0 ) 431 rc = EXIT_FAILURE; 432 433 bvtext.bv_len = textlen; 434 bvtext.bv_val = textbuf; 435 bvtext.bv_val[0] = '\0'; 436 437 if ( enable_meter ) { 438 lutil_meter_update( &meter, ftell( ldiffp->fp ), 1); 439 lutil_meter_close( &meter ); 440 } 441 442 if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) { 443 struct berval ctxdn; 444 if ( SLAP_SYNC_SUBENTRY( be )) { 445 build_new_dn( &ctxdn, &be->be_nsuffix[0], 446 (struct berval *)&slap_ldapsync_cn_bv, NULL ); 447 } else { 448 ctxdn = be->be_nsuffix[0]; 449 } 450 ctxcsn_id = be->be_dn2id_get( be, &ctxdn ); 451 if ( ctxcsn_id == NOID ) { 452 if ( SLAP_SYNC_SUBENTRY( be )) { 453 ctxcsn_e = slap_create_context_csn_entry( be, NULL ); 454 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) { 455 if ( maxcsn[ sid ].bv_len ) { 456 attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN, 457 &maxcsn[ sid ], NULL ); 458 } 459 } 460 ctxcsn_id = be->be_entry_put( be, ctxcsn_e, &bvtext ); 461 if ( ctxcsn_id == NOID ) { 462 fprintf( stderr, "%s: couldn't create context entry\n", progname ); 463 rc = EXIT_FAILURE; 464 } 465 } else { 466 fprintf( stderr, "%s: context entry is missing\n", progname ); 467 rc = EXIT_FAILURE; 468 } 469 } else { 470 ctxcsn_e = be->be_entry_get( be, ctxcsn_id ); 471 if ( ctxcsn_e != NULL ) { 472 Entry *e = entry_dup( ctxcsn_e ); 473 int change; 474 attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN ); 475 if ( attr ) { 476 int i; 477 478 change = 0; 479 480 for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) { 481 int rc_sid; 482 483 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] ); 484 if ( rc_sid < 0 ) { 485 Debug( LDAP_DEBUG_ANY, 486 "%s: unable to extract SID " 487 "from #%d contextCSN=%s\n", 488 progname, i, 489 attr->a_nvals[ i ].bv_val ); 490 continue; 491 } 492 493 assert( rc_sid <= SLAP_SYNC_SID_MAX ); 494 495 sid = (unsigned)rc_sid; 496 497 if ( maxcsn[ sid ].bv_len == 0 ) { 498 match = -1; 499 500 } else { 501 value_match( &match, slap_schema.si_ad_entryCSN, 502 slap_schema.si_ad_entryCSN->ad_type->sat_ordering, 503 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX, 504 &maxcsn[ sid ], &attr->a_nvals[i], &text ); 505 } 506 507 if ( match > 0 ) { 508 change = 1; 509 } else { 510 AC_MEMCPY( maxcsn[ sid ].bv_val, 511 attr->a_nvals[ i ].bv_val, 512 attr->a_nvals[ i ].bv_len ); 513 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0'; 514 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len; 515 } 516 } 517 518 if ( change ) { 519 if ( attr->a_nvals != attr->a_vals ) { 520 ber_bvarray_free( attr->a_nvals ); 521 } 522 attr->a_nvals = NULL; 523 ber_bvarray_free( attr->a_vals ); 524 attr->a_vals = NULL; 525 attr->a_numvals = 0; 526 } 527 } else { 528 change = 1; 529 } 530 531 if ( change ) { 532 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) { 533 if ( maxcsn[ sid ].bv_len ) { 534 attr_merge_one( e, slap_schema.si_ad_contextCSN, 535 &maxcsn[ sid], NULL ); 536 } 537 } 538 539 ctxcsn_id = be->be_entry_modify( be, e, &bvtext ); 540 if( ctxcsn_id == NOID ) { 541 fprintf( stderr, "%s: could not modify ctxcsn\n", 542 progname); 543 rc = EXIT_FAILURE; 544 } else if ( verbose ) { 545 fprintf( stderr, "modified: \"%s\" (%08lx)\n", 546 e->e_dn, (long) ctxcsn_id ); 547 } 548 } 549 entry_free( e ); 550 } 551 } 552 } 553 554 ch_free( buf ); 555 556 if ( !dryrun ) { 557 if ( enable_meter ) { 558 fprintf( stderr, "Closing DB..." ); 559 } 560 if( be->be_entry_close( be ) ) { 561 rc = EXIT_FAILURE; 562 } 563 564 if( be->be_sync ) { 565 be->be_sync( be ); 566 } 567 if ( enable_meter ) { 568 fprintf( stderr, "\n" ); 569 } 570 } 571 572 if ( slap_tool_destroy()) 573 rc = EXIT_FAILURE; 574 575 return rc; 576} 577 578