slapadd.c 3.92 KB
Newer Older
1
/* $OpenLDAP$ */
2
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4
5
6
7
8
9
10
11
12
13
14
15
16
 * 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>

17
18
19
#include <lber.h>
#include <ldif.h>

20
21
22
23
24
#include "slapcommon.h"

int
main( int argc, char **argv )
{
25
	char		*buf = NULL;
26
27
28
29
	int         lineno;
	int         lmax;
	int			rc = EXIT_SUCCESS;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
30
	const char *text;
31
	char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
Kurt Zeilenga's avatar
Kurt Zeilenga committed
32
33
	size_t textlen = sizeof textbuf;

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
36
37
	if( !be->be_entry_open ||
		!be->be_entry_close ||
38
39
40
41
42
43
44
45
		!be->be_entry_put )
	{
		fprintf( stderr, "%s: database doesn't support necessary operations.\n",
			progname );
		exit( EXIT_FAILURE );
	}

	lmax = 0;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
46
	lineno = 0;
47
48
49
50
51
52
53
54
55

	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 ) ) {
		Entry *e = str2entry( buf );
56
57
58
59
		struct berval bvtext;

		bvtext.bv_len = textlen;
		bvtext.bv_val = textbuf;
60
61

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
69
		/* make sure the DN is not empty */
70
		if( !e->e_nname.bv_len ) {
71
72
73
74
75
76
77
78
			fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
				progname, e->e_dn, lineno );
			rc = EXIT_FAILURE;
			entry_free( e );
			if( continuemode ) continue;
			break;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
79
		/* check backend */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
80
81
82
		if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
			!= be )
		{
83
84
85
			fprintf( stderr, "%s: line %d: "
				"database (%s) not configured to hold \"%s\"\n",
				progname, lineno,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
86
				be ? be->be_suffix[0]->bv_val : "<none>",
87
88
89
90
91
92
				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 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
93
94
95
96
97
98
			rc = EXIT_FAILURE;
			entry_free( e );
			if( continuemode ) continue;
			break;
		}

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
		{
			Attribute *sc = attr_find( e->e_attrs,
				slap_schema.si_ad_structuralObjectClass );
			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;
			}

			if( sc == NULL ) {
116
				struct berval vals[2];
Kurt Zeilenga's avatar
Kurt Zeilenga committed
117

Howard Chu's avatar
Howard Chu committed
118
119
				int ret = structural_class( oc->a_vals, vals,
					NULL, &text, textbuf, textlen );
120

121
				if( vals[0].bv_len == 0 ) {
122
123
124
125
126
127
128
129
					fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
					progname, e->e_dn, lineno, text );
					rc = EXIT_FAILURE;
					entry_free( e );
					if( continuemode ) continue;
					break;
				}

130
				vals[1].bv_val = NULL;
131
132
133
134
135
				attr_merge( e, slap_schema.si_ad_structuralObjectClass,
					vals );
			}
		}

136
		if( global_schemacheck ) {
137
			/* check schema */
138

139
			rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
140
141

			if( rc != LDAP_SUCCESS ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
142
143
				fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
					progname, e->e_dn, lineno, text );
144
145
146
147
148
				rc = EXIT_FAILURE;
				entry_free( e );
				if( continuemode ) continue;
				break;
			}
149
150
		}

151
152
153
154
155
156
157
158
159
160
		if (!dryrun) {
			ID 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;
			}
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
161
		
162
163
164
165
166
167
168
169
			if ( verbose ) {
				fprintf( stderr, "added: \"%s\" (%08lx)\n",
					e->e_dn, (long) id );
			}
		} else {
			if ( verbose ) {
				fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
			}
170
171
172
173
174
		}

		entry_free( e );
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
175
	ch_free( buf );
176

177
178
179
180
181
182
	be->be_entry_close( be );

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

183
	slap_tool_destroy();
184
185
	return rc;
}