slapadd.c 13 KB
Newer Older
1
2
3
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
4
 * Copyright 1998-2009 The OpenLDAP Foundation.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
 * Portions Copyright 2003 IBM Corporation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Kurt Zeilenga for inclusion
 * in OpenLDAP Software.  Additional signficant contributors include
 *    Jong Hyuk Choi
 *    Pierangelo Masarati
 */

#include "portable.h"

#include <stdio.h>

#include <ac/stdlib.h>

#include <ac/ctype.h>
#include <ac/string.h>
#include <ac/socket.h>
#include <ac/unistd.h>

#include <lber.h>
#include <ldif.h>
#include <lutil.h>
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
38
39
#include <lutil_meter.h>
#include <sys/stat.h>
40
41
42
43

#include "slapcommon.h"

static char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
44
static char maxcsnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE * ( SLAP_SYNC_SID_MAX + 1 ) ];
45
46
47
48

int
slapadd( int argc, char **argv )
{
Howard Chu's avatar
Howard Chu committed
49
	char *buf = NULL;
50
51
52
	const char *text;
	char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
	size_t textlen = sizeof textbuf;
53
	const char *progname = "slapadd";
54
55

	struct berval csn;
56
57
	struct berval maxcsn[ SLAP_SYNC_SID_MAX + 1 ];
	unsigned long sid;
Howard Chu's avatar
Howard Chu committed
58
	struct berval bvtext;
59
60
	Attribute *attr;
	Entry *ctxcsn_e;
61
	ID	ctxcsn_id, id;
62
	OperationBuffer opbuf;
Howard Chu's avatar
Howard Chu committed
63
64
65
	Operation *op;

	int match;
66
	int checkvals;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
67
	int lineno, nextline, ldifrc;
Howard Chu's avatar
Howard Chu committed
68
69
70
	int lmax;
	int rc = EXIT_SUCCESS;
	int manage = 0;	
71

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
72
73
74
75
	int enable_meter = 0;
	lutil_meter_t meter;
	struct stat stat_buf;

76
77
78
	/* default "000" */
	csnsid = 0;

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
79
	if ( isatty (2) ) enable_meter = 1;
80
81
	slap_tool_init( progname, SLAPADD, argc, argv );

82
	memset( &opbuf, 0, sizeof(opbuf) );
83
84
	op = &opbuf.ob_op;
	op->o_hdr = &opbuf.ob_hdr;
85

86
87
	if( !be->be_entry_open ||
		!be->be_entry_close ||
88
89
90
		!be->be_entry_put ||
		(update_ctxcsn &&
		 (!be->be_dn2id_get ||
91
		  !be->be_entry_get ||
92
		  !be->be_entry_modify)) )
93
94
95
	{
		fprintf( stderr, "%s: database doesn't support necessary operations.\n",
			progname );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
96
97
98
99
100
101
		if ( dryrun ) {
			fprintf( stderr, "\t(dry) continuing...\n" );

		} else {
			exit( EXIT_FAILURE );
		}
102
103
	}

104
105
	checkvals = (slapMode & SLAP_TOOL_QUICK) ? 0 : 1;

106
	lmax = 0;
107
	nextline = 0;
108

109
110
111
112
113
	/* enforce schema checking unless not disabled */
	if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
		SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK);
	}

114
	if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) {
115
116
117
118
119
		fprintf( stderr, "%s: could not open database.\n",
			progname );
		exit( EXIT_FAILURE );
	}

120
	if ( update_ctxcsn ) {
121
122
123
124
125
		maxcsn[ 0 ].bv_val = maxcsnbuf;
		for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
			maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_LUTIL_CSNSTR_BUFSIZE;
			maxcsn[ sid ].bv_len = 0;
		}
126
127
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	if ( enable_meter 
#ifdef LDAP_DEBUG
		/* tools default to "none" */
		&& slap_debug == LDAP_DEBUG_NONE
#endif
		&& !fstat ( fileno ( ldiffp->fp ), &stat_buf )
		&& S_ISREG(stat_buf.st_mode) ) {
		enable_meter = !lutil_meter_open(
			&meter,
			&lutil_meter_text_display,
			&lutil_meter_linear_estimator,
			stat_buf.st_size);
	} else {
		enable_meter = 0;
	}

