slapadd.c 11.6 KB
Newer Older
1
2
3
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
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>
Howard Chu's avatar
Howard Chu 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;
67
	int lineno, nextline;
Howard Chu's avatar
Howard Chu committed
68
69
70
	int lmax;
	int rc = EXIT_SUCCESS;
	int manage = 0;	
71

Howard Chu's avatar
Howard Chu committed
72
73
74
75
	int enable_meter = 0;
	lutil_meter_t meter;
	struct stat stat_buf;

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

Howard Chu's avatar
Howard Chu 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 ||
Howard Chu's avatar
Howard Chu committed
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
	}

Howard Chu's avatar
Howard Chu committed
128
	if ( enable_meter 
129
130
131
132
133
134
#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) ) {
Howard Chu's avatar
Howard Chu committed
135
136
137
138
139
140
141
142
143
		enable_meter = !lutil_meter_open(
			&meter,
			&lutil_meter_text_display,
			&lutil_meter_linear_estimator,
			stat_buf.st_size);
	} else {
		enable_meter = 0;
	}

144
145
146
	/* nextline is the line number of the end of the current entry */
	for( lineno=1; ldif_read_record( ldiffp, &nextline, &buf, &lmax );
		lineno=nextline+1 ) {
Howard Chu's avatar
Howard Chu committed
147
		Entry *e;
148
149
150
151

		if ( lineno < jumpline )
			continue;

Howard Chu's avatar
Howard Chu committed
152
		e = str2entry2( buf, checkvals );
153

Howard Chu's avatar
Howard Chu committed
154
155
156
157
158
		if ( enable_meter )
			lutil_meter_update( &meter,
					 ftell( ldiffp->fp ),
					 0);

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
		/*
		 * 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 */
175
176
		if( BER_BVISEMPTY( &e->e_nname ) &&
			!BER_BVISEMPTY( be->be_nsuffix )) {
177
178
179
180
181
182
183
184
185
			fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
				progname, e->e_dn, lineno );
			rc = EXIT_FAILURE;
			entry_free( e );
			if( continuemode ) continue;
			break;
		}

		/* check backend */
186
		if( select_backend( &e->e_nname, nosubordinates )
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
			!= be )
		{
			fprintf( stderr, "%s: line %d: "
				"database (%s) not configured to hold \"%s\"\n",
				progname, lineno,
				be ? be->be_suffix[0].bv_val : "<none>",
				e->e_dn );
			fprintf( stderr, "%s: line %d: "
				"database (%s) not configured to hold \"%s\"\n",
				progname, lineno,
				be ? be->be_nsuffix[0].bv_val : "<none>",
				e->e_ndn );
			rc = EXIT_FAILURE;
			entry_free( e );
			if( continuemode ) continue;
			break;
		}

205
		{
206
207
208
209
210
211
212
213
214
215
216
217
218
219
			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 */
220
221
			op->o_bd = be;

222
			if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
223
				rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
224
					&text, textbuf, textlen );
225

226
227
228
229
230
231
232
233
				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;
				}
234
				textbuf[ 0 ] = '\0';
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
			}
		}

		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 ];

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

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

255
			csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 );
256
257
258
			csn.bv_val = csnbuf;

			timestamp.bv_val = timebuf;
259
260
261
			timestamp.bv_len = sizeof(timebuf);

			slap_timestamp( &now, &timestamp );
262

Pierangelo Masarati's avatar
Pierangelo Masarati committed
263
264
265
			if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
				BER_BVSTR( &name, SLAPD_ANONYMOUS );
				nname = name;
266
267
268
269
270
271
272
273
274
275
			} else {
				name = be->be_rootdn;
				nname = be->be_rootndn;
			}

			if( attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )
				== NULL )
			{
				vals[0].bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
				vals[0].bv_val = uuidbuf;
276
				attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, vals, NULL );
277
278
279
280
281
282
283
284
285
286
			}

			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 );
			}

Howard Chu's avatar
Howard Chu committed
287
			if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp )
288
289
				== NULL )
			{
Howard Chu's avatar
Howard Chu committed
290
291
				vals[0] = timestamp;
				attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL );
292
293
			}

