slapadd.c 2.43 KB
Newer Older
1
/* $OpenLDAP$ */
2
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4
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
38
39
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
#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 "slapcommon.h"

int
main( int argc, char **argv )
{
	char		*buf;
	int         lineno;
	int         lmax;
	int			rc = EXIT_SUCCESS;

	slap_tool_init( "slapadd", SLAPADD, argc, argv );

	if( !be->be_entry_open &&
		!be->be_entry_close &&
		!be->be_entry_put )
	{
		fprintf( stderr, "%s: database doesn't support necessary operations.\n",
			progname );
		exit( EXIT_FAILURE );
	}

	buf = NULL;
	lmax = 0;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
40
	lineno = 0;
41
42
43
44
45
46
47
48
49
50
51
52

	if( be->be_entry_open( be, 1 ) != 0 ) {
		fprintf( stderr, "%s: could not open database.\n",
			progname );
		exit( EXIT_FAILURE );
	}

	while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
		ID id;
		Entry *e = str2entry( buf );

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

60
61
62
63
64
65
66
67
68
		/* make sure the DN is valid */
		if( dn_normalize( e->e_ndn ) == NULL ) {
			fprintf( stderr, "%s: bad dn=\"%s\" (line=%d)\n",
				progname, e->e_dn, lineno );
			rc = EXIT_FAILURE;
			entry_free( e );
			if( continuemode ) continue;
			break;
		}
69

70
		if( !noschemacheck ) {
71
			/* check schema */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
72
			const char *text;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
73
74
75
			if ( entry_schema_check( e, NULL, &text ) != LDAP_SUCCESS ) {
				fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
					progname, e->e_dn, lineno, text );
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
				rc = EXIT_FAILURE;
				entry_free( e );
				if( continuemode ) continue;
				break;
			}

			/* check backend */
			if( select_backend( e->e_ndn ) != be ) {
				fprintf( stderr, "%s: database not configured to hold dn=\"%s\" (line=%d)\n",
					progname, e->e_dn, lineno );
				rc = EXIT_FAILURE;
				entry_free( e );
				if( continuemode ) continue;
				break;
			}
91
92
93
94
95
		}

		id = be->be_entry_put( be, e );

		if( id == NOID ) {
96
			fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d)\n",
97
98
				progname, e->e_dn, lineno );
			rc = EXIT_FAILURE;
99
100
101
			entry_free( e );
			if( continuemode ) continue;
			break;
102
103

		} else if ( verbose ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
			fprintf( stderr, "added: \"%s\" (%08lx)\n",
105
106
107
108
109
110
				e->e_dn, (long) id );
		}

		entry_free( e );
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
111
	ch_free( buf );
112

113
114
115
116
117
118
	be->be_entry_close( be );

	if( be->be_sync ) {
		be->be_sync( be );
	}

119
	slap_tool_destroy();
120
121
	return rc;
}