144
	/* nextline is the line number of the end of the current entry */
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
145
	for( lineno=1; ( ldifrc = ldif_read_record( ldiffp, &nextline, &buf, &lmax )) > 0;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
146
147
148
		lineno=nextline+1 )
	{
		BackendDB *bd;
Howard Chu's avatar
Howard Chu committed
149
		Entry *e;
150
151
152
153

		if ( lineno < jumpline )
			continue;

Howard Chu's avatar
Howard Chu committed
154
		e = str2entry2( buf, checkvals );
155

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
156
157
158
159
160
		if ( enable_meter )
			lutil_meter_update( &meter,
					 ftell( ldiffp->fp ),
					 0);

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
		/*
		 * Initialize text buffer
		 */
		bvtext.bv_len = textlen;
		bvtext.bv_val = textbuf;
		bvtext.bv_val[0] = '\0';

		if( e == NULL ) {
			fprintf( stderr, "%s: could not parse entry (line=%d)\n",
				progname, lineno );
			rc = EXIT_FAILURE;
			if( continuemode ) continue;
			break;
		}

		/* make sure the DN is not empty */
177
		if( BER_BVISEMPTY( &e->e_nname ) &&
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
			!BER_BVISEMPTY( be->be_nsuffix ))
		{
			fprintf( stderr, "%s: line %d: "
				"cannot add entry with empty dn=\"%s\"",
				progname, lineno, e->e_dn );
			bd = select_backend( &e->e_nname, nosubordinates );
			if ( bd ) {
				BackendDB *bdtmp;
				int dbidx = 0;
				LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
					if ( bdtmp == bd ) break;
					dbidx++;
				}

				assert( bdtmp != NULL );
				
				fprintf( stderr, "; did you mean to use database #%d (%s)?",
					dbidx,
					bd->be_suffix[0].bv_val );

			}
			fprintf( stderr, "\n" );
200
201
202
203
204
205
206
			rc = EXIT_FAILURE;
			entry_free( e );
			if( continuemode ) continue;
			break;
		}

		/* check backend */
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
207
208
		bd = select_backend( &e->e_nname, nosubordinates );
		if ( bd != be ) {
209
			fprintf( stderr, "%s: line %d: "
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
210
				"database #%d (%s) not configured to hold \"%s\"",
211
				progname, lineno,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
212
213
				dbnum,
				be->be_suffix[0].bv_val,
214
				e->e_dn );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
			if ( bd ) {
				BackendDB *bdtmp;
				int dbidx = 0;
				LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
					if ( bdtmp == bd ) break;
					dbidx++;
				}

				assert( bdtmp != NULL );
				
				fprintf( stderr, "; did you mean to use database #%d (%s)?",
					dbidx,
					bd->be_suffix[0].bv_val );

			} else {
				fprintf( stderr, "; no database configured for that naming context" );
			}
			fprintf( stderr, "\n" );
233
234
235
236
237
238
			rc = EXIT_FAILURE;
			entry_free( e );
			if( continuemode ) continue;
			break;
		}

239
		{
240
241
242
243
244
245
246
247
248
249
250
251
252
253
			Attribute *oc = attr_find( e->e_attrs,
				slap_schema.si_ad_objectClass );

			if( oc == NULL ) {
				fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
					progname, e->e_dn, lineno,
					"no objectClass attribute");
				rc = EXIT_FAILURE;
				entry_free( e );
				if( continuemode ) continue;
				break;
			}

			/* check schema */
254
255
			op->o_bd = be;

256
			if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
257
				rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
258
					&text, textbuf, textlen );
259

260
261
262
263
264
265
266
267
				if( rc != LDAP_SUCCESS ) {
					fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
						progname, e->e_dn, lineno, rc, text );
					rc = EXIT_FAILURE;
					entry_free( e );
					if( continuemode ) continue;
					break;
				}
268
				textbuf[ 0 ] = '\0';