Howard Chu's avatar
Howard Chu committed
294
			if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN )
295
296
				== NULL )
			{
Howard Chu's avatar
Howard Chu committed
297
298
				vals[0] = csn;
				attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL );
299
300
			}

Howard Chu's avatar
Howard Chu committed
301
			if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName )
302
303
				== NULL )
			{
Howard Chu's avatar
Howard Chu committed
304
305
306
				vals[0] = name;
				nvals[0] = nname;
				attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals );
307
308
			}

Howard Chu's avatar
Howard Chu committed
309
			if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp )
310
311
				== NULL )
			{
Howard Chu's avatar
Howard Chu committed
312
313
				vals[0] = timestamp;
				attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL );
314
315
			}

316
			if ( update_ctxcsn ) {
317
				int rc_sid;
318

319
				attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
320
				assert( attr != NULL );
321
322
323

				rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
				if ( rc_sid < 0 ) {
324
325
326
327
					Debug( LDAP_DEBUG_ANY, "%s: could not "
						"extract SID from entryCSN=%s\n",
						progname, attr->a_nvals[ 0 ].bv_val, 0 );

328
				} else {
329
					assert( rc_sid <= SLAP_SYNC_SID_MAX );
330

331
					sid = (unsigned)rc_sid;
332
333
334
335
336
337
338
339
340
341
342
343
344
					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;
					}
345
346
347
348
				}
			}
		}

349
350
351
352
353
354
355
356
357
358
		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;
359
			}
360
			if ( verbose )
361
362
				fprintf( stderr, "added: \"%s\" (%08lx)\n",
					e->e_dn, (long) id );
363
364
365
366
		} else {
			if ( verbose )
				fprintf( stderr, "added: \"%s\"\n",
					e->e_dn );
367
368
369
370
371
372
373
374
375
		}

		entry_free( e );
	}

	bvtext.bv_len = textlen;
	bvtext.bv_val = textbuf;
	bvtext.bv_val[0] = '\0';

Howard Chu's avatar
Howard Chu committed
376
377
378
379
380
	if ( enable_meter ) {
		lutil_meter_update( &meter, ftell( ldiffp->fp ), 1);
		lutil_meter_close( &meter );
	}

381
	if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) {
382
383
384
385
386
		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 {
Howard Chu's avatar
Howard Chu committed
387
388
			ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
			if ( ctxcsn_e != NULL ) {
389
				Entry *e = entry_dup( ctxcsn_e );
Howard Chu's avatar
Howard Chu committed
390
				int change;
391
				attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
392
				if ( attr ) {
393
394
					int		i;

Howard Chu's avatar
Howard Chu committed
395
396
					change = 0;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
397
					for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
398
						int rc_sid;
399

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

410
411
412
413
						assert( rc_sid <= SLAP_SYNC_SID_MAX );

						sid = (unsigned)rc_sid;

414
415
416
417
418
419
420
421
422
423
						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 );
						}

Howard Chu's avatar
Howard Chu committed
424
425
426
						if ( match > 0 ) {
							change = 1;
						} else {
427
428
429
430
431
432
							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;
						}
433
					}
434

Howard Chu's avatar
Howard Chu committed
435
436
437
438
439
440
441
442
					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;
443
					}
Howard Chu's avatar
Howard Chu committed
444
445
				} else {
					change = 1;
446
				}
447

Howard Chu's avatar
Howard Chu committed
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
				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 );
464
					}
465
				}
466
				entry_free( e );
467
			}
468
		} 
469
470
471
472
	}

	ch_free( buf );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
473
	if ( !dryrun ) {
474
475
476
		if ( enable_meter ) {
			fprintf( stderr, "Closing DB..." );
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
477
478
479
		if( be->be_entry_close( be ) ) {
			rc = EXIT_FAILURE;
		}
480

Pierangelo Masarati's avatar
Pierangelo Masarati committed
481
482
483
		if( be->be_sync ) {
			be->be_sync( be );
		}
484
485
486
		if ( enable_meter ) {
			fprintf( stderr, "\n" );
		}
487
488
	}

489
490
	if ( slap_tool_destroy())
		rc = EXIT_FAILURE;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
491

492
493
	return rc;
}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
494