269
270
271
272
273
274
275
276
277
278
279
280
281
282
			}
		}

		if ( SLAP_LASTMOD(be) ) {
			time_t now = slap_get_time();
			char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
			struct berval vals[ 2 ];

			struct berval name, timestamp;

			struct berval nvals[ 2 ];
			struct berval nname;
			char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
283
284
285
286
287
288
289
			enum {
				GOT_NONE = 0x0,
				GOT_CSN = 0x1,
				GOT_UUID = 0x2,
				GOT_ALL = (GOT_CSN|GOT_UUID)
			} got = GOT_ALL;

290
291
292
293
294
295
			vals[1].bv_len = 0;
			vals[1].bv_val = NULL;

			nvals[1].bv_len = 0;
			nvals[1].bv_val = NULL;

296
			csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
297
298
299
			csn.bv_val = csnbuf;

			timestamp.bv_val = timebuf;
300
301
302
			timestamp.bv_len = sizeof(timebuf);

			slap_timestamp( &now, &timestamp );
303

Pierangelo Masarati's avatar
Pierangelo Masarati committed
304
305
306
			if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
				BER_BVSTR( &name, SLAPD_ANONYMOUS );
				nname = name;
307
308
309
310
311
312
313
314
			} else {
				name = be->be_rootdn;
				nname = be->be_rootndn;
			}

			if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
				== NULL )
			{
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
315
				got &= ~GOT_UUID;
316
317
				vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
				vals[0].bv_val = uuidbuf;
318
				attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
319
320
321
322
323
324
325
326
327
328
			}

			if( attr_find( e->e_attrs, slap_schema.si_ad_creatorsName )
				== NULL )
			{
				vals[0] = name;
				nvals[0] = nname;
				attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals );
			}

329
			if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
330
331
				== NULL )
			{
332
333
				vals[0] = timestamp;
				attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
334
335
			}

336
			if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
337
338
				== NULL )
			{
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
339
				got &= ~GOT_CSN;
340
341
				vals[0] = csn;
				attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
342
343
			}

344
			if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
345
346
				== NULL )
			{
347
348
349
				vals[0] = name;
				nvals[0] = nname;
				attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
350
351
			}

352
			if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
353
354
				== NULL )
			{
355
356
				vals[0] = timestamp;
				attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
357
358
			}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
359
360
361
362
363
364
365
366
367
368
369
370
371
			if ( SLAP_SINGLE_SHADOW(be) && got != GOT_ALL ) {
				char buf[SLAP_TEXT_BUFLEN];

				snprintf( buf, sizeof(buf),
					"%s%s%s",
					( !(got & GOT_UUID) ? slap_schema.si_ad_entryUUID->ad_cname.bv_val : "" ),
					( !(got & GOT_CSN) ? "," : "" ),
					( !(got & GOT_CSN) ? slap_schema.si_ad_entryCSN->ad_cname.bv_val : "" ) );

				Debug( LDAP_DEBUG_ANY, "%s: warning, missing attrs %s from entry dn=\"%s\"\n",
					progname, buf, e->e_name.bv_val );
			}

372
			if ( update_ctxcsn ) {
373
374
				int rc_sid;

375
				attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
376
377
378
379
380
				assert( attr != NULL );

				rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
				if ( rc_sid < 0 ) {
					Debug( LDAP_DEBUG_ANY, "%s: could not "
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
381
382
						"extract SID from entryCSN=%s, entry dn=\"%s\"\n",
						progname, attr->a_nvals[ 0 ].bv_val, e->e_name.bv_val );
383

384
				} else {
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
					assert( rc_sid <= SLAP_SYNC_SID_MAX );

					sid = (unsigned)rc_sid;
					if ( maxcsn[ sid ].bv_len != 0 ) {
						match = 0;
						value_match( &match, slap_schema.si_ad_entryCSN,
							slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
							SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
							&maxcsn[ sid ], &attr->a_nvals[0], &text );
					} else {
						match = -1;
					}
					if ( match < 0 ) {
						strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
						maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
					}
401
402
403
404
				}
			}
		}

405
406
407
408
409
410
411
412
413
414
		if ( !dryrun ) {
			id = be->be_entry_put( be, e, &bvtext );
			if( id == NOID ) {
				fprintf( stderr, "%s: could not add entry dn=\"%s\" "
								 "(line=%d): %s\n", progname, e->e_dn,
								 lineno, bvtext.bv_val );
				rc = EXIT_FAILURE;
				entry_free( e );
				if( continuemode ) continue;
				break;
415
			}
416
			if ( verbose )
417
418
				fprintf( stderr, "added: \"%s\" (%08lx)\n",
					e->e_dn, (long) id );
419
420
421
422
		} else {
			if ( verbose )
				fprintf( stderr, "added: \"%s\"\n",
					e->e_dn );
423
424
425
426
427
		}

		entry_free( e );
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
428
429
430
	if ( ldifrc < 0 )
		rc = EXIT_FAILURE;

431
432
433
434
	bvtext.bv_len = textlen;
	bvtext.bv_val = textbuf;
	bvtext.bv_val[0] = '\0';

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
435
436
437
438
439
	if ( enable_meter ) {
		lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
		lutil_meter_close( &meter );
	}

440
	if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
441
442
443
444
445
		ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix );
		if ( ctxcsn_id == NOID ) {
			fprintf( stderr, "%s: context entry is missing\n", progname );
			rc = EXIT_FAILURE;
		} else {
446
447
448
			ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
			if ( ctxcsn_e != NULL ) {
				Entry *e = entry_dup( ctxcsn_e );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
449
				int change;
450
				attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
451
				if ( attr ) {
452
453
					int		i;

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
454
455
					change = 0;

456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
					for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
						int rc_sid;

						rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
						if ( rc_sid < 0 ) {
							Debug( LDAP_DEBUG_ANY,
								"%s: unable to extract SID "
								"from #%d contextCSN=%s\n",
								progname, i,
								attr->a_nvals[ i ].bv_val );
							continue;
						}

						assert( rc_sid <= SLAP_SYNC_SID_MAX );

						sid = (unsigned)rc_sid;

						if ( maxcsn[ sid ].bv_len == 0 ) {
							match = -1;

						} else {
							value_match( &match, slap_schema.si_ad_entryCSN,
								slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
								SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
								&maxcsn[ sid ], &attr->a_nvals[i], &text );
						}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
483
484
485
						if ( match > 0 ) {
							change = 1;
						} else {
486
487
488
489
490
491
							AC_MEMCPY( maxcsn[ sid ].bv_val,
								attr->a_nvals[ i ].bv_val,
								attr->a_nvals[ i ].bv_len );
							maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
							maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
						}
492
					}
493

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
494
495
496
497
498
499
500
501
					if ( change ) {
						if ( attr->a_nvals != attr->a_vals ) {
							ber_bvarray_free( attr->a_nvals );
						}
						attr->a_nvals = NULL;
						ber_bvarray_free( attr->a_vals );
						attr->a_vals = NULL;
						attr->a_numvals = 0;
502
					}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
503
504
				} else {
					change = 1;
505
				}
506

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
				if ( change ) {
					for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
						if ( maxcsn[ sid ].bv_len ) {
							attr_merge_one( e, slap_schema.si_ad_contextCSN,
								&maxcsn[ sid], NULL );
						}
					}

					ctxcsn_id = be->be_entry_modify( be, e, &bvtext );
					if( ctxcsn_id == NOID ) {
						fprintf( stderr, "%s: could not modify ctxcsn\n",
							progname);
						rc = EXIT_FAILURE;
					} else if ( verbose ) {
						fprintf( stderr, "modified: \"%s\" (%08lx)\n",
							e->e_dn, (long) ctxcsn_id );
523
					}
524
525
				}
				entry_free( e );
526
			}
527
		} 
528
529
530
531
	}

	ch_free( buf );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
532
	if ( !dryrun ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
533
534
535
		if ( enable_meter ) {
			fprintf( stderr, "Closing DB..." );
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
536
537
538
		if( be->be_entry_close( be ) ) {
			rc = EXIT_FAILURE;
		}
539

Pierangelo Masarati's avatar
Pierangelo Masarati committed
540
541
542
		if( be->be_sync ) {
			be->be_sync( be );
		}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
543
544
545
		if ( enable_meter ) {
			fprintf( stderr, "\n" );
		}
546
547
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
548
549
	if ( slap_tool_destroy())
		rc = EXIT_FAILURE;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
550

551
552
	return rc;
}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
553